Class SetUniqueList<E>
- All Implemented Interfaces:
Serializable
,Iterable<E>
,Collection<E>
,List<E>
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
- See Also:
-
Constructor Summary
ModifierConstructorDescriptionprotected
SetUniqueList
(List<E> list, Set<E> set) Constructor that wraps (not copies) the List and specifies the set to use. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds an element to a specific index in the list if it is not already present.boolean
Adds an element to the list if it is not already present.boolean
addAll
(int index, Collection<? extends E> coll) Adds a collection of objects a specific index in the list avoiding duplicates.boolean
addAll
(Collection<? extends E> coll) Adds a collection of objects to the end of the list avoiding duplicates.asSet()
Gets an unmodifiable view as a Set.void
clear()
boolean
boolean
containsAll
(Collection<?> coll) createSetBasedOnList
(Set<E> set, List<E> list) iterator()
listIterator
(int index) remove
(int index) boolean
boolean
removeAll
(Collection<?> coll) boolean
boolean
retainAll
(Collection<?> coll) Sets the value at the specified index avoiding duplicates.static <E> SetUniqueList<E>
setUniqueList
(List<E> list) Factory method to create a SetList using the supplied list to retain order.subList
(int fromIndex, int toIndex) Methods inherited from class org.apache.commons.collections4.list.AbstractListDecorator
decorated, equals, get, hashCode, indexOf, lastIndexOf
Methods inherited from class org.apache.commons.collections4.collection.AbstractCollectionDecorator
isEmpty, setCollection, size, toArray, toArray, toString
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Collection
parallelStream, stream
Methods inherited from interface java.util.List
isEmpty, replaceAll, size, sort, spliterator, toArray, toArray
-
Constructor Details
-
SetUniqueList
Constructor that wraps (not copies) the List and specifies the set to use.The set and list must both be correctly initialized to the same elements.
- Parameters:
set
- the set to decorate, must not be nulllist
- the list to decorate, must not be null- Throws:
NullPointerException
- if set or list is null
-
-
Method Details
-
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
HashSet
is used for the set behavior.- Type Parameters:
E
- the element type- Parameters:
list
- the list to decorate, must not be null- Returns:
- a new
SetUniqueList
- Throws:
NullPointerException
- if list is null- Since:
- 4.0
-
add
Adds an element to the list if it is not already present.(Violation) The
List
interface requires that this method returnstrue
always. However, this class may returnfalse
because of theSet
behavior.- Specified by:
add
in interfaceCollection<E>
- Specified by:
add
in interfaceList<E>
- Overrides:
add
in classAbstractCollectionDecorator<E>
- Parameters:
object
- the object to add- Returns:
- true if object was added
-
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. -
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.- Specified by:
addAll
in interfaceCollection<E>
- Specified by:
addAll
in interfaceList<E>
- Overrides:
addAll
in classAbstractCollectionDecorator<E>
- Parameters:
coll
- the collection to add in iterator order- Returns:
- true if this collection changed
-
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. -
asSet
Gets an unmodifiable view as a Set.- Returns:
- an unmodifiable set view
-
clear
- Specified by:
clear
in interfaceCollection<E>
- Specified by:
clear
in interfaceList<E>
- Overrides:
clear
in classAbstractCollectionDecorator<E>
-
contains
- Specified by:
contains
in interfaceCollection<E>
- Specified by:
contains
in interfaceList<E>
- Overrides:
contains
in classAbstractCollectionDecorator<E>
-
containsAll
- Specified by:
containsAll
in interfaceCollection<E>
- Specified by:
containsAll
in interfaceList<E>
- Overrides:
containsAll
in classAbstractCollectionDecorator<E>
-
createSetBasedOnList
-
iterator
-
listIterator
- Specified by:
listIterator
in interfaceList<E>
- Overrides:
listIterator
in classAbstractListDecorator<E>
-
listIterator
- Specified by:
listIterator
in interfaceList<E>
- Overrides:
listIterator
in classAbstractListDecorator<E>
-
remove
-
remove
- Specified by:
remove
in interfaceCollection<E>
- Specified by:
remove
in interfaceList<E>
- Overrides:
remove
in classAbstractCollectionDecorator<E>
-
removeAll
- Specified by:
removeAll
in interfaceCollection<E>
- Specified by:
removeAll
in interfaceList<E>
- Overrides:
removeAll
in classAbstractCollectionDecorator<E>
-
removeIf
- Specified by:
removeIf
in interfaceCollection<E>
- Overrides:
removeIf
in classAbstractCollectionDecorator<E>
- Since:
- 4.4
-
retainAll
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 forcoll
that provides a fast (e.g. O(1)) implementation ofCollection.contains(Object)
.- Specified by:
retainAll
in interfaceCollection<E>
- Specified by:
retainAll
in interfaceList<E>
- Overrides:
retainAll
in classAbstractCollectionDecorator<E>
-
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.
-
subList
NOTE: from 4.0, an unmodifiable list will be returned, as changes to the subList can invalidate the parent list.
-