Class ListOrderedSet
- java.lang.Object
-
- org.apache.commons.collections.collection.AbstractCollectionDecorator
-
- org.apache.commons.collections.set.AbstractSetDecorator
-
- org.apache.commons.collections.set.AbstractSerializableSetDecorator
-
- org.apache.commons.collections.set.ListOrderedSet
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Iterable,java.util.Collection,java.util.Set
public class ListOrderedSet extends AbstractSerializableSetDecorator implements java.util.Set
Decorates anotherSetto ensure that the order of addition is retained and used by the iterator.If an object is added to the set for a second time, it will remain in the original position in the iteration. The order can be observed from the set via the iterator or toArray methods.
The ListOrderedSet also has various useful direct methods. These include many from
List, such asget(int),remove(int)andindexOf(int). An unmodifiableListview of the set can be obtained viaasList().This class cannot implement the
Listinterface directly as various interface methods (notably equals/hashCode) are incompatable with a set.This class is Serializable from Commons Collections 3.1.
- Since:
- Commons Collections 3.0
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description ListOrderedSet()Constructs a new emptyListOrderedSetusing aHashSetand anArrayListinternally.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(int index, java.lang.Object object)booleanadd(java.lang.Object object)booleanaddAll(int index, java.util.Collection coll)booleanaddAll(java.util.Collection coll)java.util.ListasList()Gets an unmodifiable view of the order of the Set.voidclear()static ListOrderedSetdecorate(java.util.List list)Factory method to create an ordered set using the supplied list to retain order.static ListOrderedSetdecorate(java.util.Set set)Factory method to create an ordered set.static ListOrderedSetdecorate(java.util.Set set, java.util.List list)Factory method to create an ordered set specifying the list and set to use.java.lang.Objectget(int index)intindexOf(java.lang.Object object)java.util.Iteratoriterator()java.lang.Objectremove(int index)booleanremove(java.lang.Object object)booleanremoveAll(java.util.Collection coll)booleanretainAll(java.util.Collection coll)java.lang.Object[]toArray()java.lang.Object[]toArray(java.lang.Object[] a)java.lang.StringtoString()Uses the underlying List's toString so that order is achieved.-
Methods inherited from class org.apache.commons.collections.collection.AbstractCollectionDecorator
contains, containsAll, equals, hashCode, isEmpty, size
-
-
-
-
Method Detail
-
decorate
public static ListOrderedSet decorate(java.util.Set set, java.util.List list)
Factory method to create an ordered set specifying the list and set to use.The list and set must both be empty.
- Parameters:
set- the set to decorate, must be empty and not nulllist- the list to decorate, must be empty and not null- Throws:
java.lang.IllegalArgumentException- if set or list is nulljava.lang.IllegalArgumentException- if either the set or list is not empty- Since:
- Commons Collections 3.1
-
decorate
public static ListOrderedSet decorate(java.util.Set set)
Factory method to create an ordered set.An
ArrayListis used to retain order.- Parameters:
set- the set to decorate, must not be null- Throws:
java.lang.IllegalArgumentException- if set is null
-
decorate
public static ListOrderedSet decorate(java.util.List list)
Factory method to create an ordered set using the supplied list to retain order.A
HashSetis used for the set behaviour.NOTE: If the list contains duplicates, the duplicates are removed, altering the specified list.
- Parameters:
list- the list to decorate, must not be null- Throws:
java.lang.IllegalArgumentException- if list is null
-
asList
public java.util.List asList()
Gets an unmodifiable view of the order of the Set.- Returns:
- an unmodifiable list view
-
clear
public void clear()
- Specified by:
clearin interfacejava.util.Collection- Specified by:
clearin interfacejava.util.Set- Overrides:
clearin classAbstractCollectionDecorator
-
iterator
public java.util.Iterator iterator()
- Specified by:
iteratorin interfacejava.util.Collection- Specified by:
iteratorin interfacejava.lang.Iterable- Specified by:
iteratorin interfacejava.util.Set- Overrides:
iteratorin classAbstractCollectionDecorator
-
add
public boolean add(java.lang.Object object)
- Specified by:
addin interfacejava.util.Collection- Specified by:
addin interfacejava.util.Set- Overrides:
addin classAbstractCollectionDecorator
-
addAll
public boolean addAll(java.util.Collection coll)
- Specified by:
addAllin interfacejava.util.Collection- Specified by:
addAllin interfacejava.util.Set- Overrides:
addAllin classAbstractCollectionDecorator
-
remove
public boolean remove(java.lang.Object object)
- Specified by:
removein interfacejava.util.Collection- Specified by:
removein interfacejava.util.Set- Overrides:
removein classAbstractCollectionDecorator
-
removeAll
public boolean removeAll(java.util.Collection coll)
- Specified by:
removeAllin interfacejava.util.Collection- Specified by:
removeAllin interfacejava.util.Set- Overrides:
removeAllin classAbstractCollectionDecorator
-
retainAll
public boolean retainAll(java.util.Collection coll)
- Specified by:
retainAllin interfacejava.util.Collection- Specified by:
retainAllin interfacejava.util.Set- Overrides:
retainAllin classAbstractCollectionDecorator
-
toArray
public java.lang.Object[] toArray()
- Specified by:
toArrayin interfacejava.util.Collection- Specified by:
toArrayin interfacejava.util.Set- Overrides:
toArrayin classAbstractCollectionDecorator
-
toArray
public java.lang.Object[] toArray(java.lang.Object[] a)
- Specified by:
toArrayin interfacejava.util.Collection- Specified by:
toArrayin interfacejava.util.Set- Overrides:
toArrayin classAbstractCollectionDecorator
-
get
public java.lang.Object get(int index)
-
indexOf
public int indexOf(java.lang.Object object)
-
add
public void add(int index, java.lang.Object object)
-
addAll
public boolean addAll(int index, java.util.Collection coll)
-
remove
public java.lang.Object remove(int index)
-
toString
public java.lang.String toString()
Uses the underlying List's toString so that order is achieved. This means that the decorated Set's toString is not used, so any custom toStrings will be ignored.- Overrides:
toStringin classAbstractCollectionDecorator
-
-