当前位置:网站首页>HashSet collection

HashSet collection

2022-06-11 18:18:00 m0_ sixty million eighty-seven thousand seven hundred and twent

package com.study.exception.demo21;

public class HashDemo {

    /*
     Hash value :
         yes JDK Based on the address or string or number of the object int Type value 
    Object Class has a method to get the hash value of the object 
        public int hashCode(): Returns the hash code value of the object 

     The hash value of the object 
    1. The same object is called more than once hashCode() Method returns the same hash value 
    2. By default , Different objects have different hash values . And rewrite hashCode() Method , The hash value of different objects can be the same 
     */

    public static void main(String[] args) {

        // Create student objects 
        Student s1 = new Student(" Zhang San ",10);

        // The same object is called more than once hashCode() Method returns the same hash value 
        System.out.println(s1.hashCode());//460141958
        System.out.println(s1.hashCode());//460141958

        // By default , Of different objects hash It's not the same 
        // Override by method , The hash value of different objects can be the same 
        Student s2 = new Student(" Zhang San ",10);
        System.out.println(s2.hashCode());//1163157884

        System.out.println("hello".hashCode());//99162322
        System.out.println("world".hashCode());//113318802

        System.out.println("---------------");
        System.out.println(" chinese ".hashCode());
        System.out.println(" english ".hashCode());
    }
}
原网站

版权声明
本文为[m0_ sixty million eighty-seven thousand seven hundred and twent]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111758322383.html