2020-08-29 HashMapでequalsメソッドとhashCodeメソッドの書き換えについて


次のコードは3つの部分に分かれています.
①カスタムキー遍歴は適用されません
②カスタムキーの遍歴を使用するがhashCode()とequals()メソッドを書くことから
③カスタムキーを用いた遍歴+hashcodeメソッドとequalsメソッドの書き換え
package map;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class Demo {
     
    public static void main(String[] args) {
     
        HashMap<String,Student> map=new HashMap<>();
        map.put(" ",new Student("   ",18));
        map.put(" ",new Student("   ",18));
        map.put(" ",new Student("   ",18));
        map.put(" ",new Student("   ",18));

        //         
        System.out.println("         ");
        for (String key:map.keySet()
             ) {
     
            System.out.println(key+" "+map.get(key).getName()+" "+map.get(key).getAge());
        }
        /**            :
         *                18
         *                18
         *                18
         *
         *            :
         *              ①hashMap    
         *              ②              ,           ,        
         *
         * */
        System.out.println("         ,    hashCode() equals()  ");
        HashMap<Student,String> map1=new HashMap<>();
        map1.put(new Student("   ",18)," ");
        map1.put(new Student("   ",18)," ");
        map1.put(new Student("   ",18)," ");
        map1.put(new Student("   ",18),"4");
        map1.put(new Student("   ",18)," ");
        for (Student key1:map1.keySet()
             ) {
     
            System.out.println(key1.getAge()+" "+key1.getName()+" "+map1.get(key1));
        }
        /**
         *
         *                 ;
         *                 18
         *                 18
         *                 18
         *                 :
         *          18     
         *          18     
         *          18    4
         *          18     
         *          18     
         *        :
         *                       equals   hashCode  ,       ,      。
         *
         * */

        //  equals   hashcode  
        HashMap<Student02, String> map2=new HashMap<>();
        map2.put(new Student02("   ",18)," ");
        map2.put(new Student02("   ",18)," ");
        map2.put(new Student02("   ",18)," ");
        map2.put(new Student02("   ",18),"4");
        map2.put(new Student02("   ",18)," ");
        System.out.println("         +  hashcode   equals  ");
        Set<Map.Entry<Student02, String>> set=map2.entrySet();
        Iterator<Map.Entry<Student02, String>> it=set.iterator();
        while(it.hasNext()){
     
            Map.Entry<Student02,String> student= it.next();
            System.out.println(student.getKey().getAge()+" "+student.getKey().getName()+" "+map2.get(student));
        }

/***
 *                   
 *                    18
 *                    18
 *                    18
 *                   ,    hashCode() equals()  
 *              18      
 *              18      
 *              18     4
 *              18      
 *              18      
 *                   +  hashcode   equals  ,    hashCode() equals()  
 *              18     null
 *              18     null
 *              18     null
 *
 *               :
 *                        ,     equals   hashCode  ,               ,     (    )
 *		                 
 *
 */

    }
}