Class SetUniqueList
- All Implemented Interfaces:
Serializable
,Iterable
,Collection
,List
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:
- Commons Collections 3.0
- Version:
- $Revision: 1713299 $ $Date: 2015-11-08 22:35:53 +0100 (Sun, 08 Nov 2015) $
- Author:
- Matthew Hawthorne, Stephen Colebourne, Tom Dunham
- See Also:
-
Field Summary
FieldsFields inherited from class org.apache.commons.collections.collection.AbstractCollectionDecorator
collection
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
SetUniqueList
(List list, Set 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 coll) Adds a collection of objects a specific index in the list avoiding duplicates.boolean
addAll
(Collection coll) Adds an element to the end of the list if it is not already present.asSet()
Gets an unmodifiable view as a Set.void
clear()
boolean
boolean
containsAll
(Collection coll) protected Set
createSetBasedOnList
(Set set, List list) static SetUniqueList
Factory method to create a SetList using the supplied list to retain order.iterator()
listIterator
(int index) remove
(int index) boolean
boolean
removeAll
(Collection coll) boolean
retainAll
(Collection coll) Sets the value at the specified index avoiding duplicates.subList
(int fromIndex, int toIndex) Methods inherited from class org.apache.commons.collections.list.AbstractListDecorator
get, getList, indexOf, lastIndexOf
Methods inherited from class org.apache.commons.collections.collection.AbstractCollectionDecorator
equals, getCollection, hashCode, isEmpty, 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, removeIf, stream, toArray
Methods inherited from interface java.util.List
equals, hashCode, isEmpty, replaceAll, size, sort, spliterator, toArray, toArray
-
Field Details
-
set
Internal Set to maintain uniqueness.
-
-
Constructor Details
-
SetUniqueList
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:
set
- the set to decorate, must not be nulllist
- the list to decorate, must not be null- Throws:
IllegalArgumentException
- if set or list is null
-
-
Method Details
-
decorate
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:
list
- the list to decorate, must not be null- Throws:
IllegalArgumentException
- if list is null
-
asSet
Gets an unmodifiable view as a Set.- Returns:
- an unmodifiable set view
-
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
behaviour.- Specified by:
add
in interfaceCollection
- Specified by:
add
in interfaceList
- Overrides:
add
in classAbstractCollectionDecorator
- 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.- Specified by:
add
in interfaceList
- Overrides:
add
in classAbstractListDecorator
- Parameters:
index
- the index to insert atobject
- the object to add
-
addAll
Adds an element to the end of 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.- Specified by:
addAll
in interfaceCollection
- Specified by:
addAll
in interfaceList
- Overrides:
addAll
in classAbstractCollectionDecorator
- Parameters:
coll
- the collection to add
-
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.- Specified by:
addAll
in interfaceList
- Overrides:
addAll
in classAbstractListDecorator
- Parameters:
index
- the index to insert atcoll
- the collection to add in iterator order- Returns:
- true if this collection changed
-
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.
- Specified by:
set
in interfaceList
- Overrides:
set
in classAbstractListDecorator
- Parameters:
index
- the index to insert atobject
- the object to set- Returns:
- the previous object
-
remove
- Specified by:
remove
in interfaceCollection
- Specified by:
remove
in interfaceList
- Overrides:
remove
in classAbstractCollectionDecorator
-
remove
- Specified by:
remove
in interfaceList
- Overrides:
remove
in classAbstractListDecorator
-
removeAll
- Specified by:
removeAll
in interfaceCollection
- Specified by:
removeAll
in interfaceList
- Overrides:
removeAll
in classAbstractCollectionDecorator
-
retainAll
- Specified by:
retainAll
in interfaceCollection
- Specified by:
retainAll
in interfaceList
- Overrides:
retainAll
in classAbstractCollectionDecorator
-
clear
public void clear()- Specified by:
clear
in interfaceCollection
- Specified by:
clear
in interfaceList
- Overrides:
clear
in classAbstractCollectionDecorator
-
contains
- Specified by:
contains
in interfaceCollection
- Specified by:
contains
in interfaceList
- Overrides:
contains
in classAbstractCollectionDecorator
-
containsAll
- Specified by:
containsAll
in interfaceCollection
- Specified by:
containsAll
in interfaceList
- Overrides:
containsAll
in classAbstractCollectionDecorator
-
iterator
- Specified by:
iterator
in interfaceCollection
- Specified by:
iterator
in interfaceIterable
- Specified by:
iterator
in interfaceList
- Overrides:
iterator
in classAbstractCollectionDecorator
-
listIterator
- Specified by:
listIterator
in interfaceList
- Overrides:
listIterator
in classAbstractListDecorator
-
listIterator
- Specified by:
listIterator
in interfaceList
- Overrides:
listIterator
in classAbstractListDecorator
-
subList
- Specified by:
subList
in interfaceList
- Overrides:
subList
in classAbstractListDecorator
-
createSetBasedOnList
-