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:
Serializable
,Iterable
,Collection
,Set
public class ListOrderedSet extends AbstractSerializableSetDecorator implements Set
Decorates anotherSet
to 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 unmodifiableList
view of the set can be obtained viaasList()
.This class cannot implement the
List
interface 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
- Version:
- $Revision: 646777 $ $Date: 2008-04-10 14:33:15 +0200 (Thu, 10 Apr 2008) $
- Author:
- Stephen Colebourne, Henning P. Schmiedehausen
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected List
setOrder
Internal list to hold the sequence of objects-
Fields inherited from class org.apache.commons.collections.collection.AbstractCollectionDecorator
collection
-
-
Constructor Summary
Constructors Modifier Constructor Description ListOrderedSet()
Constructs a new emptyListOrderedSet
using aHashSet
and anArrayList
internally.protected
ListOrderedSet(Set set)
Constructor that wraps (not copies).protected
ListOrderedSet(Set set, List list)
Constructor that wraps (not copies) the Set and specifies the list to use.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(int index, Object object)
boolean
add(Object object)
boolean
addAll(int index, Collection coll)
boolean
addAll(Collection coll)
List
asList()
Gets an unmodifiable view of the order of the Set.void
clear()
static ListOrderedSet
decorate(List list)
Factory method to create an ordered set using the supplied list to retain order.static ListOrderedSet
decorate(Set set)
Factory method to create an ordered set.static ListOrderedSet
decorate(Set set, List list)
Factory method to create an ordered set specifying the list and set to use.Object
get(int index)
int
indexOf(Object object)
Iterator
iterator()
Object
remove(int index)
boolean
remove(Object object)
boolean
removeAll(Collection coll)
boolean
retainAll(Collection coll)
Object[]
toArray()
Object[]
toArray(Object[] a)
String
toString()
Uses the underlying List's toString so that order is achieved.-
Methods inherited from class org.apache.commons.collections.set.AbstractSetDecorator
getSet
-
Methods inherited from class org.apache.commons.collections.collection.AbstractCollectionDecorator
contains, containsAll, equals, getCollection, hashCode, isEmpty, size
-
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.Set
contains, containsAll, equals, hashCode, isEmpty, size, spliterator
-
-
-
-
Field Detail
-
setOrder
protected final List setOrder
Internal list to hold the sequence of objects
-
-
Constructor Detail
-
ListOrderedSet
public ListOrderedSet()
Constructs a new emptyListOrderedSet
using aHashSet
and anArrayList
internally.- Since:
- Commons Collections 3.1
-
ListOrderedSet
protected ListOrderedSet(Set set)
Constructor that wraps (not copies).- Parameters:
set
- the set to decorate, must not be null- Throws:
IllegalArgumentException
- if set is null
-
ListOrderedSet
protected ListOrderedSet(Set set, List list)
Constructor that wraps (not copies) the Set and specifies the list 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 Detail
-
decorate
public static ListOrderedSet decorate(Set set, 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:
IllegalArgumentException
- if set or list is nullIllegalArgumentException
- if either the set or list is not empty- Since:
- Commons Collections 3.1
-
decorate
public static ListOrderedSet decorate(Set set)
Factory method to create an ordered set.An
ArrayList
is used to retain order.- Parameters:
set
- the set to decorate, must not be null- Throws:
IllegalArgumentException
- if set is null
-
decorate
public static ListOrderedSet decorate(List list)
Factory method to create an ordered set using the supplied list to retain order.A
HashSet
is 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:
IllegalArgumentException
- if list is null
-
asList
public List asList()
Gets an unmodifiable view of the order of the Set.- Returns:
- an unmodifiable list view
-
clear
public void clear()
- Specified by:
clear
in interfaceCollection
- Specified by:
clear
in interfaceSet
- Overrides:
clear
in classAbstractCollectionDecorator
-
iterator
public Iterator iterator()
- Specified by:
iterator
in interfaceCollection
- Specified by:
iterator
in interfaceIterable
- Specified by:
iterator
in interfaceSet
- Overrides:
iterator
in classAbstractCollectionDecorator
-
add
public boolean add(Object object)
- Specified by:
add
in interfaceCollection
- Specified by:
add
in interfaceSet
- Overrides:
add
in classAbstractCollectionDecorator
-
addAll
public boolean addAll(Collection coll)
- Specified by:
addAll
in interfaceCollection
- Specified by:
addAll
in interfaceSet
- Overrides:
addAll
in classAbstractCollectionDecorator
-
remove
public boolean remove(Object object)
- Specified by:
remove
in interfaceCollection
- Specified by:
remove
in interfaceSet
- Overrides:
remove
in classAbstractCollectionDecorator
-
removeAll
public boolean removeAll(Collection coll)
- Specified by:
removeAll
in interfaceCollection
- Specified by:
removeAll
in interfaceSet
- Overrides:
removeAll
in classAbstractCollectionDecorator
-
retainAll
public boolean retainAll(Collection coll)
- Specified by:
retainAll
in interfaceCollection
- Specified by:
retainAll
in interfaceSet
- Overrides:
retainAll
in classAbstractCollectionDecorator
-
toArray
public Object[] toArray()
- Specified by:
toArray
in interfaceCollection
- Specified by:
toArray
in interfaceSet
- Overrides:
toArray
in classAbstractCollectionDecorator
-
toArray
public Object[] toArray(Object[] a)
- Specified by:
toArray
in interfaceCollection
- Specified by:
toArray
in interfaceSet
- Overrides:
toArray
in classAbstractCollectionDecorator
-
get
public Object get(int index)
-
indexOf
public int indexOf(Object object)
-
add
public void add(int index, Object object)
-
addAll
public boolean addAll(int index, Collection coll)
-
remove
public Object remove(int index)
-
toString
public 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:
toString
in classAbstractCollectionDecorator
-
-