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>

public class InconsistentSortedSet<E> extends Object implements 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 Details

    • InconsistentSortedSet

      public InconsistentSortedSet()
      Constructs a new, empty set; the backing HashSet and ArrayList instances have the initial capacity specified by their respective implementation.
    • InconsistentSortedSet

      public InconsistentSortedSet(Collection<? extends E> c)
      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 is null
    • InconsistentSortedSet

      public InconsistentSortedSet(int initialCapacity)
      Constructs a new, empty set; the backing HashSet and ArrayList instances have the specified initial capacity.
      Parameters:
      initialCapacity - the initial capacity of the hash set and array list
    • InconsistentSortedSet

      public InconsistentSortedSet(Comparator<? super E> comparator)
      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 a ClassCastException for any elements k1 and k2 in the set. If the user attempts to put an element into the set that violates this constraint, the put(Object key, Object value) call will throw a ClassCastException.
      Parameters:
      comparator - the comparator that will be used to order this set. If null, the natural ordering of the keys will be used.
  • Method Details