Package org.drasyl.util
Class InconsistentSortedSet<E>
java.lang.Object
org.drasyl.util.InconsistentSortedSet<E>
- Type Parameters:
E- the type of elements maintained by this set
- All Implemented Interfaces:
Iterable<E>,Collection<E>,Set<E>,SortedSet<E>
A
SortedSet where elements are ordered using their
natural ordering, or by a Comparator provided at set creation
time, depending on which constructor is used.
Unlike TreeSet, ordering does not have to be consistent with equals. In
other words, TreeSet implies that objects are equal if
Comparable.compareTo(Object) returns 0. In contrast,
InconsistentSortedSet uses Object.equals(Object) to determine equality.
Here an example which cannot be handled by TreeSet but from
InconsistentSortedSet:
Assume the DAO class Person with attributes
name and height. Now assume a data structure containing a set of Persons
where each name should be unique, and the elements should be sorted by height.
Internally, this class is backed by a HashSet and ArrayUtil, so the memory
consumption of this class is probably equal to the sum of these two classes.
-
Constructor Summary
ConstructorsConstructorDescriptionInconsistentSortedSet(int initialCapacity) InconsistentSortedSet(Collection<? extends E> c) Constructs a new set containing the elements in the specified collection.InconsistentSortedSet(Comparator<? super E> comparator) Constructs a new, empty set, ordered according to the given comparator. -
Method Summary
Modifier and TypeMethodDescriptionbooleanbooleanaddAll(Collection<? extends E> c) voidclear()Comparator<? super E>booleanbooleancontainsAll(Collection<?> c) first()booleanisEmpty()iterator()last()booleanbooleanremoveAll(Collection<?> c) booleanretainAll(Collection<?> c) intsize()Object[]toArray()<T> T[]toArray(T[] a) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArrayMethods inherited from interface java.util.SortedSet
spliterator
-
Constructor Details
-
InconsistentSortedSet
public InconsistentSortedSet() -
InconsistentSortedSet
Constructs a new set containing the elements in the specified collection.- Parameters:
c- the collection whose elements are to be placed into this set- Throws:
NullPointerException- if the specified collection isnull
-
InconsistentSortedSet
public InconsistentSortedSet(int initialCapacity) Constructs a new, empty set; the backingHashSetandArrayListinstances have the specified initial capacity.- Parameters:
initialCapacity- the initial capacity of the hash set and array list
-
InconsistentSortedSet
Constructs a new, empty set, ordered according to the given comparator. All keys inserted into the map must be mutually comparable by the given comparator:comparator.compare(k1, k2)must not throw aClassCastExceptionfor any elementsk1andk2in the set. If the user attempts to put an element into the set that violates this constraint, theput(Object key, Object value)call will throw aClassCastException.- Parameters:
comparator- the comparator that will be used to order this set. Ifnull, the natural ordering of the keys will be used.
-
-
Method Details
-
size
public int size() -
isEmpty
public boolean isEmpty() -
contains
-
iterator
-
toArray
-
toArray
public <T> T[] toArray(T[] a) -
add
-
remove
-
containsAll
- Specified by:
containsAllin interfaceCollection<E>- Specified by:
containsAllin interfaceSet<E>
-
addAll
-
retainAll
-
removeAll
-
clear
public void clear() -
comparator
- Specified by:
comparatorin interfaceSortedSet<E>
-
subSet
-
headSet
-
tailSet
-
first
-
last
-