0

I have little problem displaying the contents of my hashmap.

public ConcurrentHashMap<Integer, Student> StudentList 
                        = new ConcurrentHashMap<>();

'Student' is a class with two string fields(first name, name), and addition method to return the name (get_name).

But my question is how can I display or it is possible to display names with keys about students in HashMap ? I tried something like that, but only for one filed.

for(Student i : StudentList.values()) {
    System.out.println(i.get_name());
}
Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108
anocyney_
  • 1
  • 3
  • 2
    https://stackoverflow.com/questions/5920135/printing-hashmap-in-java – OldProgrammer Jun 29 '20 at 00:45
  • 1
    By the way, according to Java style conventions, method names and variable names start with a lowercase letter and use camel case, like `getName` and `studentList` – user Jun 29 '20 at 00:52

1 Answers1

1

You can use entrySet.

for(Map.Entry<Integer, Student> entry: StudentList.entrySet()){
   System.out.println(entry.getKey() + ": " + entry.getValue());
}

Or forEach.

StudentList.forEach((key,value)->System.out.println(key + ": " + value));
Unmitigated
  • 76,500
  • 11
  • 62
  • 80