24

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?

8 Answers 8

51

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.
Sign up to request clarification or add additional context in comments.

2 Comments

There's also Spring MultiValueMap and LinkedMultiValueMap
Nice example, exactly what a Java n00b like me required.
8
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!

3 Comments

and how can i get the keys,values?
@David Use the HashMap's keySet() method. It will return all keys which in the above example would be Strings. Then for each key, you can get the corresponding ArrayList<String> by using the HashMap's get(key) method, where 'key' is the keys you retrieved earlier. Hope this helps!
@greenhorn No problem!
5

Java 8+ has Map.compute for such cases:

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

Comments

4
  • 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.....

2 Comments

Is it necessary to put the arraylist back into the hashmap? or is an item added by reference?
No, you don't have to put the list back in mapList, you can skip the final step listed here.
3

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);

Comments

2

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)

Comments

0

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")
}

Comments

0
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)));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.