I have set of values, an arraylist, and i have to find duplicate keys. One approach is to use 2 loops. and iterate through the list for each value resutling O(n2).
the other thing, That i can do is to put the values as keys in HashTable. I believed, that hashtable would throw an exception if there is already same key in it. But it is not throwing an exception
Hashtable<String, String> ht = new Hashtable<String, String>();
for (int i = 0; i<20; i++){
ht.put(String.valueOf(i%10), String.valueOf(i%10));
}
do i understand it wrong? Doesn’t hastable/hashmap throw exception if there is already same key in it?
My suggestion is you want a
HashSetinstead of aHashtable:If you don’t care about the values that you add to a map, you almost certainly want a
Setand not aMap/table.