I have a HashMap:
K1, false
K2, false
K3, false
...
K1000, false
Initially all the values of keys are false.
I have an ArrayList:
K3, K10, K33,..., K417, K834, K998
I need to get the values of the list and mark just those values as "true" in the Hashmap for the corresponding keys. Now I know we can do this:
Iterate HashMap:
Iterate List:
if HashMap.contains(List[i])
HashMap.put(List[i], true)
But we are using 2 iterations in this logic and maybe for 1000 keys it is not much of a problem, but if I want to scale it to a million keys, is there any efficient algorithm I can use to achieve above?
Thanks.
Iterate HashMap:.true.for (Object k : list) map.put(k, true)Setfrom theArrayList's contents; then anything that's not in the set isfalse. Two reasons to keep theMapinstead: 1. Some other interface requires aMap; 2. You need to throw an exception for unexpected keys like"KMFDM"instead of just returningfalse.