๐ŸŒ
Reddit
reddit.com โ€บ r/learnjava โ€บ how to populate an arraylist in a hashmap?
r/learnjava on Reddit: How to populate an ArrayList in a HashMap?
May 31, 2021 -

I found a thread on stackoverflow, but I'm just not tracking how to do this in my program.

I am reading the book "Job Ready Java" and one of the exercises is to create a program of student quiz scores that stores their name as a String and scores as an ArrayList in a Hashmap. I think what they want is for there to be a Hashmap like this:

StudentNam(key) : Scores(values)

Bob : [47, 59, 60, 27]

Frank : [89, 79, 80, 90]

I completely cannot fathom how to add multiple values into Scores using a HashMap. If it was just a HashMap<String, Integer> then I would do:

Hashmap<String, Integer> students = new HashMap<String, Integer>();
students.put("Bob" 69);

But how to enter an array of values to represent Bob's scores if he has like 10 scores? TYIA and sorry if this is kind of stream of conscious question, but I'm racking my brain on this one.

๐ŸŒ
Quora
quora.com โ€บ How-do-you-add-an-element-to-an-arraylist-thats-in-a-hashmap
How to add an element to an arraylist that's in a hashmap - Quora
Quora is a place to gain and share knowledge. It's a platform to ask questions and connect with people who contribute unique insights and quality answers.
Discussions

java - How to add element into ArrayList in HashMap - Stack Overflow
How to add element into ArrayList in HashMap? HashMap (); More on stackoverflow.com
๐ŸŒ stackoverflow.com
java - How to add HashMap to ArrayList - Stack Overflow
Can someone please tell me why the code below overwrites every element in the ArrayList with the most recent entry into the ArrayList? Or how to correctly add new elements of hashmaps to my ArrayL... More on stackoverflow.com
๐ŸŒ stackoverflow.com
java - Converting ArrayList to HashMap - Stack Overflow
The general methodology would be to iterate through the ArrayList, and insert the values into the HashMap. An example is as follows: HashMap productMap = new HashMap(); for (Product product : productList) { productMap.put(product.getProductCode(), product); } More on stackoverflow.com
๐ŸŒ stackoverflow.com
java - Best way to create a hashmap of arraylist - Stack Overflow
I have one million rows of data in .txt format. the format is very simple. For each row: user1,value1 user2,value2 user3,value3 user1,value4 ... You know what I mean. For each user, it could appea... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ convert-arraylist-to-hashmap-in-java
Convert ArrayList to HashMap in Java - GeeksforGeeks
July 23, 2025 - // Java program to convert ArrayList // to HashMap import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.Map; public class ArrayListExample { public static void main(String[] args) { // ArrayList of string ArrayList<String> languageList = new ArrayList<>(Arrays.asList("Java", "C++", "Python", "PHP", "NodeJS")); System.out.println( "-------------ArrayList---------------"); // printing the ArrayList for (String language : languageList) { System.out.println(language); } System.out.println( "--------------HashMap----------------"); // convertArrayListToHas
๐ŸŒ
Java67
java67.com โ€บ 2017 โ€บ 10 โ€บ java-8-convert-arraylist-to-hashmap-or.html
How to convert ArrayList to HashMap and LinkedHashMap in Java 8 - Example Tutorial | Java67
This is the ideal way to convert a List to Map in Java. We are iterating over List using enhanced for loop and inserting String as a key into a HashMap and its length as a value into HashMap.
๐ŸŒ
Quora
quora.com โ€บ How-do-I-add-an-ArrayList-to-a-Java-HashMap
How to add an ArrayList to a Java HashMap - Quora
Quora is a place to gain and share knowledge. It's a platform to ask questions and connect with people who contribute unique insights and quality answers.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ convert-arraylist-to-hashmap-in-java
Convert ArrayList to HashMap in Java
June 17, 2024 - In this possible approach, we are going to apply the Java 8 Stream method approach to perform the conversion of an array list into a set of Hash Map. colors.stream().collect(Collectors.toMap(x -> x.getName(),x -> x.getCode())); colors.strea...
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java collections โ€บ java map โ€บ how to store hashmap inside a list
How to Store HashMap Inside a List | Baeldung
April 3, 2025 - In this article, we talked about storing HashMaps inside a List in Java. Then, we wrote a simple example in which we added HashMap<String, ArrayList<String>> to a List<String> for Two book categories.
Find elsewhere
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 631749 โ€บ java โ€บ arraylist-index-items-hashmap-key
Using arraylist index and items as hashmap key and values? (Beginning Java forum at Coderanch)
April 6, 2014 - (Also note the use of Map, rather than HashMap - it's generally a better way to do things.) Fifth: Since you want each actor to have his/her own mapping. I would suggest that you add another loop to go through your ArrayList and put() each element into your Map.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ how-to-convert-arraylist-to-hashmap-before-java-8
How to Convert ArrayList to HashMap Before Java 8? - GeeksforGeeks
July 23, 2025 - Case 1: The most ideal way to convert an ArrayList into HashMap before Java 8 is through iterating over the given ArrayList using enhanced for-loop and inserting the String as the key and its length as the value into HashMap.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java collections โ€บ java map โ€บ add values to arraylist used as value in hashmap in java
Add Values to ArrayList Used as Value in HashMap in Java | Baeldung
July 27, 2026 - Map<String, List<String>> map = new HashMap<>(); ArrayListInHashMap.addKeyManually(map, K1, K1_V1); ArrayListInHashMap.addKeyManually(map, K1, K1_V2); ArrayListInHashMap.addKeyManually(map, K2, K2_V1); This will construct a map that looks like ...
๐ŸŒ
Dot Net Perls
dotnetperls.com โ€บ convert-hashmap-arraylist-java
Java - Convert HashMap to ArrayList - Dot Net Perls
May 25, 2023 - We then use put() to add 3 entries to the HashMap. Start We create an ArrayList of Entry elements. The Entry has the same key and value types as the HashMap we just created. Next We call addAll() on the ArrayList to add the entire entrySet to it. The ArrayList now contains all the HashMap's ...
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_hashmap.asp
Java HashMap
A HashMap can store many different combinations, such as: ... import java.util.HashMap; // Import the HashMap class HashMap<String, String> capitalCities = new HashMap<>(); Now you can use methods like put() to add key/value pairs, get() to ...
Top answer
1 of 1
2

I think what you need is a Map of Map of List.ie.,

Map< String, Map< String, List< String>>>

Sample code below:

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class Test {
public static void main(String[] args) throws IOException {

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

    //Adding
    addSchedule(scheduleMap, "1111", "Engineering", "Tue3");
    addSchedule(scheduleMap, "1111", "Engineering", "Wed2");
    addSchedule(scheduleMap, "1111", "Math", "Wed4");
    addSchedule(scheduleMap, "1111", "Physics", "Fri10");
    addSchedule(scheduleMap, "2222", "Engineering", "Wed2");
    addSchedule(scheduleMap, "2222", "Chemistry ", "Wed4");

    //Printing
    printSchedule(scheduleMap);

}

private static void printSchedule(Map<String, Map<String, List<String>>> scheduleMap) {
    //Sample printing
    for(Entry<String, Map<String, List<String>>> staff : scheduleMap.entrySet()) {
        for(Entry<String,List<String>> subject:staff.getValue().entrySet()) {
            StringBuilder sb=new StringBuilder();
            sb.append(staff.getKey()).append(" : ").append(subject.getKey()).append(" : ");
            for(String sessionTime: subject.getValue()) {
                sb.append(sessionTime).append(", ");
            }
            sb.setLength(sb.length()-2);
            System.out.println(sb.toString());
        }
    }
}

public static void addSchedule(Map<String,Map<String,List<String>>> scheduleMap, String id,String subject,String sessionTime) {
    if(!scheduleMap.containsKey(id)) {
        scheduleMap.put(id, new HashMap<String,List<String>>());
    }
    if(!scheduleMap.get(id).containsKey(subject)) {
        scheduleMap.get(id).put(subject, new ArrayList<String>());
    }
    scheduleMap.get(id).get(subject).add(sessionTime);
}
}
๐ŸŒ
Bukkit
bukkit.org โ€บ threads โ€บ arraylists-inside-of-a-hashmap.378937
ArrayLists inside of a HashMap. | Bukkit Forums
I've been trying to add a team's feature into my plugin and yes I know there's hundreds of plugins out there that do this but I want to learn it. I did...
๐ŸŒ
Blogger
javahungry.blogspot.com โ€บ 2021 โ€บ 02 โ€บ iterate-hashmap-with-arraylist.html
How to Iterate HashMap with ArrayList in Java with Example | Java Hungry
import java.util.*; public class HashMapWithArrayList { public static void main(String args[]) { // Creating HashMap object with ArrayList as Value HashMap<String, ArrayList<String>> map = new HashMap<>(); // Inserting key-value pairs in the above map map.put("USA", new ArrayList(Arrays.as...