Class OrderedHashSet<E>
java.lang.Object
de.tomatengames.util.data.OrderedHashSet<E>
- Type Parameters:
E- The type of elements in this set.
- All Implemented Interfaces:
Iterable<E>, Collection<E>, Set<E>
A set that maintains the insertion order of its elements. This class is a
wrapper around an
OrderedHashMap where each element is used as a key.
New elements are added to the end of the ordered set. Adding an element that is already present does nothing, its position is kept.
- Since:
- 1.8
-
Constructor Summary
ConstructorsConstructorDescriptionCreates an empty ordered hash set.OrderedHashSet(Collection<? extends E> elements) Creates an ordered hash set containing all elements from the specified collection. -
Method Summary
Modifier and TypeMethodDescriptionbooleanbooleanaddAll(Collection<? extends E> elements) voidclear()booleanbooleancontainsAll(Collection<?> elements) booleanReturns whether this OrderedHashSet is equal to the given other object.getFirst()Returns the first element added to this set, or throws aNoSuchElementExceptionif this set is empty.getLast()Returns the last element added to this set, or throws aNoSuchElementExceptionif this set is empty.inthashCode()Matching theequals(Object)method of OrderedHashSet, the hash code is similar to those ofLists as the order of the stored elements is relevant.booleanisEmpty()iterator()booleanbooleanremoveAll(Collection<?> elements) Removes the first element added to this set, or throws aNoSuchElementExceptionif this set is empty.Removes the last element added to this set, or throws aNoSuchElementExceptionif this set is empty.booleanretainAll(Collection<?> elements) intsize()longsizeLong()Returns the number of elements in this set as a long value.Object[]toArray()<T> T[]toArray(T[] a) toString()Methods inherited from interface Collection
parallelStream, removeIf, stream, toArrayMethods inherited from interface Set
spliterator
-
Constructor Details
-
OrderedHashSet
public OrderedHashSet()Creates an empty ordered hash set. -
OrderedHashSet
Creates an ordered hash set containing all elements from the specified collection. The elements are added in the order they appear in the collection.- Parameters:
elements- The collection of elements to add to this set.
-
-
Method Details
-
sizeLong
public long sizeLong()Returns the number of elements in this set as a long value. This method is useful when dealing with large sets that may exceed the maximum size representable by an integer.- Returns:
- The number of elements in this set.
-
size
-
add
-
contains
-
remove
-
clear
-
isEmpty
-
getFirst
Returns the first element added to this set, or throws aNoSuchElementExceptionif this set is empty.- Returns:
- The first element added to this set.
- Throws:
NoSuchElementException- If this set is empty.
-
getLast
Returns the last element added to this set, or throws aNoSuchElementExceptionif this set is empty.- Returns:
- The last element added to this set.
- Throws:
NoSuchElementException- If this set is empty.
-
removeFirst
Removes the first element added to this set, or throws aNoSuchElementExceptionif this set is empty. The removed element is returned as the result of this method.- Returns:
- The first element added to this set.
- Throws:
NoSuchElementException- If this set is empty.
-
removeLast
Removes the last element added to this set, or throws aNoSuchElementExceptionif this set is empty. The removed element is returned as the result of this method.- Returns:
- The last element added to this set.
- Throws:
NoSuchElementException- If this set is empty.
-
iterator
-
hashCode
public int hashCode()Matching theequals(Object)method of OrderedHashSet, the hash code is similar to those ofLists as the order of the stored elements is relevant. -
equals
Returns whether this OrderedHashSet is equal to the given other object. Another object is considered equal iff it is also an OrderedHashSet and contains equal elements at equal positions, similar toList.equals(Object)s. -
toString
-
addAll
-
containsAll
- Specified by:
containsAllin interfaceCollection<E>- Specified by:
containsAllin interfaceSet<E>
-
removeAll
-
retainAll
-
toArray
-
toArray
-