public class SetUniqueList<E> extends AbstractSerializableListDecorator<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.
Modifier and Type | Method and Description |
---|---|
boolean |
add(E object)
Adds an element to the list if it is not already present.
|
void |
add(int index,
E object)
Adds an element to a specific index in the list if it is not already
present.
|
boolean |
addAll(java.util.Collection<? extends E> coll)
Adds a collection of objects to the end of the list avoiding duplicates.
|
boolean |
addAll(int index,
java.util.Collection<? extends E> coll)
Adds a collection of objects a specific index in the list avoiding
duplicates.
|
java.util.Set<E> |
asSet()
Gets an unmodifiable view as a Set.
|
void |
clear() |
boolean |
contains(java.lang.Object object) |
boolean |
containsAll(java.util.Collection<?> coll) |
java.util.Iterator<E> |
iterator() |
java.util.ListIterator<E> |
listIterator() |
java.util.ListIterator<E> |
listIterator(int index) |
E |
remove(int index) |
boolean |
remove(java.lang.Object object) |
boolean |
removeAll(java.util.Collection<?> coll) |
boolean |
retainAll(java.util.Collection<?> coll) |
E |
set(int index,
E object)
Sets the value at the specified index avoiding duplicates.
|
static <E> SetUniqueList<E> |
setUniqueList(java.util.List<E> list)
Factory method to create a SetList using the supplied list to retain order.
|
java.util.List<E> |
subList(int fromIndex,
int toIndex) |
equals, get, hashCode, indexOf, lastIndexOf
isEmpty, size, toArray, toArray, toString
public static <E> SetUniqueList<E> setUniqueList(java.util.List<E> list)
If the list contains duplicates, these are removed (first indexed one
kept). A HashSet
is used for the set behaviour.
E
- the element typelist
- the list to decorate, must not be nullSetUniqueList
java.lang.NullPointerException
- if list is nullpublic java.util.Set<E> asSet()
public boolean add(E 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.Collection<E>
add
in interface java.util.List<E>
add
in class AbstractCollectionDecorator<E>
object
- the object to addpublic void add(int index, E 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.List<E>
add
in class AbstractListDecorator<E>
index
- the index to insert atobject
- the object to addpublic boolean addAll(java.util.Collection<? extends E> 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.Collection<E>
addAll
in interface java.util.List<E>
addAll
in class AbstractCollectionDecorator<E>
coll
- the collection to add in iterator orderpublic boolean addAll(int index, java.util.Collection<? extends E> 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.List<E>
addAll
in class AbstractListDecorator<E>
index
- the index to insert atcoll
- the collection to add in iterator orderpublic E set(int index, E 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.List<E>
set
in class AbstractListDecorator<E>
index
- the index to insert atobject
- the object to setpublic boolean remove(java.lang.Object object)
remove
in interface java.util.Collection<E>
remove
in interface java.util.List<E>
remove
in class AbstractCollectionDecorator<E>
public E remove(int index)
remove
in interface java.util.List<E>
remove
in class AbstractListDecorator<E>
public boolean removeAll(java.util.Collection<?> coll)
removeAll
in interface java.util.Collection<E>
removeAll
in interface java.util.List<E>
removeAll
in class AbstractCollectionDecorator<E>
public boolean retainAll(java.util.Collection<?> coll)
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)
.
retainAll
in interface java.util.Collection<E>
retainAll
in interface java.util.List<E>
retainAll
in class AbstractCollectionDecorator<E>
public void clear()
clear
in interface java.util.Collection<E>
clear
in interface java.util.List<E>
clear
in class AbstractCollectionDecorator<E>
public boolean contains(java.lang.Object object)
contains
in interface java.util.Collection<E>
contains
in interface java.util.List<E>
contains
in class AbstractCollectionDecorator<E>
public boolean containsAll(java.util.Collection<?> coll)
containsAll
in interface java.util.Collection<E>
containsAll
in interface java.util.List<E>
containsAll
in class AbstractCollectionDecorator<E>
public java.util.Iterator<E> iterator()
public java.util.ListIterator<E> listIterator()
listIterator
in interface java.util.List<E>
listIterator
in class AbstractListDecorator<E>
public java.util.ListIterator<E> listIterator(int index)
listIterator
in interface java.util.List<E>
listIterator
in class AbstractListDecorator<E>
public java.util.List<E> subList(int fromIndex, int toIndex)
NOTE: from 4.0, an unmodifiable list will be returned, as changes to the subList can invalidate the parent list.
subList
in interface java.util.List<E>
subList
in class AbstractListDecorator<E>
Copyright © 2010 - 2020 Adobe. All Rights Reserved