【问题标题】:Printing elements of list, which is the value of HashMap打印list的元素,也就是HashMap的值
【发布时间】:2021-02-28 23:37:32
【问题描述】:

我创建了一个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());
    }
    

  }
}

【问题讨论】:

    标签: java list hashmap


    【解决方案1】:

    如果我们可以在获取 Key 值时使用 for 循环,然后我们去 for 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();
            }
    
        }
    }
    

    【讨论】:

    • 非常感谢!只是把戏。
    • 继续学习 :)
    猜你喜欢
    • 2017-10-10
    • 1970-01-01
    • 2014-01-15
    • 1970-01-01
    • 1970-01-01
    • 2019-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多