-6
Map<String, String> map = new HashMap<String, String>();
map.put("1", "xyz");
map.put("1", "abc");
map.put("1", "cde");
map.put("2", "err");`

`

for the above map I want to get all the values associated with the key 1. Expected output.

Key:: 1 values are:: xyz, abc, cde

Order of the values doesn't important.

Vini.g.fer
  • 11,639
  • 16
  • 61
  • 90
ajit kumar
  • 43
  • 1
  • 1
  • 3
  • 5
    If you need to have duplciated key (don't even know a reason why you should), reconsider using another Collection. – araknoid Jul 25 '13 at 10:07
  • You probably want a multimap - http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/Multimap.html – Paul Cager Jul 25 '13 at 10:09
  • First of all you need to understand concepts of HashMap, its internal life. I encourage you to cover [my tutorial](http://volodial.blogspot.com/2013/07/internal-life-of-hashmap-in-java.html) where all these things explained ! – Volodymyr Levytskyi Jul 25 '13 at 11:07

11 Answers11

7

In a Map the key should always be unique. If you associate a new value to an existing key, it will overwrite the value of the existing entry.

You might need to check the interface for Map#put(K, V) method.

If the map previously contained a mapping for the key, the old value is replaced by the specified value.

So in your case your map will always have "cde" as the value for the key "1".

Jayamohan
  • 12,734
  • 2
  • 27
  • 41
6

Use MultiMap

    MultiMap mapValue = new MultiValueMap();

    mapValue.put("1", "xyz");
    mapValue.put("1", "abc");
    mapValue.put("1", "cde");
    mapValue.put("2", "err");
    System.out.println("Map : " + mapValue);

Output: Map : {2=[err], 1=[xyz, abc, cde]}

newuser
  • 8,338
  • 2
  • 25
  • 33
4

A map can not have duplicate keys.

If you want to implement what you describe in question. First you need to use multimaps

Mukul Goel
  • 8,387
  • 6
  • 37
  • 77
1

What you are doing is wrong.

Map doesn't allow duplicates.

So one key -----------> one value

If you see docs of put()

Associates the specified value with the specified key in this map (optional operation). If the map previously contained a mapping for the key, the old value is replaced by the specified value. (A map m is said to contain a mapping for a key k if and only if m.containsKey(k) would return true.)

You can print the values of each key and value like

Ex:

Map<String, String> map = new HashMap<String, String>();
for (Map.Entry<String, String> entry : map.entrySet()) {
    System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0

This is impossible, a map is called a map because it maps one key value to a value. Multiple keys can map to the same value but not the other way around.

What you probably want is a map which maps to a List<String> instead:

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

if (map.get("1") == null) {
  map.put("1", new ArrayList<String>());
}

map.get("1").add("xyz");

// ...

A helper function for adding might be convenient

public static <K, V> void add(final K key, final V value, final Map<K, List<V>> map)
{
    if (map.get(key) == null) {
      map.put(key, new ArrayList<V>());
    }

    map.get(key).add(value);
}
Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
0

In Map you can't have duplicate keys. so In your case final value put for key 1. "cde" will remain in Map

You can do some thing like following to achive what you are expecting

  Map<String, List<String>> map = new HashMap<>();
    List<String> list=new ArrayList<>();
    List<String> list1=new ArrayList<>();
    list.add("xyz");
    list.add("abc");
    list.add("cde");
    list1.add("err");
    map.put("1", list);
    map.put("2",list1);
    System.out.println(map.get("1"));
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
0

You can not do this with this type of Map. The key in map must be unique.

To be able to do that you should declare a map, where key is string but values are collections of Strings.

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

The to list values from it you can do this

for(String valueOfKey : map.get("key") {
   //print or something else 
}

Note that to add some values to it you must first check that key is already stored and if not then fist declare a collection.

if(map.contains("key") == false) {
  map.put(new ArrayList<String>());
}

map.get("key").add("value");

As this is well know design you might be interest in guava framework and Multimap

The benefit of this class is that it already has implemented the logic how to add and retrieve values from it.

0

HashMap::put overrides the old value associated with the key. You have to put a List in each map entry and insert new values in the appropriate list.

Chapeiro
  • 1
  • 1
  • 1
0

From the java documentation about HashMap.put(K key, V value) method:

Associates the specified value with the specified key in this map. If the map previously contained a mapping for the key, the old value is replaced.

So you can't do that.

BitExodus
  • 749
  • 6
  • 10
0

You could do something like:

for (String k : map.keySet())
    System.out.println(k);

This would print the keys in the HashMap, but without any guarantees on order.

CarManuel
  • 325
  • 3
  • 12
-1

You can not have duplicate key for a hash map see the below S.O for What happens for duplicate keys in HashMap

Community
  • 1
  • 1
Siva
  • 1,938
  • 1
  • 17
  • 36