Class CountHashCollection<K>
- java.lang.Object
-
- dev.brachtendorf.datastructures.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 oncontains(java.lang.Object),add(K)andremove(java.lang.Object)allowing to keep track of duplicate values.Nullvalues are permitted.This collection specializes in remembering how often a equal value are added and removed. It is backed by a
HashMapwhose key is the added object and the value holds the count of the duplicate elements present in this collection. Aaddcall will increment the counter while a call toremovewill 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
removewill decrease the count of elements by one and not fully remove the node if there are still duplicates present in the collection. If desired callremoveFully(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 Summary
Constructors Constructor Description CountHashCollection()CountHashCollection(Collection<K> c)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanadd(K o)booleanaddAll(Collection<? extends K> c)voidclear()booleancontains(Object o)booleancontainsAll(Collection<?> c)intcontainsCount(K key)booleanisEmpty()Iterator<K>iterator()booleanremove(Object o)booleanremoveAll(Collection<?> c)booleanremoveFully(Object o)booleanretainAll(Collection<?> c)Retain all fully removes keys which are present in the collection and not just one instance of the duplicate!intsize()intsizeUnique()Returns the number of unique elements in this collection.Object[]toArray()Returns an array containing all of the elements in this collection.<T> T[]toArray(T[] a)Returns an array containing all of the elements in this collection.K[]toArrayUnique()K[]toArrayUnique(K[] array)StringtoString()-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.Collection
equals, hashCode, parallelStream, removeIf, spliterator, stream, toArray
-
-
-
-
Constructor Detail
-
CountHashCollection
public CountHashCollection()
-
CountHashCollection
public CountHashCollection(Collection<K> c)
-
-
Method Detail
-
add
public boolean add(K o)
- Specified by:
addin interfaceCollection<K>
-
contains
public boolean contains(Object o)
- Specified by:
containsin interfaceCollection<K>
-
containsCount
public int containsCount(K key)
-
remove
public boolean remove(Object o)
- Specified by:
removein interfaceCollection<K>
-
removeFully
public boolean removeFully(Object o)
-
size
public int size()
To retrieve the unique count of elements see
sizeUnique()- Specified by:
sizein interfaceCollection<K>- See Also:
sizeUnique()
-
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()
-
isEmpty
public boolean isEmpty()
- Specified by:
isEmptyin interfaceCollection<K>
-
toArrayUnique
public K[] toArrayUnique()
-
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 seetoArrayUnique().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:
toArrayin interfaceCollection<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 seetoArrayUnique().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:
toArrayin interfaceCollection<K>
-
containsAll
public boolean containsAll(Collection<?> c)
- Specified by:
containsAllin interfaceCollection<K>
-
addAll
public boolean addAll(Collection<? extends K> c)
- Specified by:
addAllin interfaceCollection<K>
-
removeAll
public boolean removeAll(Collection<?> c)
- Specified by:
removeAllin interfaceCollection<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:
retainAllin interfaceCollection<K>
-
clear
public void clear()
- Specified by:
clearin interfaceCollection<K>
-
-