JAVA:HashMapの一般的な方法、カスタムクラスのストレージ、ソースコード分析

4042 ワード

  public  static void main(String[] args) {

        //hashMap       +  
        //          

        HashMap hashMap = new HashMap();
        hashMap.put("zhangsan", 18);//    
        hashMap.put("lisi", 19);
        Integer lisi = hashMap.get("lisi");//       
        //        ,  key         ,      , null          , value    
        //       
        /**
         *        :
         * entrySet(),keySet(),values()
         */
        System.out.println("      ");
        Set> entries = hashMap.entrySet();
        Iterator> iterator = entries.iterator();
        while (iterator.hasNext()){
            Map.Entry next = iterator.next();
            System.out.println(next.getKey());
        }
        System.out.println("  key   ");
        Set strings = hashMap.keySet();
        Iterator iterator1 = strings.iterator();
        while (iterator1.hasNext()){
            String next = iterator1.next();
            System.out.println(next);
        }
        System.out.println("  values   ");
        Collection values = hashMap.values();
        Iterator iterator2 = values.iterator();
        while (iterator2.hasNext()){
            Integer next = iterator2.next();
            System.out.println(next);
        }

        /**
         *
         *       String,Integer  ,            hashcode(),equals()  ;
         *          ,    hashcode(),equals()  ;
         * hashcode()              ,                ,      key           ,
         * equals()           value key    ,        ,    , new entry()       
         *             ,      ,   key value     ,         ,             ,
         *        ,           
         *           ?
         *       ,  jdk1.8  ,        8   ,      ,      ,    
         */
        
  HashMap hashMap1=new HashMap();
  hashMap1.put(new Student("lisi",201302),2);
  hashMap1.put(new Student("lisi",201302),2);
          //    hashcode() equals()  ,          ,           ,put ,  object  hashcode() equals()
        //   ,  new Student()     ,         ,   hashmap          ,              ,    。
        Set students = hashMap1.keySet();
        Iterator iterator3 = students.iterator();
        while (iterator3.hasNext()){
            Student next = iterator3.next();
            System.out.println(next.toString());
        }
        /**
         * hashmap        ,         ,   0.75,           0.75*         ,     2   ;
         *       hashmap   :
         * key     ,     null
         * values       null
         *         ,   key    value   ,    value    value     value;
         *          ,      for  
         *          
         * 
         * 
         */

    }

    }
class Student {

    private String name;
    private int id;

    public Student(String name, int id) {
        this.name = name;
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return id == student.id &&
                Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {

        return Objects.hash(name, id);
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", id=" + id +
                '}';
    }
}