You can use store the items in a ArrayList
and use a HashMap<ItemType, Integer>
to map from the element type to the number of elements in equivalence classes in the List
allowing access to the elements using the list and allowing you to test, if an item is contained by using
Integer i = map.get(object);
boolean contained = ( i != null ) && ( i > 0 );
Updating the map for adding an element:
map.merge(object, 1, Integer::sum);
removing an element:
map.computeIfPresent(object, (k, v) -> v > 1 ? v - 1 : null);
If an item is replaced in the list, you can handle as
map.merge(newValue, 1, Integer::sum);
map.computeIfPresent(oldValue, (k, v) -> v > 1 ? v - 1 : null);