HashMap в методы

220
07 мая 2017, 23:42

Нужно вывести все значения HashMap hmap за ключом Group. Но в метод передается пустой hmap. Выводит null. Как передать в метод заполненный 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);
    }
}
Answer 1

В потоке выполнения просто нет этапа где заполнялась бы ваша мапа.

Вот вы пишите:

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

Вы вызываете метод frak, а в нем только перекидывание ссылок, циклы, и вывод на консоль.

Вероятно заполнение должно произойти в методе somm, и что бы было что выводить, вызвать его мне кажется было бы не лишним...

Answer 2

Спасибо. переписала, все работает.

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);
    }
}
READ ALSO
Загрузка ресурсов из .jar файла

Загрузка ресурсов из .jar файла

У меня в проекте в папке с ресурсами лежит папка сxml файлами

250
Как &ldquo;ужать&rdquo; кнопку?

Как “ужать” кнопку?

Как стандартный компонент Button сделать узким, но чтобы текст на них не "резался", а оставался посередине кнопки?

184
Совместное использование container и container-fluid в Bootstrap 3.5

Совместное использование container и container-fluid в Bootstrap 3.5

Допускается ли совместное использование классов container-fluid и container? Можно ли их вкладывать друг в друга?

284
Картинка, прикрепляемая к ссылке ВК

Картинка, прикрепляемая к ссылке ВК

Как задать конкретную картинку, которая всегда будет прикрепляться к ссылке, размещенной ВК?

212