Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Adding another String value to existing HashMap Key without overwriting?

Tags:

java

hashmap

I was wondering if someone would be able to help with regards to adding another String value to an existing key within a HashMap in Java?

I understand that you can add a Key-Value pair using the this.put("String", "String") method. However, it overwrites the existing value, whereas I would like multiple values stored and paired, with the same key?

Thanks for your help.

like image 816
jxc Avatar asked Apr 18 '15 12:04

jxc


People also ask

How do I add a key-value to a map?

Use the set() method to add a key/value pair to a Map , e.g. map. set('myKey', 'myValue') . The set() method adds or updates the element with the provided key and value and returns the Map object.

How do you change a value in a Hashtable in Java?

You can use computeIfPresent method and supply it a mapping function, which will be called to compute a new value based on existing one. For example, Map<String, Integer> words = new HashMap<>(); words. put("hello", 3); words.

Can a HashMap have multiple values for same key Java?

HashMap can be used to store key-value pairs. But sometimes you may want to store multiple values for the same key. For example: For Key A, you want to store - Apple, Aeroplane.


3 Answers

What are you hoping to achieve here?

A Map (the HashMap) in your case is a direct "mapping" from one "key" to another value.

E.g.

"foo" -> 123
"bar" -> 321
"far" -> 12345
"boo" -> 54321

This means that if you were to try:

myHashMap.get("foo");

It would return the value 123 (of course, the type of the value you return can be anything you want).

Of course, this also means that any changes you make to the value of the key, it overrides the original value you assigned it, just like changing the value of a variable will override the original one assigned.

Say:

myHashMap.put("foo", 42);

The old value of "foo" in the map would be replaced with 42. So it would become:

"foo" -> 42
"bar" -> 321
"far" -> 12345
"boo" -> 54321

However, if you need multiple String objects that are mapped from a single key, you could use a different object which can store multiple objects, such as an Array or a List (or even another HashMap if you wanted.

For example, if you were to be using ArrayLists, when you are assigning a value to the HashMap, (say it is called myHashMap), you would first check if the key has been used before, if it hasn't, then you create a new ArrayList with the value you want to add, if it has, then you just add the value to the list.

(Assume key and value have the values you want)

ArrayList<String> list;
if(myHashMap.containsKey(key)){
    // if the key has already been used,
    // we'll just grab the array list and add the value to it
    list = myHashMap.get(key);
    list.add(value);
} else {
    // if the key hasn't been used yet,
    // we'll create a new ArrayList<String> object, add the value
    // and put it in the array list with the new key
    list = new ArrayList<String>();
    list.add(value);
    myHashMap.put(key, list);
}
like image 92
SCB Avatar answered Sep 19 '22 06:09

SCB


You can do like this!

Map<String,List<String>> map = new HashMap<>();
.
.
if(map.containsKey(key)){
    map.get(key).add(value);
} else {
   List<String> list = new ArrayList<>();
   list.add(value);
   map.put(key, list);
}

Or you can do the same thing by one line code in Java 8 style .

map.computeIfAbsent(key, k ->new ArrayList<>()).add(value);
like image 29
Prashanth Terala Avatar answered Sep 17 '22 06:09

Prashanth Terala


Would you like a concatenation of the two strings?

map.put(key, val);
if (map.containsKey(key)) {
    map.put(key, map.get(key) + newVal);
}

Or would you like a list of all the values for that key?

HashMap<String,List<String>> map = new HashMap<String,List<String>>();
String key = "key";
String val = "val";
String newVal = "newVal";
List<String> list = new ArrayList<String>();
list.add(val);
map.put(key, list);
if (map.containsKey(key)) {
    map.get(key).add(newVal);
}
like image 31
EvenLisle Avatar answered Sep 18 '22 06:09

EvenLisle