It is necessary to display all the values ​​of HashMap hmap for the Group key. But an empty hmap is passed to the method. Displays null. How to transfer to a method the filled HashMap hmap?

public class ListGroup { static Map<Group, ArrayList<People>> hmap; @Override public String toString() { return "ListGroup [hmap=" + hmap + "]"; } public Map<Group, ArrayList<People>> getHmap() { return hmap; } public void setHmap(Map<Group, ArrayList<People>> hmap) { this.hmap = hmap; } public ListGroup(Map<Group, ArrayList<People>> hmap) { super(); this.hmap = hmap; } public ListGroup() { super(); } public static Map<Group, ArrayList<People>> somm() { ArrayList<People> people1 = new ArrayList<>(); people1.add(new People(85, 170, "Gava", "Ivan", true)); people1.add(new People(120, 180, "Nazumo", "Petro", false)); people1.add(new People(95, 167, "Dorod", "Marija", true)); people1.add(new People(69, 170, "Omaee", "Vova", true)); Map<Group, ArrayList<People>> hmap = new HashMap<>(); hmap.put(new Group("The Best"), people1); return hmap; } public static void frak(Map<Group, ArrayList<People>> hmap) { Set<Entry<Group, ArrayList<People>>> entries = hmap.entrySet(); for (Iterator<Entry<Group, ArrayList<People>>> iterator = entries.iterator(); iterator.hasNext();) { Entry<Group, ArrayList<People>> entry = iterator.next(); if (entry.getKey().getName().equals("The Best")) { for (Iterator<People> i = entry.getValue().iterator(); i.hasNext();) { System.out.println(entry); break; } } } } public static void main(String[] args) { System.out.println(hmap); frak(hmap); } } 
  • one
    and where do you have to go assigning values ​​to this hmap itself? as I see, from the very first line of the executed code you are trying to output hmap (in which there is nothing yet), and then again without any assignment you give it as an argument = / - MrModest

2 answers 2

There is simply no stage in the execution flow where your map would fill.

Here you write:

 public static void main(String[] args) { System.out.println(hmap); frak(hmap); // <-- } 

You call the frak method, and in it only throw links, cycles, and output to the console.

Probably the filling should occur in the somm method, and what would be what to output, it would seem to me not to cause it to be superfluous ...

    Thank. rewrote, everything works.

     public class ListGroup { static Map<Group, ArrayList<People>> hmap; @Override public String toString() { return "ListGroup [hmap=" + hmap + "]"; } public Map<Group, ArrayList<People>> getHmap() { return hmap; } public void setHmap(Map<Group, ArrayList<People>> hmap) { this.hmap = hmap; } public ListGroup(Map<Group, ArrayList<People>> hmap) { super(); this.hmap = hmap; } public ListGroup() { super(); } public static Map<Group, ArrayList<People>> somm(Map<Group, ArrayList<People>> map) { ArrayList<People> people1 = new ArrayList<>(); people1.add(new People(85, 170, "Gava", "Ivan", true)); people1.add(new People(120, 180, "Nazumo", "Petro", false)); people1.add(new People(95, 167, "Dorod", "Marija", true)); people1.add(new People(69, 170, "Omaee", "Vova", true)); Map<Group, ArrayList<People>> hmap = new HashMap<>(); hmap.put(new Group("The Best"), people1); return hmap; } public static void frak(Map<Group, ArrayList<People>> hmap) { Set<Entry<Group, ArrayList<People>>> entries = hmap.entrySet(); for (Iterator<Entry<Group, ArrayList<People>>> iterator = entries.iterator(); iterator.hasNext();) { Entry<Group, ArrayList<People>> entry = iterator.next(); if (entry.getKey().getName().equals("The Best")) { for (Iterator<People> i = entry.getValue().iterator(); i.hasNext();) { System.out.println(entry); break; } } } } public static void main(String[] args) { Map<Group, ArrayList<People>> map = somm(hmap); System.out.println(map); frak(map); } }