I want to store a value based on a key, composed of set of elements. Something like the example below. Of course I know that my pseudo-example wouldn't work, as the hash of the object will probably be it's address which will be unique for each new instance, regardless of it's contents.
// in this pseudo-example this is my SET http://algs4.cs.princeton.edu/35applications/SET.java.html
// but the idea is that values are unique
HashMap<SET<Integer>, String> map = new HashMap<>();
SET a = new SET();
a.add(1);
a.add(2);
a.add(5);
SET b = new SET();
b.add(5);
b.add(1);
b.add(2);
map.put(a, "w00t");
System.out.println(map.get(b)); // I would want to get "w00t" because my key is the same set of elements
Of course I can just sort and concatenate the SET values as a string, with a separator and use that in a HashMap<String, String>
structure but that just doesn't feel right. I'm quite new to Java programming so there might be an obvious solution that I'm missing.
keySet()
. Also it's aSet
and notSET