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 | Field and Description |
---|---|
protected Set |
set
Internal Set to maintain uniqueness.
|
collection
Modifier | Constructor and Description |
---|---|
protected |
SetUniqueList(List list,
Set set)
Constructor that wraps (not copies) the List and specifies the set to use.
|
Modifier and Type | Method and Description |
---|---|
void |
add(int index,
Object object)
Adds an element to a specific index in the list if it is not already present.
|
boolean |
add(Object object)
Adds an element to the list if it is not already present.
|
boolean |
addAll(Collection coll)
Adds an element to the end of 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.
|
Set |
asSet()
Gets an unmodifiable view as a Set.
|
void |
clear() |
boolean |
contains(Object object) |
boolean |
containsAll(Collection coll) |
protected Set |
createSetBasedOnList(Set set,
List list)
|
static SetUniqueList |
decorate(List list)
Factory method to create a SetList using the supplied list to retain order.
|
Iterator |
iterator() |
ListIterator |
listIterator() |
ListIterator |
listIterator(int index) |
Object |
remove(int index) |
boolean |
remove(Object object) |
boolean |
removeAll(Collection coll) |
boolean |
retainAll(Collection coll) |
Object |
set(int index,
Object object)
Sets the value at the specified index avoiding duplicates.
|
List |
subList(int fromIndex,
int toIndex) |
get, getList, indexOf, lastIndexOf
equals, getCollection, hashCode, isEmpty, size, toArray, toArray, toString
protected final Set set
protected SetUniqueList(List list, Set set)
The set and list must both be correctly initialised to the same elements.
set
- the set to decorate, must not be nulllist
- the list to decorate, must not be nullIllegalArgumentException
- if set or list is nullpublic static SetUniqueList decorate(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 nullIllegalArgumentException
- if list is nullpublic Set asSet()
public boolean add(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 Collection
add
in interface List
add
in class AbstractCollectionDecorator
object
- the object to addpublic void add(int index, 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 List
add
in class AbstractListDecorator
index
- the index to insert atobject
- the object to addpublic boolean addAll(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 Collection
addAll
in interface List
addAll
in class AbstractCollectionDecorator
coll
- the collection to addpublic boolean addAll(int index, 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 List
addAll
in class AbstractListDecorator
index
- the index to insert atcoll
- the collection to add in iterator orderpublic Object set(int index, 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 List
set
in class AbstractListDecorator
index
- the index to insert atobject
- the object to setpublic boolean remove(Object object)
remove
in interface Collection
remove
in interface List
remove
in class AbstractCollectionDecorator
public Object remove(int index)
remove
in interface List
remove
in class AbstractListDecorator
public boolean removeAll(Collection coll)
removeAll
in interface Collection
removeAll
in interface List
removeAll
in class AbstractCollectionDecorator
public boolean retainAll(Collection coll)
retainAll
in interface Collection
retainAll
in interface List
retainAll
in class AbstractCollectionDecorator
public void clear()
clear
in interface Collection
clear
in interface List
clear
in class AbstractCollectionDecorator
public boolean contains(Object object)
contains
in interface Collection
contains
in interface List
contains
in class AbstractCollectionDecorator
public boolean containsAll(Collection coll)
containsAll
in interface Collection
containsAll
in interface List
containsAll
in class AbstractCollectionDecorator
public Iterator iterator()
iterator
in interface Iterable
iterator
in interface Collection
iterator
in interface List
iterator
in class AbstractCollectionDecorator
public ListIterator listIterator()
listIterator
in interface List
listIterator
in class AbstractListDecorator
public ListIterator listIterator(int index)
listIterator
in interface List
listIterator
in class AbstractListDecorator
public List subList(int fromIndex, int toIndex)
subList
in interface List
subList
in class AbstractListDecorator
Copyright © 2001–2015 The Apache Software Foundation. All rights reserved.