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 Person
s
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
ConstructorDescriptionInconsistentSortedSet
(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 TypeMethodDescriptionboolean
boolean
addAll
(Collection<? extends E> c) void
clear()
Comparator<? super E>
boolean
boolean
containsAll
(Collection<?> c) first()
boolean
isEmpty()
iterator()
last()
boolean
boolean
removeAll
(Collection<?> c) boolean
retainAll
(Collection<?> c) int
size()
Object[]
toArray()
<T> T[]
toArray
(T[] a) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray
Methods 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 backingHashSet
andArrayList
instances 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 aClassCastException
for any elementsk1
andk2
in 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:
containsAll
in interfaceCollection<E>
- Specified by:
containsAll
in interfaceSet<E>
-
addAll
-
retainAll
-
removeAll
-
clear
public void clear() -
comparator
- Specified by:
comparator
in interfaceSortedSet<E>
-
subSet
-
headSet
-
tailSet
-
first
-
last
-