E
- the type of the elements in this setpublic class ListOrderedSet<E> extends AbstractSerializableSetDecorator<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.
Modifier | Constructor and Description |
---|---|
|
ListOrderedSet()
Constructs a new empty
ListOrderedSet using a
HashSet and an ArrayList internally. |
protected |
ListOrderedSet(Set<E> set)
Constructor that wraps (not copies).
|
protected |
ListOrderedSet(Set<E> set,
List<E> list)
Constructor that wraps (not copies) the Set and specifies the list to
use.
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(E object) |
void |
add(int index,
E object)
Inserts the specified element at the specified position if it is not yet
contained in this ordered set (optional operation).
|
boolean |
addAll(Collection<? extends E> coll) |
boolean |
addAll(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).
|
List<E> |
asList()
Gets an unmodifiable view of the order of the Set.
|
void |
clear() |
E |
get(int index)
Returns the element at the specified position in this ordered set.
|
int |
indexOf(Object object)
Returns the index of the first occurrence of the specified element in
ordered set.
|
OrderedIterator<E> |
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.
|
E |
remove(int index)
Removes the element at the specified position from the ordered set.
|
boolean |
remove(Object object) |
boolean |
removeAll(Collection<?> coll) |
boolean |
retainAll(Collection<?> coll) |
Object[] |
toArray() |
<T> T[] |
toArray(T[] a) |
String |
toString()
Uses the underlying List's toString so that order is achieved.
|
decorated, equals, hashCode
contains, containsAll, isEmpty, setCollection, size
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
contains, containsAll, isEmpty, size, spliterator
parallelStream, removeIf, stream
public ListOrderedSet()
ListOrderedSet
using a
HashSet
and an ArrayList
internally.protected ListOrderedSet(Set<E> set)
set
- the set to decorate, must not be nullIllegalArgumentException
- if set is nullprotected ListOrderedSet(Set<E> set, List<E> list)
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 nullNullPointerException
- if set or list is nullpublic static <E> ListOrderedSet<E> listOrderedSet(Set<E> set, List<E> list)
The list and set must both be empty.
E
- the element typeset
- the set to decorate, must be empty and not nulllist
- the list to decorate, must be empty and not nullNullPointerException
- if set or list is nullIllegalArgumentException
- if either the set or list is not emptypublic static <E> ListOrderedSet<E> listOrderedSet(Set<E> set)
An ArrayList
is used to retain order.
E
- the element typeset
- the set to decorate, must not be nullNullPointerException
- if set is nullpublic static <E> ListOrderedSet<E> listOrderedSet(List<E> list)
A HashSet
is used for the set behaviour.
NOTE: If the list contains duplicates, the duplicates are removed, altering the specified list.
E
- the element typelist
- the list to decorate, must not be nullNullPointerException
- if list is nullpublic List<E> asList()
public void clear()
clear
in interface Collection<E>
clear
in interface Set<E>
clear
in class AbstractCollectionDecorator<E>
public OrderedIterator<E> iterator()
public boolean add(E object)
add
in interface Collection<E>
add
in interface Set<E>
add
in class AbstractCollectionDecorator<E>
public boolean addAll(Collection<? extends E> coll)
addAll
in interface Collection<E>
addAll
in interface Set<E>
addAll
in class AbstractCollectionDecorator<E>
public boolean remove(Object object)
remove
in interface Collection<E>
remove
in interface Set<E>
remove
in class AbstractCollectionDecorator<E>
public boolean removeAll(Collection<?> coll)
removeAll
in interface Collection<E>
removeAll
in interface Set<E>
removeAll
in class AbstractCollectionDecorator<E>
public boolean retainAll(Collection<?> coll)
This 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 for coll
that provides
a fast (e.g. O(1)) implementation of Collection.contains(Object)
.
retainAll
in interface Collection<E>
retainAll
in interface Set<E>
retainAll
in class AbstractCollectionDecorator<E>
public Object[] toArray()
toArray
in interface Collection<E>
toArray
in interface Set<E>
toArray
in class AbstractCollectionDecorator<E>
public <T> T[] toArray(T[] a)
toArray
in interface Collection<E>
toArray
in interface Set<E>
toArray
in class AbstractCollectionDecorator<E>
public E get(int index)
index
- the position of the element in the ordered Set
.index
List.get(int)
public int indexOf(Object object)
object
- the element to search for-1
if
this ordered set does not contain this objectList.indexOf(Object)
public void add(int index, E object)
index
- the index at which the element is to be insertedobject
- the element to be insertedList.add(int, Object)
public boolean addAll(int index, Collection<? extends E> coll)
index
- the position to insert the elementscoll
- the collection containing the elements to be insertedtrue
if this ordered set changed as a result of the callList.addAll(int, Collection)
public E remove(int index)
index
- the index of the element to be removedList.remove(int)
public String toString()
toString
in class AbstractCollectionDecorator<E>
Copyright © 2001–2018 The Apache Software Foundation. All rights reserved.