E - The type of the entries.public final class ListSet<E> extends AbstractSet<E> implements SortedSet<E>
java.util.ArrayList.
The iteration order of the set
is the iteration order of the underlying ArrayList.
This class permits the null element.
The size, isEmpty, and iterator operations run in constant time. The add operation runs in amortized constant time, that is, adding n elements requires O(n) time. All of the other operations run in linear time (roughly speaking).
Each ListSet instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the set size. As elements are added an ListSet, its capacity grows automatically. The details of the growth policy are not specified beyond the fact that adding an element has constant amortized time cost.
Note that this implementation is not synchronized. If multiple threads access a set concurrently, and at least one of the threads modifies the set, it must be synchronized externally. This is typically accomplished by synchronizing on some object that naturally encapsulates the set. If no such object exists, the set should be "wrapped" using the Collections.synchronizedSet method. This is best done at creation time, to prevent accidental unsynchronized access to the ListSet instance:
Set s = Collections.synchronizedSet(new ListSet(...));
The iterators returned by this class's iterator method are fail-fast: if the set ismodified at any time after the iterator is created, in any way except through the iterator's own remove method, the Iterator throws a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.
Collection,
List,
ArrayList.add(E),
SortedSet,
HashSet,
TreeSet,
AbstractSet,
Collections| Modifier and Type | Field and Description |
|---|---|
private Comparator<? super E> |
innerCmp
If
outerCmp is not null,
the two comparators coincide. |
private List<E> |
list
Contains the elements of this set.
|
private Comparator<? super E> |
outerCmp
The comparator initialized in the constructor
and returned by
comparator(). |
| Modifier | Constructor and Description |
|---|---|
|
ListSet()
Constructs a new, empty set,
sorted according to the natural ordering of its elements.
|
|
ListSet(Collection<? extends E> coll)
Creates a new set containing the elements of the specified collection
in the natural ordering.
|
|
ListSet(Comparator<? super E> cmp)
Constructs a new, empty set,
sorted according to the specified comparator
cmp. |
private |
ListSet(List<E> list,
Comparator<? super E> cmp)
Creates a new
ListSet
with elements given by list
and ordering given by cmp. |
|
ListSet(SortedSet<E> sortedSet)
Constructs a new set containing the same elements
and using the same ordering as the
sortedSet. |
| Modifier and Type | Method and Description |
|---|---|
boolean |
add(E obj)
Adds the specified element to this set if it is not already present.
|
boolean |
addAll(Collection<? extends E> coll)
Adds all of the elements in the specified collection to this set
if they're not already present.
|
private boolean |
addList(List<E> list1,
List<E> list2)
Merges
list2 into list1
and returns whether l1 had been modified. |
void |
clear()
Removes all of the elements from this set.
|
Comparator<? super E> |
comparator() |
boolean |
contains(Object obj)
Returns
true if this set contains the specified element. |
boolean |
containsAll(Collection<?> coll)
Returns
true if this set contains all of the elements
of the specified collection. |
boolean |
equals(Object obj)
**** ask at oracle whether for sorted sets equality
should include ordering. ****
Compares the specified object with this set for equality.
|
E |
first() |
List<E> |
getList()
Returns a list backing this set,
so changes in the returned list are reflected in this set,
and vice-versa.
|
int |
hashCode()
Returns the hash code value for this set.
|
SortedSet<E> |
headSet(E toObj) |
boolean |
isEmpty()
Returns
true if this set contains no elements. |
private boolean |
isSortedWithSameComparator(Collection<?> coll)
Returns whether this set has the same comparator as
the collection given.
|
Iterator<E> |
iterator()
Returns an iterator over the elements in this set.
|
E |
last() |
ListIterator<E> |
listIterator()
Returns an iterator over the elements in this set.
|
static void |
main(String[] args) |
(package private) int |
obj2idx(E obj) |
boolean |
remove(Object obj)
Removes the specified element from this set if it is present.
|
boolean |
removeAll(Collection<?> coll)
Removes from this set all of its elements
that are contained in the specified collection.
|
boolean |
retainAll(Collection<?> coll)
Retains only the elements in this set
that are contained in the specified collection.
|
int |
size()
Returns the number of elements in this set (its cardinality).
|
static <E> ListSet<E> |
sortedAsAdded()
Creates a new
ListSet with ordering as added
in ascending ordering. |
static <E> ListSet<E> |
sortedAsListed(List<E> list)
Returns an
ListSet with elements and ordering
given by list
as described in Comparators.getAsListed(List). |
ListSet<E> |
subSet(E fromObj,
E toObj) |
(package private) ListSet<E> |
subSetIdx(int fromIdx,
int toIdx) |
SortedSet<E> |
tailSet(E fromObj) |
Object[] |
toArray()
Returns an array containing all of the elements in this set;
the order is as in the backing list
list. |
<T> T[] |
toArray(T[] arr)
Returns an array containing all of the elements in this set
whose runtime type is that of the specified array;
the order is as in the backing list
list. |
String |
toString()
Returns a string representation of this set.
|
clone, finalize, getClass, notify, notifyAll, wait, wait, waitspliteratorparallelStream, removeIf, streamprivate final List<E> list
list.get(a).equals(list.get(b))
implies a == b.private final Comparator<? super E> outerCmp
comparator().
This may well be null and is nowhere used directly.private final Comparator<? super E> innerCmp
outerCmp is not null,
the two comparators coincide.
Otherwise this comparator reflects the natural ordering
and throws a ClassCastException
if feeded with no appropriate Comparable.private ListSet(List<E> list, Comparator<? super E> cmp)
ListSet
with elements given by list
and ordering given by cmp.
If the latter is null
then the type parameter of list
must be a Comparable.
and list must
This is inspired by a constructor of TreeSet.list - the list wrapped by this set.
It must be ordered ascending according to cmp
if this is non-null, else as Comparables.
It is desireable that list
contains each element once only.cmp - A comparator or null.public ListSet(SortedSet<E> sortedSet)
sortedSet.sortedSet - sorted set whose elements will comprise the new setNullPointerException - if the specified sorted set is null.public ListSet(Collection<? extends E> coll)
e1.compareTo(e2) must not throw a ClassCastException
for any elements e1 and e2 in the set.
The backing ArrayList instance list
has an initial capacity of 110% the size of the specified collection.
coll - the collection whose elements are to be placed into this set.ClassCastException - if the elements in c are not Comparable,
or are not mutually comparable.NullPointerException - if the specified collection is null.public ListSet(Comparator<? super E> cmp)
cmp.
For cmp==null
a comparator defining the natural ordering is assumed.
All elements inserted into the set
must be mutually comparable by the specified comparator:
cmp.compare(e1, e2) must not throw a ClassCastException
for any elements e1 and e2 in the set.
If the user attempts to add an element to the set
that violates this constraint,
the add call will throw a ClassCastException.
Implementational note:
Whereas outerCmp is initialized with cmp
whether it is null or not,
innerCmp is initialized with cmp
only if this is not null.
Otherwise, it is initialized with a comparator
defining the natural ordering.
cmp - the comparator that will be used to order this set.
If null, the natural ordering of the elements will be used.public ListSet()
e1.compareTo(e2) must not throw a ClassCastException
for any elements e1 and e2 in the set.
If the user attempts to add an element to the set
that violates this constraint
(for example, the user attempts to add a string element to a set
whose elements are integers),
the add call will throw a ClassCastException.
For a generalization to a given comparator
see ListSet(Comparator).public static <E> ListSet<E> sortedAsAdded()
ListSet with ordering as added
in ascending ordering.public static <E> ListSet<E> sortedAsListed(List<E> list)
ListSet with elements and ordering
given by list
as described in Comparators.getAsListed(List).
If an element is added to list,
the ordering is as added at the end of list.
CAUTION: The list backs this set:
changes in the list affect this set and the other way round.
Changing the list also changes the ordering.NullPointerException - if list is null.public List<E> getList()
public int size()
Integer.MAX_VALUE elements,
returns Integer.MAX_VALUE.size in interface Collection<E>size in interface Set<E>size in class AbstractCollection<E>public boolean isEmpty()
true if this set contains no elements.isEmpty in interface Collection<E>isEmpty in interface Set<E>isEmpty in class AbstractCollection<E>true if this set contains no elements.public boolean contains(Object obj)
true if this set contains the specified element.
More formally, returns true if and only
if this set contains an element e
such that (obj==null ? e==null : obj.equals(e)).contains in interface Collection<E>contains in interface Set<E>contains in class AbstractCollection<E>obj - obj is some object that may be in this collection.true if this set contains the specified element.public Iterator<E> iterator()
list.public ListIterator<E> listIterator()
list.public Object[] toArray()
list.
Obeys the general contract of the Collection.toArray method.toArray in interface Collection<E>toArray in interface Set<E>toArray in class AbstractCollection<E>public <T> T[] toArray(T[] arr)
list.
Obeys the general contract
of the Collection.toArray(Object[]) method.toArray in interface Collection<E>toArray in interface Set<E>toArray in class AbstractCollection<E>arr - the array into which the elements of this set are to be stored,
if it is big enough;
otherwise, a new array of the same runtime type is allocated
for this purpose.ArrayStoreException - the runtime type of a is not a supertype
of the runtime type of every element in this set.public boolean add(E obj)
o,
to this set if this set contains no element e
such that (o==null ? e==null : o.equals(e)).
If this set already contains the specified element,
the call leaves this set unchanged and returns false.
In combination with the restriction on constructors,
this ensures that sets never contain duplicate elements.
Note that also null may be added to this set,
but only if it is not already present, this changes this set.
add in interface Collection<E>add in interface Set<E>add in class AbstractCollection<E>obj - element to be added to this set.true if this set did not already contain the specified
element.public boolean remove(Object obj)
e
such that (o==null ? e==null : o.equals(e)),
if the set contains such an element.
Returns true if the set contained the specified element
(or equivalently, if the set changed as a result of the call).
(The set will not contain the specified element once the call returns.)remove in interface Collection<E>remove in interface Set<E>remove in class AbstractCollection<E>obj - object to be removed from this set, if present.true if the set contained the specified element.public boolean containsAll(Collection<?> coll)
true if this set contains all of the elements
of the specified collection.
If the specified collection is also a set,
this method returns true
if it is a subset of this set.containsAll in interface Collection<E>containsAll in interface Set<E>containsAll in class AbstractCollection<E>coll - collection to be checked for containment in this set.true if this set contains all of the elements
of the specified collection.private boolean isSortedWithSameComparator(Collection<?> coll)
coll - a collection which may be a SortedSet or not.true if c is a sorted set
and if c.comparator yields the same result:
null
null
and equals the other one.
This case implies that neither are null.
public boolean addAll(Collection<? extends E> coll)
addAll operation effectively modifies this set so
that its value is the union of the two sets.
The behavior of this operation is unspecified if the specified
collection is modified while the operation is in progress.addAll in interface Collection<E>addAll in interface Set<E>addAll in class AbstractCollection<E>coll - collection whose elements are to be added to this set.true if this set changed as a result of the call.add(E)private boolean addList(List<E> list1, List<E> list2)
list2 into list1
and returns whether l1 had been modified.
At the end, list2 is not modified
and list1 is ordered again.public boolean retainAll(Collection<?> coll)
retainAll in interface Collection<E>retainAll in interface Set<E>retainAll in class AbstractCollection<E>coll - collection that defines which elements this set will retain.true if this collection changed as a result of the call.remove(java.lang.Object)public boolean removeAll(Collection<?> coll)
removeAll in interface Collection<E>removeAll in interface Set<E>removeAll in class AbstractSet<E>coll - collection that defines
which elements will be removed from this set.true if this set changed as a result of the call.remove(java.lang.Object)public void clear()
clear in interface Collection<E>clear in interface Set<E>clear in class AbstractCollection<E>public Comparator<? super E> comparator()
comparator in interface SortedSet<E>int obj2idx(E obj)
public boolean equals(Object obj)
true if the specified object is also a set,
the two sets have the same size,
and every member of the specified set is contained in this set
(or equivalently, every member of this set
is contained in the specified set).
This definition ensures that the equals method works properly
across different implementations of the set interface.equals in interface Collection<E>equals in interface Set<E>equals in class AbstractSet<E>obj - Object to be compared for equality with this set.true if the specified Object is equal to this set.public String toString()
toString in class AbstractCollection<E>String-representations
of the elements of this list
enclosed in triangle brackets.public int hashCode()
null element is defined to be zero.
This ensures that s1.equals(s2) implies that
s1.hashCode()==s2.hashCode() for any two sets
s1 and s2, as required by the general
contract of the Object.hashCode method.hashCode in interface Collection<E>hashCode in interface Set<E>hashCode in class AbstractSet<E>Object.hashCode(),
Object.equals(Object),
Set.equals(Object)public static void main(String[] args)
Copyright © 2012–2018 Simuline Organization (l2r). All rights reserved.