简体   繁体   中英

Adding a value to a list to an already existing key in Map

Evening!

I have the following Map:

HashMap<String, ArrayList> myMap = new HashMap<String, ArrayList>();

I then added the following data to it:

ArrayList myList = new ArrayList();
myList.add("Test 1");
myList.add("Test 2");
myList.add("Test 3");
myMap.put("Tests", myList);

This left me with the following data:

Key: Tests


Values: Test 1, Test 2, Test 3

My question is, how do I then add new values on to my already existing key? So for example, how could I add the value "Test 4" onto my key "Tests".

Thanks.

I'd recommend using Map#computeIfAbsent , to always ensure retrieving a List from the map:

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

private List<String> getList(String key) {
    return this.example.computeIfAbsent(key, k -> new ArrayList<>());
}

//elsewheres
getList("test").add("foobar");
getList("test").forEach(System.out::println); // "foobar"

This means that if the map doesn't contain an entry for the key , it will use the provided lambda to generate a new value for the key and return that.

Simply get the list from the map and then add the element to the list:

ArrayList list = myMap.get("Tests");
list.add("Test4");

There are some other things that can be remarked about your code. First of all, don't use the raw type ArrayList . Use generics:

HashMap<String, ArrayList<String>> myMap = new HashMap<String, ArrayList<String>>();

ArrayList<String> myList = new ArrayList<String>();
myList.add("Test 1");
myList.add("Test 2");
myList.add("Test 3");
myMap.put("Tests", myList);

Second, program to interfaces, not implementations. In other words, program using interfaces Map and List rather than the implementations HashMap and ArrayList . This is a well-known OO programming principle, which makes it for example easier to switch to a different implementation, if necessary.

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

List<String> myList = new ArrayList<String>();
myList.add("Test 1");
myList.add("Test 2");
myList.add("Test 3");
myMap.put("Tests", myList);

Finally, a syntax tip: if you're using Java 7 or newer you can use <> and you don't have to repeat the type arguments:

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

List<String> myList = new ArrayList<>();
myList.add("Test 1");
myList.add("Test 2");
myList.add("Test 3");
myMap.put("Tests", myList);

myMap.get("Tests").add("Test 4");
import java.util.*;

class M {
    public static void main( String ... args ) {
        List<String> l = new ArrayList<>();
        l.add("Test 1");
        l.add("Test 2");
        l.add("Test 3");

        Map<String,List<String>> m = new HashMap<>();
        m.put("Tests", l );

        // some time later that day ... 
        m.computeIfAbsent("Tests", k -> new ArrayList<>()).add("Test 4");
        System.out.println(m);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM