Class CountHashCollection<K>

  • Type Parameters:
    K - the type of elements in this collection
    All Implemented Interfaces:
    Iterable<K>, Collection<K>

    public class CountHashCollection<K>
    extends Object
    implements Collection<K>
    A hash collection offering a O(1) performance on contains(java.lang.Object), add(K) and remove(java.lang.Object) allowing to keep track of duplicate values. Null values are permitted.

    This collection specializes in remembering how often a equal value are added and removed. It is backed by a HashMap whose key is the added object and the value holds the count of the duplicate elements present in this collection. A add call will increment the counter while a call to remove will decrease it. If the count reaches 0 (add and remove was called the same amount of time) the key is fully removed.

    Objects are considered duplicates if their equals method returns true.

    Unlike regular a collection a call to remove will decrease the count of elements by one and not fully remove the node if there are still duplicates present in the collection. If desired call removeFully(Object) instead.

    NOTE: duplicates are detected based on equality. Due to the fact that the count is mapped to the first object inserted any methods returning values i.e toArray() or iterators , will return objects pointing to the same reference. Therefore the following scenario might happen:

      
     Object o;
     Object o1;
     
     CountHashCollection h;
     
     // Equality :  o.equals(o1) -> true
     // Reference: (o == o1)     -> false
     h.add(o);
     h.add(o1);
     
     h.size() = 2
     h.sizeUnique() = 1
     
     Iterator<Object> iter = h.iterator();
     
     Object o2 = iter.next();
     Object o3 = iter.next();
    
     o2.equals(o) -> True
     o2.equals(o1) -> True
     o2.equals(o3) -> True
     o3.equals(o) -> True
     o3.equals(o1) -> True
     
     o == o2
     o == o3
     
     BUT!
     o1 != o2
     o1 != o3
     
     
    Since:
    1.1.0 com.github.kilianB
    Author:
    Kilian
    • Constructor Detail

      • CountHashCollection

        public CountHashCollection()
      • CountHashCollection

        public CountHashCollection​(Collection<K> c)
    • Method Detail

      • add

        public boolean add​(K o)
        Specified by:
        add in interface Collection<K>
      • containsCount

        public int containsCount​(K key)
      • removeFully

        public boolean removeFully​(Object o)
      • sizeUnique

        public int sizeUnique()
        Returns the number of unique elements in this collection. If this collection contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.
        Returns:
        the number of unique elements
        See Also:
        size()
      • toArrayUnique

        public K[] toArrayUnique()
      • toArrayUnique

        public K[] toArrayUnique​(K[] array)
      • toArray

        public Object[] toArray()
        Returns an array containing all of the elements in this collection. Duplicate values will be present the amount of times they were added. If you are only interested in unique elements see toArrayUnique().

        Returned duplicate objects all reference the first object added the the collection

        No guarantee is given about the order of the elements, only that duplicate objects will be present next to each other. The returned array will be "safe" in that no references to it are maintained by this collection. (In other words, this method must allocate a new array even if this collection is backed by an array).The caller is thus free to modify the returned array.

        Specified by:
        toArray in interface Collection<K>
        Returns:
        an array, whose runtime component type is Object, containing all of the elements in this collection
      • toArray

        public <T> T[] toArray​(T[] a)
        Returns an array containing all of the elements in this collection. Duplicate values will be present the amount of times they were added. If you are only interested in unique elements see toArrayUnique().

        Returned duplicate objects all reference the first object added the the collection

        No guarantee is given about the order of the elements, only that duplicate objects will be present next to each other.

        If this collection fits in the specified array with room to spare(i.e., the array has more elements than this collection), the element in the array immediately following the end of the collection is set to null. The returned array will be "safe" in that no references to it are maintained by this collection. (In other words, this method must allocate a new array even if this collection is backed by an array).The caller is thus free to modify the returned array.

        Specified by:
        toArray in interface Collection<K>
      • retainAll

        public boolean retainAll​(Collection<?> c)
        Retain all fully removes keys which are present in the collection and not just one instance of the duplicate!

        Specified by:
        retainAll in interface Collection<K>