二つの方法はMapを経由して集合します。

3320 ワード

以下の需要を分析して、コードで実現します。(1)属性を含む学生タイプStudentを定義します。名前(String name)、年齢(int age)(2)はMapセットを定義し、Studentオブジェクトをkeyとして、文字列(これは学生の住所を表します。)でvalue(3)として4つの方法でMapセットの内容を巡回します。
public class test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        HashMap hashMap = new HashMap<>();
        hashMap.put(new Student("xiaoming",18), "111");
        hashMap.put(new Student("xiaohong",18), "222");
        hashMap.put(new Student("xiaohua",18), "333");
        hashMap.put(new Student("xiaobai",18), "111");
        function_2(hashMap);


    }
    public static void function_1(HashMap hashMap){
        Set set = hashMap.keySet();
        Iterator it = set.iterator();
        while(it.hasNext()){
            System.out.println(hashMap.get(it.next()));
        }

    }

    public static void function_2(HashMap hashMap) {
        Set> entry = hashMap.entrySet();
//      System.out.println(entry);
        Iterator> it = entry.iterator();
        while(it.hasNext()){
            Entry ent = it.next();
            String s = ent.getValue();
            Student student = ent.getKey();
            System.out.println(student+" "+s);
        }

    }

    public static void function_3(HashMap hashMap) {

    }

    public static void function_4(HashMap hashMap) {

    }

}