Reputation: 1369
I want to get all the values(multiple) of a particular key.But i m getting only one value?I dont know how to print all the values.Great help if someone correct the code..did not get any help from google search..
import java.util.*;
public class hashing
{
public static void main(String args[])
{
String[] ary=new String[4];
String key;
char[] chrary;
ary[0]=new String("abcdef");
ary[1]=new String("defabc");
ary[2]=new String("ghijkl");
ary[3]=new String("jklghi");
Hashtable<String, String> hasht = new Hashtable<String, String>();
for(int i=0;i<4;i++){
chrary=ary[i].toCharArray();
Arrays.sort(chrary);
key=new String(chrary);
hasht.put(key,ary[i]);
}
Enumeration iterator = hasht.elements();
while(iterator.hasMoreElements()) {
String temp = (String)iterator.nextElement();
System.out.println(temp);
}
}
}
PS:output is defabc jklghi.I want abcdef defabc ghijkl jklghi.
Upvotes: 1
Views: 3172
Reputation: 272277
Hashtables can only contain one value per key. To store multiple values, you should either
List<String>
or array) per key. Note that you'll have to initialise the collection prior to insertion of the first value corresponding to that keyNote that many MultiMap implementations exist. The Oracle docs provide a simple implementation too (see here, and search for MultiMap)
Upvotes: 8
Reputation: 5837
Actually, in your case, its not collision, its same key with same hashcode. In general Collision occurs only if two different keys generate same hashcode, This can occur due to a bad implementation of hashCode()
method.
Yes, java.util.HashMap
will handle hash collisions, If you look at the source code of HashMap
, it stores each value in a LinkedList
. That means, if two different keys with same hashcode comes in.. then both values will go into same bucket but as two different nodes in the linked list
.
Found this link online, which explain How hash map works in detail.
Upvotes: 1
Reputation: 328608
The way HashMaps work is that there is only one value for a given key. So if you call:
map.put(key, value1);
map.put(key, value2);
the second line will override the value corresponding to the key.
Regarding your comment about collision, it means something different. Internally, a HashMap stores the key/value pairs in buckets that are defined based on the hashcode of the key (hence the name: hashmap). In the (low probability if the hashcode function is good) case where two non-equal keys have the same hashcode, the implementation needs to make sure that querying the hashmap on one of those keys will return the correct value. That is where hash collision need to be handled.
Upvotes: 4
Reputation: 533500
Hashtable, like all Map keep only one value per key, the last value you set.
If you want to keep all the values, just print the original array.
String[] ary = "abcdef,defabc,ghijkl,jklghi".split(",");
System.out.println(Arrays.toString(ary));
prints
[abcdef, defabc, ghijkl, jklghi]
Upvotes: 0
Reputation: 39950
That's not what collision resolution is meant to do. Collision resolution lets you handle the case when two object with different keys would go into the same "bucket" in the hash map. How this resolution happens is an internal detail of the hash map implementation, not something that would be exposed to you.
Upvotes: 2
Reputation: 17930
You are only putting one String for each key:
hasht.put(key,ary[i]);
So if i=1 that means you put defabc
, why do you expect to get multiple values for same key?
Upvotes: 0
Reputation: 382150
Your Hashtable<String, String>
maps one string to one string. So put
replaces the value that was before linked to a specific key.
If you want multiple values, you can make a Hashtable<String, []String>
or a Hashtable<String, List<String>>
.
A cleaner solution would be to use Google's Multimap which allows to associate multiple values to one key :
A collection similar to a Map, but which may associate multiple values with a single key. If you call put(K, V) twice, with the same key but different values, the multimap contains mappings from the key to both values.
Upvotes: 0
Reputation: 933
if key is the same, the value will be updated. jvm will not put a new key/value for same keys...
Upvotes: 0