Results 1 to 2 of 2
- 01-14-2012, 02:14 PM #1
Member
- Join Date
- Jan 2012
- Posts
- 3
- Rep Power
- 0
HashTable and Hash Map are not Showing duplicate values
i think hashtable can allow multiple values with same key. But how can i look at all the values with the same key. I have just far reached uptill here
in both the cases of the while loop it shows only the values 1, 2 , and 4. If it allows multiple values with the same key then it must show 3 and 4 for the same key which is 3. But it is not showing here. or i don't know how to print the values.
Hashtable<String,String> m = new Hashtable<String,String>();
m.put("1","1");
m.put("2","2");
m.put("3","3");
m.put("3","4");
Set set = m.keySet(); // get set-view of keys
Set set1 = m.entrySet(); //get set of all values in hash table
// get iterator
Iterator itr = set.iterator();
Iterator itr1 = set1.iterator();
String str;
double bal;
while(itr.hasNext())
{
str = (String) itr.next();
System.out.println(str + ": " +m.get(str));
}
while(itr1.hasNext())
{
Map.Entry e = (Map.Entry) itr1.next();
System.out.println(e.getValue());
}
but in both cases the output is
1 : 1
2 : 2
3 : 4
-
Re: HashTable and Hash Map are not Showing duplicate values
Please see your private messages for an important note.
As for your original question, you'll want to re-read the API as your assumptions are incorrect. The only way around this that I know is to have the HashTable's (or better the HashMap's) value be an ArrayList or other collection.
Similar Threads
-
Can 2-3 Trees contain duplicate values?
By 6thDAY in forum Advanced JavaReplies: 1Last Post: 05-14-2011, 03:47 AM -
Discard duplicate or repeating values in the xml.
By reach2sudhakar in forum XMLReplies: 10Last Post: 09-23-2010, 12:59 PM -
How to detect duplicate values in an Array?
By maz09 in forum New To JavaReplies: 1Last Post: 04-08-2010, 07:58 AM -
Hash Values ???
By MuslimCoder in forum New To JavaReplies: 6Last Post: 01-16-2009, 04:26 AM -
How to make a hashmap to allow duplicate values?
By Preethi in forum New To JavaReplies: 0Last Post: 02-08-2008, 12:35 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks