Class ListOrderedSet<E>
- Type Parameters:
- E- the type of the elements in this set
- All Implemented Interfaces:
- Serializable,- Iterable<E>,- Collection<E>,- Set<E>
Set 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 as get(int),
 remove(int) and indexOf(int). An unmodifiable
 List view of the set can be obtained via asList().
 
 This class cannot implement the List interface directly as
 various interface methods (notably equals/hashCode) are incompatible with a
 set.
 
This class is Serializable from Commons Collections 3.1.
- Since:
- 3.0
- See Also:
- 
Constructor SummaryConstructorsModifierConstructorDescriptionConstructs a new emptyListOrderedSetusing aHashSetand anArrayListinternally.protectedListOrderedSet(Set<E> set) Constructor that wraps (not copies).protectedListOrderedSet(Set<E> set, List<E> list) Constructor that wraps (not copies) the Set and specifies the list to use.
- 
Method SummaryModifier and TypeMethodDescriptionvoidInserts the specified element at the specified position if it is not yet contained in this ordered set (optional operation).booleanbooleanaddAll(int index, Collection<? extends E> coll) Inserts all elements in the specified collection not yet contained in the ordered set at the specified position (optional operation).booleanaddAll(Collection<? extends E> coll) asList()Gets an unmodifiable view of the order of the Set.voidclear()get(int index) Gets the element at the specified position in this ordered set.intReturns the index of the first occurrence of the specified element in ordered set.iterator()static <E> ListOrderedSet<E> listOrderedSet(List<E> list) Factory method to create an ordered set using the supplied list to retain order.static <E> ListOrderedSet<E> listOrderedSet(Set<E> set) Factory method to create an ordered set.static <E> ListOrderedSet<E> listOrderedSet(Set<E> set, List<E> list) Factory method to create an ordered set specifying the list and set to use.remove(int index) Removes the element at the specified position from the ordered set.booleanbooleanremoveAll(Collection<?> coll) booleanbooleanretainAll(Collection<?> coll) Object[]toArray()<T> T[]toArray(T[] a) toString()Uses the underlying List's toString so that order is achieved.Methods inherited from class org.apache.commons.collections4.set.AbstractSetDecoratordecorated, equals, hashCodeMethods inherited from class org.apache.commons.collections4.collection.AbstractCollectionDecoratorcontains, containsAll, isEmpty, setCollection, sizeMethods inherited from class java.lang.Objectclone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.CollectionparallelStream, streamMethods inherited from interface java.util.Setcontains, containsAll, isEmpty, size, spliterator
- 
Constructor Details- 
ListOrderedSetpublic ListOrderedSet()Constructs a new emptyListOrderedSetusing aHashSetand anArrayListinternally.- Since:
- 3.1
 
- 
ListOrderedSetConstructor that wraps (not copies).- Parameters:
- set- the set to decorate, must not be null
- Throws:
- NullPointerException- if set is null
 
- 
ListOrderedSetConstructor that wraps (not copies) the Set and specifies the list to use.The set and list must both be correctly initialized to the same elements. - Parameters:
- set- the set to decorate, must not be null
- list- the list to decorate, must not be null
- Throws:
- NullPointerException- if set or list is null
 
 
- 
- 
Method Details- 
listOrderedSetFactory method to create an ordered set using the supplied list to retain order.A HashSetis used for the set behavior.NOTE: If the list contains duplicates, the duplicates are removed, altering the specified list. - Type Parameters:
- E- the element type
- Parameters:
- list- the list to decorate, must not be null
- Returns:
- a new ordered set
- Throws:
- NullPointerException- if list is null
- Since:
- 4.0
 
- 
listOrderedSetFactory method to create an ordered set.An ArrayListis used to retain order.- Type Parameters:
- E- the element type
- Parameters:
- set- the set to decorate, must not be null
- Returns:
- a new ordered set
- Throws:
- NullPointerException- if set is null
- Since:
- 4.0
 
- 
listOrderedSetFactory method to create an ordered set specifying the list and set to use.The list and set must both be empty. - Type Parameters:
- E- the element type
- Parameters:
- set- the set to decorate, must be empty and not null
- list- the list to decorate, must be empty and not null
- Returns:
- a new ordered set
- Throws:
- NullPointerException- if set or list is null
- IllegalArgumentException- if either the set or list is not empty
- Since:
- 4.0
 
- 
add
- 
addInserts the specified element at the specified position if it is not yet contained in this ordered set (optional operation). Shifts the element currently at this position and any subsequent elements to the right.- Parameters:
- index- the index at which the element is to be inserted
- object- the element to be inserted
- See Also:
 
- 
addAll- Specified by:
- addAllin interface- Collection<E>
- Specified by:
- addAllin interface- Set<E>
- Overrides:
- addAllin class- AbstractCollectionDecorator<E>
 
- 
addAllInserts all elements in the specified collection not yet contained in the ordered set at the specified position (optional operation). Shifts the element currently at the position and all subsequent elements to the right.- Parameters:
- index- the position to insert the elements
- coll- the collection containing the elements to be inserted
- Returns:
- trueif this ordered set changed as a result of the call
- See Also:
 
- 
asList
- 
clear- Specified by:
- clearin interface- Collection<E>
- Specified by:
- clearin interface- Set<E>
- Overrides:
- clearin class- AbstractCollectionDecorator<E>
 
- 
get
- 
indexOf
- 
iterator
- 
remove
- 
remove
- 
removeAll- Specified by:
- removeAllin interface- Collection<E>
- Specified by:
- removeAllin interface- Set<E>
- Overrides:
- removeAllin class- AbstractCollectionDecorator<E>
 
- 
removeIf- Specified by:
- removeIfin interface- Collection<E>
- Overrides:
- removeIfin class- AbstractCollectionDecorator<E>
- Since:
- 4.4
 
- 
retainAllThis implementation iterates over the elements of this set, checking each element in turn to see if it's contained in coll. If it's not contained, it's removed from this set. As a consequence, it is advised to use a collection type forcollthat provides a fast (for example O(1)) implementation ofCollection.contains(Object).- Specified by:
- retainAllin interface- Collection<E>
- Specified by:
- retainAllin interface- Set<E>
- Overrides:
- retainAllin class- AbstractCollectionDecorator<E>
 
- 
toArray
- 
toArray- Specified by:
- toArrayin interface- Collection<E>
- Specified by:
- toArrayin interface- Set<E>
- Overrides:
- toArrayin class- AbstractCollectionDecorator<E>
 
- 
toStringUses 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 class- AbstractCollectionDecorator<E>
- Returns:
- a string representation of the ordered set
 
 
-