Top Description Inners Fields Constructors Methods
org.apache.commons.collections4.list

public Class SetUniqueList<E>

extends AbstractSerializableListDecorator<E>
Class Inheritance
Imports
java.lang.reflect.InvocationTargetException, java.util.ArrayList, .Collection, .HashSet, .Iterator, .List, .ListIterator, .Set, java.util.function.Predicate, org.apache.commons.collections4.ListUtils, org.apache.commons.collections4.iterators.AbstractIteratorDecorator, .AbstractListIteratorDecorator, org.apache.commons.collections4.set.UnmodifiableSet

Decorates a 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.

Since
3.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
SetUniqueList.SetListIterator<E>

Inner class iterator.

pack-priv static class
SetUniqueList.SetListListIterator<E>

Inner class iterator.

Field Summary

Modifier and TypeField and Description
private static final long
private final Set<E>
set

Internal Set to maintain uniqueness.

Constructor Summary

AccessConstructor 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.

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

true if object was added
add
(final E
the object to add
object
)

Overrides org.apache.commons.collections4.collection.AbstractCollectionDecorator.add.

Implements java.util.List.add, java.util.Collection.add.

Adds an element to the list if it is not already present.
public void
add(final int
the index to insert at
index
,
final E
the object to add
object
)

Overrides org.apache.commons.collections4.list.AbstractListDecorator.add.

Implements java.util.List.add.

Adds an element to a specific index in the list if it is not already present.
public boolean

Returns:

true if this collection changed
addAll
(final Collection<? extends E>
the collection to add in iterator order
coll
)

Overrides org.apache.commons.collections4.collection.AbstractCollectionDecorator.addAll.

Implements java.util.List.addAll, java.util.Collection.addAll.

Adds a collection of objects to the end of the list avoiding duplicates.
public boolean

Returns:

true if this collection changed
addAll
(final int
the index to insert at
index
,
final Collection<? extends E>
the collection to add in iterator order
coll
)

Overrides org.apache.commons.collections4.list.AbstractListDecorator.addAll.

Implements java.util.List.addAll.

Adds a collection of objects a specific index in the list avoiding duplicates.
public Set<E>

Returns:

an unmodifiable set view
asSet
()

Gets an unmodifiable view as a Set.

public void
clear()

Overrides org.apache.commons.collections4.collection.AbstractCollectionDecorator.clear.

Implements java.util.List.clear, java.util.Collection.clear.

Removes all of the elements from this list (optional operation).
public boolean
contains(final Object
element whose presence in this list is to be tested
object
)

Overrides org.apache.commons.collections4.collection.AbstractCollectionDecorator.contains.

Implements java.util.List.contains, java.util.Collection.contains.

Returns 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.apache.commons.collections4.collection.AbstractCollectionDecorator.containsAll.

Implements java.util.List.containsAll, java.util.Collection.containsAll.

Returns true if this list contains all of the elements of the specified collection.
protected Set<E>

Returns:

a new Set populated with all elements of the provided List
createSetBasedOnList
(final Set<E>
the Set to be used as return type, must not be null
set
,
final List<E>
the List to populate the Set
list
)

Create a new Set with the same type as the provided set and populate it with all elements of list.

public Iterator<E>
iterator()

Overrides org.apache.commons.collections4.collection.AbstractCollectionDecorator.iterator.

Implements java.util.List.iterator, java.util.Collection.iterator.

Returns an iterator over the elements in this list in proper sequence.
public ListIterator<E>
listIterator()

Overrides org.apache.commons.collections4.list.AbstractListDecorator.listIterator.

Implements java.util.List.listIterator.

Returns a list iterator over the elements in this list (in proper sequence).
public ListIterator<E>
listIterator(final int
index of the first element to be returned from the list iterator (by a call to next)
index
)

Overrides org.apache.commons.collections4.list.AbstractListDecorator.listIterator.

Implements java.util.List.listIterator.

Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.
public boolean
remove(final Object
element to be removed from this list, if present
object
)

Overrides org.apache.commons.collections4.collection.AbstractCollectionDecorator.remove.

Implements java.util.List.remove, java.util.Collection.remove.

Removes the first occurrence of the specified element from this list, if it is present (optional operation).
public E
remove(final int
the index of the element to be removed
index
)

Overrides org.apache.commons.collections4.list.AbstractListDecorator.remove.

Implements java.util.List.remove.

Removes the element at the specified position in this list (optional operation).
public boolean
removeAll(final Collection<?>
collection containing elements to be removed from this list
coll
)

Overrides org.apache.commons.collections4.collection.AbstractCollectionDecorator.removeAll.

Implements java.util.List.removeAll, java.util.Collection.removeAll.

Removes from this list all of its elements that are contained in the specified collection (optional operation).
public boolean
removeIf(Predicate<? super E>
a predicate which returns true for elements to be removed
filter
)

Overrides org.apache.commons.collections4.collection.AbstractCollectionDecorator.removeIf.

Overrides default java.util.Collection.removeIf.

Removes all of the elements of this collection that satisfy the given predicate.
public boolean
retainAll(final Collection<?>
collection containing elements to be retained in this list
coll
)

Overrides org.apache.commons.collections4.collection.AbstractCollectionDecorator.retainAll.

Implements java.util.List.retainAll, java.util.Collection.retainAll.

Retains only the elements in this list that are contained in the specified collection (optional operation).
public E

Returns:

the previous object
set
(final int
the index to insert at
index
,
final E
the object to set
object
)

Overrides org.apache.commons.collections4.list.AbstractListDecorator.set.

Implements java.util.List.set.

Sets the value at the specified index avoiding duplicates.
public static <
the element type
E
>
SetUniqueList<E>

Returns:

a new SetUniqueList
setUniqueList
(final List<E>
the list to decorate, must not be null
list
)

Factory method to create a SetList using the supplied list to retain order.

public List<E>
subList(final int
low endpoint (inclusive) of the subList
fromIndex
,
final int
high endpoint (exclusive) of the subList
toIndex
)

Overrides org.apache.commons.collections4.list.AbstractListDecorator.subList.

Implements java.util.List.subList.

Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.

Field Detail

serialVersionUIDback to summary
private static final long serialVersionUID

Hides org.apache.commons.collections4.list.AbstractSerializableListDecorator.serialVersionUID.

Serialization version.

setback to summary
private final Set<E> set

Internal Set to maintain uniqueness.

Constructor Detail

SetUniqueListback 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.

Parameters
list:List<E>

the list to decorate, must not be null

set:Set<E>

the set to decorate, must not be null

Exceptions
NullPointerException:
if set or list is null

Method Detail

addback to summary
public boolean add(final E object)

Overrides org.apache.commons.collections4.collection.AbstractCollectionDecorator.add.

Implements java.util.List.add, java.util.Collection.add.

Adds an element to the list if it is not already present.

(Violation) The List interface requires that this method returns true always. However this class may return false because of the Set behaviour.

Parameters
object:E

the object to add

Returns:boolean

true if object was added

Annotations
@Override
addback to summary
public void add(final int index, final E object)

Overrides org.apache.commons.collections4.list.AbstractListDecorator.add.

Implements java.util.List.add.

Adds an element to a specific index in the list if it is not already present.

(Violation) The List interface makes the assumption that the element is always inserted. This may not happen with this implementation.

Parameters
index:int

the index to insert at

object:E

the object to add

Annotations
@Override
addAllback to summary
public boolean addAll(final Collection<? extends E> coll)

Overrides org.apache.commons.collections4.collection.AbstractCollectionDecorator.addAll.

Implements java.util.List.addAll, java.util.Collection.addAll.

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 List interface makes the assumption that the elements are always inserted. This may not happen with this implementation.

Parameters
coll:Collection<? extends E>

the collection to add in iterator order

Returns:boolean

true if this collection changed

Annotations
@Override
addAllback to summary
public boolean addAll(final int index, final Collection<? extends E> coll)

Overrides org.apache.commons.collections4.list.AbstractListDecorator.addAll.

Implements java.util.List.addAll.

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 List interface makes the assumption that the elements are always inserted. This may not happen with this implementation.

Parameters
index:int

the index to insert at

coll:Collection<? extends E>

the collection to add in iterator order

Returns:boolean

true if this collection changed

Annotations
@Override
asSetback to summary
public Set<E> asSet()

Gets an unmodifiable view as a Set.

Returns:Set<E>

an unmodifiable set view

clearback to summary
public void clear()

Overrides org.apache.commons.collections4.collection.AbstractCollectionDecorator.clear.

Implements java.util.List.clear, java.util.Collection.clear.

Doc from java.util.List.clear.

Removes all of the elements from this list (optional operation). The list will be empty after this call returns.

Annotations
@Override
containsback to summary
public boolean contains(final Object object)

Overrides org.apache.commons.collections4.collection.AbstractCollectionDecorator.contains.

Implements java.util.List.contains, java.util.Collection.contains.

Doc from java.util.List.contains.

Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that Objects.equals(o, e).

Parameters
object:Object

element whose presence in this list is to be tested

Returns:boolean

true if this list contains the specified element

Annotations
@Override
containsAllback to summary
public boolean containsAll(final Collection<?> coll)

Overrides org.apache.commons.collections4.collection.AbstractCollectionDecorator.containsAll.

Implements java.util.List.containsAll, java.util.Collection.containsAll.

Doc from java.util.List.containsAll.

Returns true if this list contains all of the elements of the specified collection.

Parameters
coll:Collection<?>

collection to be checked for containment in this list

Returns:boolean

true if this list contains all of the elements of the specified collection

Annotations
@Override
createSetBasedOnListback to summary
protected Set<E> createSetBasedOnList(final Set<E> set, final List<E> list)

Create a new Set with the same type as the provided set and populate it with all elements of list.

Parameters
set:Set<E>

the Set to be used as return type, must not be null

list:List<E>

the List to populate the Set

Returns:Set<E>

a new Set populated with all elements of the provided List

iteratorback to summary
public Iterator<E> iterator()

Overrides org.apache.commons.collections4.collection.AbstractCollectionDecorator.iterator.

Implements java.util.List.iterator, java.util.Collection.iterator.

Doc from java.util.List.iterator.

Returns an iterator over the elements in this list in proper sequence.

Returns:Iterator<E>

an iterator over the elements in this list in proper sequence

Annotations
@Override
listIteratorback to summary
public ListIterator<E> listIterator()

Overrides org.apache.commons.collections4.list.AbstractListDecorator.listIterator.

Implements java.util.List.listIterator.

Doc from java.util.List.listIterator.

Returns a list iterator over the elements in this list (in proper sequence).

Returns:ListIterator<E>

a list iterator over the elements in this list (in proper sequence)

Annotations
@Override
listIteratorback to summary
public ListIterator<E> listIterator(final int index)

Overrides org.apache.commons.collections4.list.AbstractListDecorator.listIterator.

Implements java.util.List.listIterator.

Doc from java.util.List.listIterator.

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 next. An initial call to previous would return the element with the specified index minus one.

Parameters
index:int

index of the first element to be returned from the list iterator (by a call to next)

Returns:ListIterator<E>

a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list

Annotations
@Override
removeback to summary
public boolean remove(final Object object)

Overrides org.apache.commons.collections4.collection.AbstractCollectionDecorator.remove.

Implements java.util.List.remove, java.util.Collection.remove.

Doc from java.util.List.remove.

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 i such that Objects.equals(o, get(i)) (if such an element exists). Returns true if this list contained the specified element (or equivalently, if this list changed as a result of the call).

Parameters
object:Object

element to be removed from this list, if present

Returns:boolean

true if this list contained the specified element

Annotations
@Override
removeback to summary
public E remove(final int index)

Overrides org.apache.commons.collections4.list.AbstractListDecorator.remove.

Implements java.util.List.remove.

Doc from java.util.List.remove.

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.

Parameters
index:int

the index of the element to be removed

Returns:E

the element previously at the specified position

Annotations
@Override
removeAllback to summary
public boolean removeAll(final Collection<?> coll)

Overrides org.apache.commons.collections4.collection.AbstractCollectionDecorator.removeAll.

Implements java.util.List.removeAll, java.util.Collection.removeAll.

Doc from java.util.List.removeAll.

Removes from this list all of its elements that are contained in the specified collection (optional operation).

Parameters
coll:Collection<?>

collection containing elements to be removed from this list

Returns:boolean

true if this list changed as a result of the call

Annotations
@Override
removeIfback to summary
public boolean removeIf(Predicate<? super E> filter)

Overrides org.apache.commons.collections4.collection.AbstractCollectionDecorator.removeIf.

Overrides default java.util.Collection.removeIf.

Doc from java.util.Collection.removeIf.

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.

Parameters
filter:Predicate<? super E>

a predicate which returns true for elements to be removed

Returns:boolean

true if any elements were removed

Annotations
@Override
Since
4.4
retainAllback to summary
public boolean retainAll(final Collection<?> coll)

Overrides org.apache.commons.collections4.collection.AbstractCollectionDecorator.retainAll.

Implements java.util.List.retainAll, java.util.Collection.retainAll.

Doc from java.util.List.retainAll.

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 coll. If it's not contained, it's removed from this list. As a consequence, it is advised to use a collection type for coll that provides a fast (e.g. O(1)) implementation of Collection#contains(Object).

Parameters
coll:Collection<?>

collection containing elements to be retained in this list

Returns:boolean

true if this list changed as a result of the call

Annotations
@Override
setback to summary
public E set(final int index, final E object)

Overrides org.apache.commons.collections4.list.AbstractListDecorator.set.

Implements java.util.List.set.

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.

Parameters
index:int

the index to insert at

object:E

the object to set

Returns:E

the previous object

Annotations
@Override
setUniqueListback to summary
public static <E> SetUniqueList<E> setUniqueList(final List<E> list)

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 HashSet is used for the set behaviour.

Parameters
<E>
the element type
list:List<E>

the list to decorate, must not be null

Returns:SetUniqueList<E>

a new SetUniqueList

Exceptions
NullPointerException:
if list is null
Since
4.0
subListback to summary
public List<E> subList(final int fromIndex, final int toIndex)

Overrides org.apache.commons.collections4.list.AbstractListDecorator.subList.

Implements java.util.List.subList.

Doc from java.util.List.subList.

Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned list is empty.) The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa. The returned list supports all of the optional list operations supported by this list.

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:

list.subList(from, to).clear();
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.

Parameters
fromIndex:int

low endpoint (inclusive) of the subList

toIndex:int

high endpoint (exclusive) of the subList

Returns:List<E>

a view of the specified range within this list

Annotations
@Override
org.apache.commons.collections4.list back to summary

pack-priv Class SetUniqueList.SetListIterator<E>

extends AbstractIteratorDecorator<E>
Class Inheritance

Inner class iterator.

Field Summary

Modifier and TypeField and Description
private E
private final Set<E>

Constructor Summary

AccessConstructor and Description
protected
SetListIterator(final Iterator<E> it, final Set<E> set)

Method Summary

Modifier and TypeMethod and Description
public E
next()

Overrides org.apache.commons.collections4.iterators.AbstractIteratorDecorator.next.

Implements java.util.Iterator.next.

Returns the next element in the iteration.
public void
remove()

Overrides org.apache.commons.collections4.iterators.AbstractUntypedIteratorDecorator.remove.

Overrides default java.util.Iterator.remove.

Removes from the underlying collection the last element returned by this iterator (optional operation).

Field Detail

lastback to summary
private E last
setback to summary
private final Set<E> set

Constructor Detail

SetListIteratorback to summary
protected SetListIterator(final Iterator<E> it, final Set<E> set)

Method Detail

nextback to summary
public E next()

Overrides org.apache.commons.collections4.iterators.AbstractIteratorDecorator.next.

Implements java.util.Iterator.next.

Doc from org.apache.commons.collections4.iterators.AbstractIteratorDecorator.next.

Returns the next element in the iteration.

Returns:E

Doc from java.util.Iterator.next.

the next element in the iteration

Annotations
@Override
removeback to summary
public void remove()

Overrides org.apache.commons.collections4.iterators.AbstractUntypedIteratorDecorator.remove.

Overrides default java.util.Iterator.remove.

Doc from java.util.Iterator.remove.

Removes from the underlying collection the last element returned by this iterator (optional operation). This method can be called only once per call to next.

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 forEachRemaining method.

Annotations
@Override
org.apache.commons.collections4.list back to summary

pack-priv Class SetUniqueList.SetListListIterator<E>

extends AbstractListIteratorDecorator<E>
Class Inheritance

Inner class iterator.

Field Summary

Modifier and TypeField and Description
private E
private final Set<E>

Constructor Summary

AccessConstructor and Description
protected
SetListListIterator(final ListIterator<E> it, final Set<E> set)

Method Summary

Modifier and TypeMethod and Description
public void
add(final E
the element to insert
object
)

Overrides org.apache.commons.collections4.iterators.AbstractListIteratorDecorator.add.

Implements java.util.ListIterator.add.

Inserts the specified element into the list (optional operation).
public E
next()

Overrides org.apache.commons.collections4.iterators.AbstractListIteratorDecorator.next.

Implements java.util.ListIterator.next.

Returns the next element in the list and advances the cursor position.
public E
previous()

Overrides org.apache.commons.collections4.iterators.AbstractListIteratorDecorator.previous.

Implements java.util.ListIterator.previous.

Returns the previous element in the list and moves the cursor position backwards.
public void
remove()

Overrides org.apache.commons.collections4.iterators.AbstractListIteratorDecorator.remove.

Implements java.util.ListIterator.remove.

Removes from the list the last element that was returned by next or previous (optional operation).
public void
set(final E
the element with which to replace the last element returned by next or previous
object
)

Overrides org.apache.commons.collections4.iterators.AbstractListIteratorDecorator.set.

Implements java.util.ListIterator.set.

Replaces the last element returned by next or previous with the specified element (optional operation).
Inherited from org.apache.commons.collections4.iterators.AbstractListIteratorDecorator:
getListIteratorhasNexthasPreviousnextIndexpreviousIndex

Field Detail

lastback to summary
private E last
setback to summary
private final Set<E> set

Constructor Detail

SetListListIteratorback to summary
protected SetListListIterator(final ListIterator<E> it, final Set<E> set)

Method Detail

addback to summary
public void add(final E object)

Overrides org.apache.commons.collections4.iterators.AbstractListIteratorDecorator.add.

Implements java.util.ListIterator.add.

Doc from org.apache.commons.collections4.iterators.AbstractListIteratorDecorator.add.

Inserts the specified element into the list (optional operation). The element is inserted immediately before the element that would be returned by next, if any, and after the element that would be returned by previous, if any. (If the list contains no elements, the new element becomes the sole element on the list.) The new element is inserted before the implicit cursor: a subsequent call to next would be unaffected, and a subsequent call to previous would return the new element. (This call increases by one the value that would be returned by a call to nextIndex or previousIndex.)

Parameters
object:E

Doc from java.util.ListIterator.add.

the element to insert

Annotations
@Override
nextback to summary
public E next()

Overrides org.apache.commons.collections4.iterators.AbstractListIteratorDecorator.next.

Implements java.util.ListIterator.next.

Doc from org.apache.commons.collections4.iterators.AbstractListIteratorDecorator.next.

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 to go back and forth. (Note that alternating calls to next and previous will return the same element repeatedly.)

Returns:E

Doc from java.util.ListIterator.next.

the next element in the list

Annotations
@Override
previousback to summary
public E previous()

Overrides org.apache.commons.collections4.iterators.AbstractListIteratorDecorator.previous.

Implements java.util.ListIterator.previous.

Doc from org.apache.commons.collections4.iterators.AbstractListIteratorDecorator.previous.

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 next to go back and forth. (Note that alternating calls to next and previous will return the same element repeatedly.)

Returns:E

Doc from java.util.ListIterator.previous.

the previous element in the list

Annotations
@Override
removeback to summary
public void remove()

Overrides org.apache.commons.collections4.iterators.AbstractListIteratorDecorator.remove.

Implements java.util.ListIterator.remove.

Doc from org.apache.commons.collections4.iterators.AbstractListIteratorDecorator.remove.

Removes from the list the last element that was returned by next or previous (optional operation). This call can only be made once per call to next or previous. It can be made only if add has not been called after the last call to next or previous.

Annotations
@Override
setback to summary
public void set(final E object)

Overrides org.apache.commons.collections4.iterators.AbstractListIteratorDecorator.set.

Implements java.util.ListIterator.set.

Doc from org.apache.commons.collections4.iterators.AbstractListIteratorDecorator.set.

Replaces the last element returned by next or previous with the specified element (optional operation). This call can be made only if neither remove nor add have been called after the last call to next or previous.

Parameters
object:E

Doc from java.util.ListIterator.set.

the element with which to replace the last element returned by next or previous

Annotations
@Override