-1

I have created a Hashmap which has multiple entries for a single Key entry. Now I want to retrieve all values individually. How can I do this?

I have checked many sites but couldn't get a clue on how to implement it, and an example code snippet can be useful for me.

HashMap<String,String>

  key       value
04302014  04302014
04302014  abc
04302014  10
04302014  20
04302014  20
05302014  05302014
05302014  def
05302014  10
05302014  20
05302014  20

Now using the key I want to retrieve all values individually and store in variables.

Like for key 04302014 I need to extract once and store in variables and in second iteration for the key 05302014.

Zoyd
  • 3,449
  • 1
  • 18
  • 27
Siva
  • 9,043
  • 12
  • 40
  • 63
  • Thanks for the edit... can anyone please provide me a wayout? – Siva May 01 '14 at 09:41
  • As Zoyd says in his answer, a `HashMap` only stores one value per key. As it stands, we can't answer your question. – SimonC May 01 '14 at 09:49
  • @SimonC thanks for your reply.. I will implement `Multimap` but my query is after implementing the multimap is it possible for me to retrive individual entries of the key value? – Siva May 01 '14 at 09:51
  • I'm not really sure what you're asking. Have you read the JavaDoc that Zoyd linked to? The get method returns all of the values associated with a key: https://guava-libraries.googlecode.com/svn/tags/release03/javadoc/com/google/common/collect/Multimap.html#get(K) – SimonC May 01 '14 at 09:54

2 Answers2

2

A Hashmap only has one value for each key, so this will not be possible. You would need a Multimap for this (as in Guava for example), or a Map to a List of values.

Zoyd
  • 3,449
  • 1
  • 18
  • 27
  • thanks for your reply.. will surely implement it and check but can you please let me know is it possible to retrive all the values of the key and store in varialbles? – Siva May 01 '14 at 09:42
0

any map (hashmap,linkedhashmap ,hashtable or treemap) in java can have only one unique key.

if you add same key again then it overrides the previous key.

so better to add values in a list or set and add that to map using single key.

same is demonstrated as below

Map<String, List<String>> map = new HashMap<String, List<String>>();

List<String> values1 = new ArrayList<String>();
List<String> values2 = new ArrayList<String>();

values1.add("10");
values1.add("20");

values2.add("30");
values2.add("40");

map.put("key1", values1);
map.put("key2", values2);

for (Map.Entry<String, List<String>> entry : map.entrySet()) {

    System.out.println(entry.getKey() + " values are ");
    System.out.println(entry.getValue());
}

in your case create list with the values 04302014,abc,10,20,20

another list with values def,10,20,20

add these two list with corresponding key key 04302014 for list1 and key 05302014 for list2

Karibasappa G C
  • 2,686
  • 1
  • 18
  • 27
  • thanks for your reply.. will try your solution and accept as answer. – Siva May 01 '14 at 10:11
  • @Siva, do you have a good reason not to use the Guava library's Multimap? It appears to do exactly what you want and will be significantly less error prone than implementing it yourself. – SimonC May 01 '14 at 10:21
  • @SimonC not a big reason but unable to add the Guava jars to the eclipse as it is a third party google jars.. please tell me a easy way to update the jars... downloaded from Guava but didn't get the jar... confused.. This is the site http://code.google.com/p/guava-libraries/ – Siva May 01 '14 at 10:23
  • You can download the jar from the front page: https://code.google.com/p/guava-libraries/. The jar for the current version, 17 is at: http://search.maven.org/remotecontent?filepath=com/google/guava/guava/17.0/guava-17.0.jar Then go to the build path for you project, and add this as an external library. See https://wiki.eclipse.org/FAQ_How_do_I_add_an_extra_library_to_my_project's_classpath%3F – SimonC May 01 '14 at 10:27
  • @SimonC Thanks for your assistance and I followed the way you suggested but when I add the Jar file still it is unable to recognize..Sorry for the reply simon.. now I am able to use the Multimap.. thanks. if you can add it as answer I will accept it.. Please add all your suggestions in answer – Siva May 01 '14 at 10:41