Search code examples
javaarraysarraylisthashmaphashtable

How to store key=string, value=arraylist in HashMap or any other method in java?


public class Hashmapeg {
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    HashMap<String, ArrayList<Integer>> marksTable = new HashMap<String, ArrayList<Integer>>();
    ArrayList<Integer> marks = new ArrayList<>();
    int marksSub;
    
    for(int j = 0; j < 2; j++){
        System.out.print("\nEnter name " + j + " = ");
        String name = sc.nextLine();

        for(int i = 0; i < 3; i++){
            System.out.print("Enter marks " + i + " = ");
            marksSub = sc.nextInt();
            marks.add(marksSub);
        }
        marksTable.put(name, marks);
    }

    System.out.println(marksTable);
    sc.close();
}}

OUTPUT

Enter name 0 = abc
Enter marks 0 = 1
Enter marks 1 = 2
Enter marks 2 = 3

Enter name 1 = Enter marks 0 = 4
Enter marks 1 = 5
Enter marks 2 = 6
{=[1, 2, 3, 4, 5, 6], abc=[1, 2, 3, 4, 5, 6]}

EXPECTED OUTPUT is look like this

{name1=[1,2,3], name2=[4,5,6]}

I want to store this type of table in key value pair

-------------------------------------
|   name1   |   35  |   23  |   12  |
|   name2   |   45  |   20  |   10  |
-------------------------------------

How do I store this type of table using only java also I dont know how to iterate into this.


Solution

    1. You create marks outside the loop, so the same ArrayList is being used, only ONE, you need to create a new one at each iteration.

      Also prefer Integer.parseInt(sc.nextLine()) as sc.nextInt() because of newlines you don't want to handle correctly (that is what causes empty name in your output)

       for (int j = 0; j < 2; j++) {
           System.out.print("\nEnter name " + j + " = ");
           String name = sc.nextLine();
           marks = new ArrayList<>();
           for (int i = 0; i < 3; i++) {
               System.out.print("Enter marks " + i + " = ");
               marks.add(Integer.parseInt(sc.nextLine()));
           }
           marksTable.put(name, marks);
       }
      
    2. For printing, take a look at How to iterate a Map and

       Map<String, List<Integer>> marksTable = Map.of(
               "name1", Arrays.asList(1, 2, 3),
               "name2", Arrays.asList(4, 5, 6),
               "name3", Arrays.asList(7, 8, 9)
       );
      
       System.out.println("---------------------");
       for (Map.Entry<String, List<Integer>> entry : marksTable.entrySet()) {
           String values = entry.getValue().stream().map(Object::toString).collect(Collectors.joining(" | "));
           System.out.println("| " + entry.getKey() + " | " + values + " |");
       }
       System.out.println("---------------------");
      

    Also prefer more generic definitions, Map instead of HashMap, List instead of ArrayList, for the definition, for the instanciation keep classes

    Map<String, List<Integer>> marksTable = new HashMap<>();
    List<Integer> marks;