打印list的元素,即HashMap的值

Printing elements of list, which is the value of HashMap

我创建了一个HashMap<String,String[]>

我正在尝试在每个值的列表元素旁边打印键。

所以例如第一行应该打印

English : Alex Lamplough Kayleigh Lamplough Bella Lamplough

我的代码:

public class Students {

private HashMap <String, String[]> subjects;

public Students() {
    
    subjects = new HashMap<String, String[]>();
    
}

public void initialDetails() {
    
    this.subjects.put("English", new String[] {"Alex Lamplough", "Kayleigh Lamplough", "Bella Lamplough"});
    this.subjects.put("Maths", new String[] {"Bill Burr", "Kayleigh Lamplough", "Jake Stanford"});
    this.subjects.put("Science", new String[] {"Paul Hatton", "Jake Stanford", "Bill Burr"});
    this.subjects.put("IT", new String[] {"Jake Stanford", "Alex Lamplough", "Julie King"});
    this.subjects.put("Sports", new String[] {"Barbara Kensington", "Bella Lamplough", "Alex Lamplough"});
    this.subjects.put("Languages", new String[] {"Julie King", "Harry Milner", "Bella Lamplough"});
    this.subjects.put("History", new String[] {"Harry Milner", "Kayleigh Lamplough", "Bill Burr"});
    
    Set set = this.subjects.entrySet();
    Iterator iterator = set.iterator();
    while(iterator.hasNext()) {
        Map.Entry mentry = (Map.Entry) iterator.next();
        System.out.println(mentry.getKey() + " : " + mentry.getValue());
    }
    

  }
}

也许更好的方法是,如果我们可以在获取 Key 值时使用 for 循环,然后我们去寻找作为 String 数组的 HashMap 值

String [] names = (String[]) entry.getValue();

当然,我们用 for each 来打印元素

    for(Map.Entry entry : subjects.entrySet()){
            System.out.print("key " + entry.getKey() + " : ");
            String [] names = (String[]) entry.getValue();
            for(String name : names){
                System.out.print(name + " ");
            }
            System.out.println();
        }

    }
}

输出

key English : Alex Lamplough Kayleigh Lamplough Bella Lamplough 
key Maths : Bill Burr Kayleigh Lamplough Jake Stanford 
key Science : Paul Hatton Jake Stanford Bill Burr 
key Languages : Julie King Harry Milner Bella Lamplough 
key IT : Jake Stanford Alex Lamplough Julie King 
key History : Harry Milner Kayleigh Lamplough Bill Burr 
key Sports : Barbara Kensington Bella Lamplough Alex Lamplough 

完整代码

    public class Students {

    private HashMap<String, String[]> subjects;

    public Students() {

        subjects = new HashMap<String, String[]>();

    }

    public static void main(String[] args) {

        Students s = new Students();

        s.initialDetails();
    }

    public void initialDetails() {

        this.subjects.put("English", new String[]{"Alex Lamplough", "Kayleigh Lamplough", "Bella Lamplough"});
        this.subjects.put("Maths", new String[]{"Bill Burr", "Kayleigh Lamplough", "Jake Stanford"});
        this.subjects.put("Science", new String[]{"Paul Hatton", "Jake Stanford", "Bill Burr"});
        this.subjects.put("IT", new String[]{"Jake Stanford", "Alex Lamplough", "Julie King"});
        this.subjects.put("Sports", new String[]{"Barbara Kensington", "Bella Lamplough", "Alex Lamplough"});
        this.subjects.put("Languages", new String[]{"Julie King", "Harry Milner", "Bella Lamplough"});
        this.subjects.put("History", new String[]{"Harry Milner", "Kayleigh Lamplough", "Bill Burr"});

//        Set set = this.subjects.entrySet();
//        Iterator iterator = set.iterator();
//        while (iterator.hasNext()) {
//            Map.Entry mentry = (Map.Entry) iterator.next();
//            System.out.println(mentry.getKey() + " : " + mentry.getValue());
//        }


        for(Map.Entry entry : subjects.entrySet()){
            System.out.print("key " + entry.getKey() + " : ");
            String [] names = (String[]) entry.getValue();
            for(String name : names){
                System.out.print(name + " ");
            }
            System.out.println();
        }

    }
}