public class SetUniqueList extends AbstractSerializableListDecorator
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 | Method and Description |
|---|---|
void |
add(int index,
java.lang.Object object)
Adds an element to a specific index in the list if it is not already present.
|
boolean |
add(java.lang.Object object)
Adds an element to the list if it is not already present.
|
boolean |
addAll(java.util.Collection coll)
Adds an element to the end of the list if it is not already present.
|
boolean |
addAll(int index,
java.util.Collection coll)
Adds a collection of objects a specific index in the list avoiding
duplicates.
|
java.util.Set |
asSet()
Gets an unmodifiable view as a Set.
|
void |
clear() |
boolean |
contains(java.lang.Object object) |
boolean |
containsAll(java.util.Collection coll) |
static SetUniqueList |
decorate(java.util.List list)
Factory method to create a SetList using the supplied list to retain order.
|
java.util.Iterator |
iterator() |
java.util.ListIterator |
listIterator() |
java.util.ListIterator |
listIterator(int index) |
java.lang.Object |
remove(int index) |
boolean |
remove(java.lang.Object object) |
boolean |
removeAll(java.util.Collection coll) |
boolean |
retainAll(java.util.Collection coll) |
java.lang.Object |
set(int index,
java.lang.Object object)
Sets the value at the specified index avoiding duplicates.
|
java.util.List |
subList(int fromIndex,
int toIndex) |
get, indexOf, lastIndexOfequals, hashCode, isEmpty, size, toArray, toArray, toStringpublic static SetUniqueList decorate(java.util.List list)
If the list contains duplicates, these are removed (first indexed one kept).
A HashSet is used for the set behaviour.
list - the list to decorate, must not be nulljava.lang.IllegalArgumentException - if list is nullpublic java.util.Set asSet()
public boolean add(java.lang.Object object)
(Violation)
The List interface requires that this method returns
true always. However this class may return false
because of the Set behaviour.
add in interface java.util.Collectionadd in interface java.util.Listadd in class AbstractCollectionDecoratorobject - the object to addpublic void add(int index,
java.lang.Object object)
(Violation)
The List interface makes the assumption that the element is
always inserted. This may not happen with this implementation.
add in interface java.util.Listadd in class AbstractListDecoratorindex - the index to insert atobject - the object to addpublic boolean addAll(java.util.Collection coll)
(Violation)
The List interface makes the assumption that the element is
always inserted. This may not happen with this implementation.
addAll in interface java.util.CollectionaddAll in interface java.util.ListaddAll in class AbstractCollectionDecoratorcoll - the collection to addpublic boolean addAll(int index,
java.util.Collection coll)
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.
addAll in interface java.util.ListaddAll in class AbstractListDecoratorindex - the index to insert atcoll - the collection to add in iterator orderpublic java.lang.Object set(int index,
java.lang.Object object)
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.
set in interface java.util.Listset in class AbstractListDecoratorindex - the index to insert atobject - the object to setpublic boolean remove(java.lang.Object object)
remove in interface java.util.Collectionremove in interface java.util.Listremove in class AbstractCollectionDecoratorpublic java.lang.Object remove(int index)
remove in interface java.util.Listremove in class AbstractListDecoratorpublic boolean removeAll(java.util.Collection coll)
removeAll in interface java.util.CollectionremoveAll in interface java.util.ListremoveAll in class AbstractCollectionDecoratorpublic boolean retainAll(java.util.Collection coll)
retainAll in interface java.util.CollectionretainAll in interface java.util.ListretainAll in class AbstractCollectionDecoratorpublic void clear()
clear in interface java.util.Collectionclear in interface java.util.Listclear in class AbstractCollectionDecoratorpublic boolean contains(java.lang.Object object)
contains in interface java.util.Collectioncontains in interface java.util.Listcontains in class AbstractCollectionDecoratorpublic boolean containsAll(java.util.Collection coll)
containsAll in interface java.util.CollectioncontainsAll in interface java.util.ListcontainsAll in class AbstractCollectionDecoratorpublic java.util.Iterator iterator()
iterator in interface java.lang.Iterableiterator in interface java.util.Collectioniterator in interface java.util.Listiterator in class AbstractCollectionDecoratorpublic java.util.ListIterator listIterator()
listIterator in interface java.util.ListlistIterator in class AbstractListDecoratorpublic java.util.ListIterator listIterator(int index)
listIterator in interface java.util.ListlistIterator in class AbstractListDecoratorpublic java.util.List subList(int fromIndex,
int toIndex)
subList in interface java.util.ListsubList in class AbstractListDecoratorCopyright © 2010 - 2023 Adobe. All Rights Reserved