List to ensure that no duplicates are present much
like a Set.
The List interface makes certain assumptions/requirements. This
implementation breaks these in certain ways, but this is merely the result of
rejecting duplicates. Each violation is explained in the method, but it
should not affect you. Bear in mind that Sets require immutable objects to
function correctly.
The ListOrderedSet
class provides an alternative approach, by wrapping an existing Set and
retaining insertion order in the iterator.
This class is Serializable from Commons Collections 3.1.
| Modifier and Type | Class and Description |
|---|---|
| pack-priv static class | |
| pack-priv static class |
| Modifier and Type | Field and Description |
|---|---|
| private static final long | serialVersionUID
Hides org. |
| private final Set | set
Internal Set to maintain uniqueness. |
| Access | Constructor and Description |
|---|---|
| protected | SetUniqueList(final List<E>
the list to decorate, must not be null list, final Set<E> the set to decorate, must not be null set)Constructor that wraps (not copies) the List and specifies the set to use. |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | Returns: true if object was addedthe object to add object)Overrides org. Implements java. |
| public void | add(final int
the index to insert at index, final E the object to add object)Overrides org. Implements java. |
| public boolean | Returns: true if this collection changedthe collection to add in iterator order coll)Overrides org. Implements java. |
| public boolean | Returns: true if this collection changedthe index to insert at index, final Collection<? extends E> the collection to add in iterator order coll)Overrides org. Implements java. |
| public Set | |
| public void | clear()
Overrides org. Implements java. |
| public boolean | contains(final Object
element whose presence in this list is to be tested object)Overrides org. Implements java. true if this list contains the specified element.
|
| public boolean | containsAll(final Collection<?>
collection to be checked for containment in this list coll)Overrides org. Implements java. true if this list contains all of the elements of the
specified collection.
|
| protected Set | createSetBasedOnList(final Set<E>
the set, final List<E> list)Set to be used as return type, must not be nullCreate a new |
| public Iterator | iterator()
Overrides org. Implements java. |
| public ListIterator | listIterator()
Overrides org. Implements java. |
| public ListIterator | listIterator(final int
index of the first element to be returned from the
list iterator (by a call to index)next)Overrides org. Implements java. |
| public boolean | remove(final Object
element to be removed from this list, if present object)Overrides org. Implements java. |
| public E | remove(final int
the index of the element to be removed index)Overrides org. Implements java. |
| public boolean | removeAll(final Collection<?>
collection containing elements to be removed from this list coll)Overrides org. Implements java. |
| public boolean | removeIf(Predicate<? super E>
a predicate which returns filter)true for elements to be
removedOverrides org. Overrides default java. |
| public boolean | retainAll(final Collection<?>
collection containing elements to be retained in this list coll)Overrides org. Implements java. |
| public E | Returns: the previous objectthe index to insert at index, final E the object to set object)Overrides org. Implements java. |
| public static < the element type E> SetUniqueList | Returns: a newSetUniqueListthe list to decorate, must not be null list)Factory method to create a SetList using the supplied list to retain order. |
| public List | subList(final int
low endpoint (inclusive) of the subList fromIndex, final int high endpoint (exclusive) of the subList toIndex)Overrides org. Implements java. fromIndex, inclusive, and toIndex, exclusive.
|
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID Hides org. Serialization version. | |
| set | back to summary |
|---|---|
| private final Set<E> set Internal Set to maintain uniqueness. | |
| SetUniqueList | back to summary |
|---|---|
| protected SetUniqueList(final List<E> list, final Set<E> set) Constructor that wraps (not copies) the List and specifies the set to use. The set and list must both be correctly initialised to the same elements.
| |
| add | back to summary |
|---|---|
| public boolean add(final E object) Overrides org. Implements java. Adds an element to the list if it is not already present.
(Violation) The
| |
| add | back to summary |
|---|---|
| public void add(final int index, final E object) Overrides org. Implements java. Adds an element to a specific index in the list if it is not already present.
(Violation) The
| |
| addAll | back to summary |
|---|---|
| public boolean addAll(final Collection<? extends E> coll) Overrides org. Implements java. Adds a collection of objects to the end of the list avoiding duplicates. Only elements that are not already in this list will be added, and duplicates from the specified collection will be ignored.
(Violation) The
| |
| addAll | back to summary |
|---|---|
| public boolean addAll(final int index, final Collection<? extends E> coll) Overrides org. Implements java. Adds a collection of objects a specific index in the list avoiding duplicates. Only elements that are not already in this list will be added, and duplicates from the specified collection will be ignored.
(Violation) The
| |
| asSet | back to summary |
|---|---|
| public Set Gets an unmodifiable view as a Set.
| |
| clear | back to summary |
|---|---|
| public void clear() Overrides org. Implements java. Doc from java. Removes all of the elements from this list (optional operation). The list will be empty after this call returns.
| |
| contains | back to summary |
|---|---|
| public boolean contains(final Object object) Overrides org. Implements java. Doc from java. Returns | |
| containsAll | back to summary |
|---|---|
| public boolean containsAll(final Collection<?> coll) Overrides org. Implements java. Doc from java. Returns
| |
| createSetBasedOnList | back to summary |
|---|---|
| protected Set Create a new | |
| iterator | back to summary |
|---|---|
| public Iterator Overrides org. Implements java. Doc from java. Returns an iterator over the elements in this list in proper sequence. | |
| listIterator | back to summary |
|---|---|
| public ListIterator Overrides org. Implements java. Doc from java. Returns a list iterator over the elements in this list (in proper sequence).
| |
| listIterator | back to summary |
|---|---|
| public ListIterator Overrides org. Implements java. Doc from java. Returns a list iterator over the elements in this list (in proper
sequence), starting at the specified position in the list.
The specified index indicates the first element that would be
returned by an initial call to
| |
| remove | back to summary |
|---|---|
| public boolean remove(final Object object) Overrides org. Implements java. Doc from java. Removes the first occurrence of the specified element from this list,
if it is present (optional operation). If this list does not contain
the element, it is unchanged. More formally, removes the element with
the lowest index | |
| remove | back to summary |
|---|---|
| public E remove(final int index) Overrides org. Implements java. Doc from java. Removes the element at the specified position in this list (optional operation). Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the list.
| |
| removeAll | back to summary |
|---|---|
| public boolean removeAll(final Collection<?> coll) Overrides org. Implements java. Doc from java. Removes from this list all of its elements that are contained in the specified collection (optional operation).
| |
| removeIf | back to summary |
|---|---|
| public boolean removeIf(Predicate<? super E> filter) Overrides org. Overrides default java. Doc from java. Removes all of the elements of this collection that satisfy the given predicate. Errors or runtime exceptions thrown during iteration or by the predicate are relayed to the caller. | |
| retainAll | back to summary |
|---|---|
| public boolean retainAll(final Collection<?> coll) Overrides org. Implements java. Doc from java. Retains only the elements in this list that are contained in the specified collection (optional operation). In other words, removes from this list all of its elements that are not contained in the specified collection.
This implementation iterates over the elements of this list, checking
each element in turn to see if it's contained in
| |
| set | back to summary |
|---|---|
| public E set(final int index, final E object) Overrides org. Implements java. Sets the value at the specified index avoiding duplicates. The object is set into the specified index. Afterwards, any previous duplicate is removed. If the object is not already in the list then a normal set occurs. If it is present, then the old version is removed.
| |
| setUniqueList | back to summary |
|---|---|
| public static <E> SetUniqueList Factory method to create a SetList using the supplied list to retain order.
If the list contains duplicates, these are removed (first indexed one
kept). A
| |
| subList | back to summary |
|---|---|
| public List Overrides org. Implements java. Doc from java. Returns a view of the portion of this list between the specified
This method eliminates the need for explicit range operations (of the sort that commonly exist for arrays). Any operation that expects a list can be used as a range operation by passing a subList view instead of a whole list. For example, the following idiom removes a range of elements from a list:
Similar idioms may be constructed for indexOf and
lastIndexOf, and all of the algorithms in the
Collections class can be applied to a subList.The semantics of the list returned by this method become undefined if the backing list (i.e., this list) is structurally modified in any way other than via the returned list. (Structural modifications are those that change the size of this list, or otherwise perturb it in such a fashion that iterations in progress may yield incorrect results.) Note from 4.0, an unmodifiable list will be returned, as changes to the subList can invalidate the parent list. | |
| Access | Constructor and Description |
|---|---|
| protected |
| Modifier and Type | Method and Description |
|---|---|
| public E | next()
Overrides org. Implements java. |
| public void | remove()
Overrides org. Overrides default java. |
| SetListIterator | back to summary |
|---|---|
| protected SetListIterator(final Iterator<E> it, final Set<E> set) | |
| next | back to summary |
|---|---|
| public E next() Overrides org. Implements java. Doc from org. Returns the next element in the iteration.
| |
| remove | back to summary |
|---|---|
| public void remove() Overrides org. Overrides default java. Doc from java. Removes from the underlying collection the last element returned
by this iterator (optional operation). This method can be called
only once per call to The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method, unless an overriding class has specified a concurrent modification policy.
The behavior of an iterator is unspecified if this method is called
after a call to the
| |
| Access | Constructor and Description |
|---|---|
| protected |
| Modifier and Type | Method and Description |
|---|---|
| public void | add(final E
the element to insert object)Overrides org. Implements java. |
| public E | next()
Overrides org. Implements java. |
| public E | previous()
Overrides org. Implements java. |
| public void | remove()
Overrides org. Implements java. next or previous (optional operation).
|
| public void | set(final E
the element with which to replace the last element returned by
object)next or previousOverrides org. Implements java. next or
previous with the specified element (optional operation).
|
| SetListListIterator | back to summary |
|---|---|
| protected SetListListIterator(final ListIterator<E> it, final Set<E> set) | |
| add | back to summary |
|---|---|
| public void add(final E object) Overrides org. Implements java. Doc from org. Inserts the specified element into the list (optional operation).
The element is inserted immediately before the element that
would be returned by
| |
| next | back to summary |
|---|---|
| public E next() Overrides org. Implements java. Doc from org. Returns the next element in the list and advances the cursor position.
This method may be called repeatedly to iterate through the list,
or intermixed with calls to
| |
| previous | back to summary |
|---|---|
| public E previous() Overrides org. Implements java. Doc from org. Returns the previous element in the list and moves the cursor
position backwards. This method may be called repeatedly to
iterate through the list backwards, or intermixed with calls to
| |
| remove | back to summary |
|---|---|
| public void remove() Overrides org. Implements java. Doc from org. Removes from the list the last element that was returned by
| |
| set | back to summary |
|---|---|
| public void set(final E object) Overrides org. Implements java. Doc from org. Replaces the last element returned by
| |