I have to store a close list for this I defined a Map -
Map<Node, Boolean> closeList = new HashMap<Node, Boolean>()
now for checking whether a node is exist in this map I use -
boolean binExists = closeList .containsKey(node)
seems that the value-boolean
of the map is unnecessary .
Have you any better idea for this check using HashMap mode (O(1)) ?
A HashSet seems to be exactly what you need.
Set<Node> closeSet = new HashSet<>();
Node n1 = new Node();
Node n2 = new Node();
closeSet.add(n1);
System.out.println(closeSet.contains(n1)); //true
System.out.println(closeSet.contains(n2)); //false - though depending upon equals/hashcode implementation of Node
Even though using a Set<Node>
looks better than using a Map<Node, Boolean>
, java.util.HashSet
is using a HashMap
internally in it's implementation. If you need an implementation that uses less memory, you could for instance have a look at this implementation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With