vedran
vedran

Reputation: 2188

Java, How to add values to Array List used as value in HashMap

What I have is a HashMap<String, ArrayList<String>> called examList. What I want to use it for is to save grades of each course a person is attending. So key for this HashMap is couresID, and value is a ArrayList of all grades (exam attempts) this person has made.

The problem is I know how to work with array lists and hashmaps normally, but I'm not sure how to even begin with this example. So how would I, or example, add something to ArrayList inside HashMap?

Upvotes: 24

Views: 142162

Answers (8)

Diacrome
Diacrome

Reputation: 95

Java 8+ has Map.compute for such cases:

examList.compute(courseId, (id, grades) ->
        grades != null ? grades : new ArrayList<>())
    .add(value);

Upvotes: 5

sarath
sarath

Reputation: 123

HashMap<String, ArrayList<ObjectX>> objList = new HashMap<>();

if(objList.containsKey(key))
   objList.get(key).add(Object1);
else
   objList.put(key, new ArrayList<ObjectX>(Arrays.asList(Object1)));

Upvotes: 0

Rod Efraim
Rod Efraim

Reputation: 65

Can also do this in Kotlin without using any external libraries.

var hashMap : HashMap<String, MutableList<String>> = HashMap()

if(hashMap.get(id) == null){
        hashMap.put(id, mutableListOf<String>("yourString"))
} else{
        hashMap.get(id)?.add("yourString")
}

Upvotes: 0

dunni
dunni

Reputation: 44515

You could either use the Google Guava library, which has implementations for Multi-Value-Maps (Apache Commons Collections has also implementations, but without generics).

However, if you don't want to use an external lib, then you would do something like this:

if (map.get(id) == null) { //gets the value for an id)
    map.put(id, new ArrayList<String>()); //no ArrayList assigned, create new ArrayList

map.get(id).add(value); //adds value to list.

Upvotes: 50

Chetan
Chetan

Reputation: 184

  • First create HashMap.

    HashMap> mapList = new HashMap>();

  • Get value from HashMap against your input key.

    ArrayList arrayList = mapList.get(key);

  • Add value to arraylist.

    arrayList.add(addvalue);

  • Then again put arraylist against that key value. mapList.put(key,arrayList);

It will work.....

Upvotes: 4

&#211;scar L&#243;pez
&#211;scar L&#243;pez

Reputation: 235984

First you retreieve the value (given a key) and then you add a new element to it

    ArrayList<String> grades = examList.get(courseId);
    grades.add(aGrade);

Upvotes: 3

Mechkov
Mechkov

Reputation: 4324

String courseID = "Comp-101";
List<String> scores = new ArrayList<String> ();
scores.add("100");
scores.add("90");
scores.add("80");
scores.add("97");

Map<String, ArrayList<String>> myMap = new HashMap<String, ArrayList<String>>();
myMap.put(courseID, scores);

Hope this helps!

Upvotes: 8

Matt Fenwick
Matt Fenwick

Reputation: 49085

First, you have to lookup the correct ArrayList in the HashMap:

ArrayList<String> myAList = theHashMap.get(courseID)

Then, add the new grade to the ArrayList:

myAList.add(newGrade)

Upvotes: 2

Related Questions