HashSet的底层实现了解和使用方法

来源:互联网 发布:淘宝官方旗舰店怎么开 编辑:程序博客网 时间:2024/06/05 21:09

HashSet:实现了Set接口的一个类。

90、  关于 Object 类的 equals 方法的特点

a)  自反性:x.equals(x)应该返回 true

b)  对称性:x.equals(y)为 true,那么 y.equals(x)也为 true。

c)  传递性:x.equals(y)为  true 并且 y.equals(z)为 true,那么 x.equals(z)也应该为 true。

d)  一致性:x.equals(y)的第一次调用为 true,那么 x.equals(y)的第二次、第三次、第 n

次调用也应该为true,前提条件是在比较之间没有修改 x 也没有修改 y。

e)  对于非空引用 x,x.equals(null)返回 false。

91、  关于 Object 类的 hashCode()方法的特点:

a)  在 Java 应用的一次执行过程当中,对于同一个对象的 hashCode 方法的多次调用,

他们应该返回同样的值(前提是该对象的信息没有发生变化)。

b)  对于两个对象来说,如果使用 equals 方法比较返回 true,那么这两个对象的 hashCode值一定是相同的。

c)  对于两个对象来说,如果使用 equals方法比较返回false,那么这两个对象的 hashCode值不要求一定不同(可以相同,可以不同),但是如果不同则可以提高应用的性能。

d)  对于Object类来说,不同的Object对象的hashCode值是不同的(Object类的hashCode

值表示的是对象的地址)。

92、  当使用 HashSet 时,hashCode()方法就会得到调用,判断已经存储在集合中的对象的hash code 值是否与增加的对象的 hash code 值一致;如果不一致,直接加进去;如果一致,再进行 equals 方法的比较,equals 方法如果返回 true,表示对象已经加进去了,就不会再增加新的对象,否则加进去。

93、  如果我们重写 equals 方法,那么也要重写 hashCode 方法,反之亦然。

94、

public class TestSet {        public static void main(String[] args) {                HashSet set = new HashSet();                System.out.println(set.add("a"));        set.add("b");        set.add("c");        set.add("d");        System.out.println(set.add("a"));                System.out.println(set);    }}  public class TestSet1 {     public static void main(String[] args) {                HashSet set = new HashSet();                //People p1 = new People("zhangsan");        //People p2 = new People("lisi");        //People p3 = new People("zhangsan");                //set.add(p1);        //set.add(p2);        //set.add(p3);                //System.out.println(set);                //People p1 = new People("zhangsan");                //set.add(p1);        //set.add(p1);                //System.out.println(set);                        String s1 = new String("a");        String s2 = new String("a");                System.out.println(s1.hashCode()== s2.hashCode());//比较字符串的 hash code                set.add(s1);        set.add(s2);                System.out.println(set);                    }}  class People{        String name;        People(String name){        this.name = name;    }}


 

String 的方法:hashCode源码:

public int hashCode() {    int h = hash;    if (h == 0) {        int off =offset;        char val[] =value;        int len =count;             for (int i = 0; i < len; i++) {                h = 31*h + val[off++];            }            hash = h;        }        return h;}


 

String 对象的哈希码根据以下公式计算:

 s[0]*31^(n-1) +s[1]*31^(n-2) + ... + s[n-1]

实际上String的hash code就是字符串的内容。

95、举例:

public class TestSet2 {         public static void main(String[] args) {                HashSet set = new HashSet();                Student s1 = new Student("zhangsan");        Student s2 = new Student("zhangsan");                set.add(s1);        set.add(s2);                System.out.println(set);    }}  class Student{    String name;        Student(String name){        this.name = name;    }        public int hashCode(){        return this.name.hashCode();    }        public boolean equals(Object obj){        if(this == obj){            return true;        }                if(obj !=null && obj instanceof Student){            Student s = (Student)obj;            if(name.equals(s.name)){                return true;            }        }        return false;    }}


 

在使用HashSet时,要重写equalshashCode方法。

原创粉丝点击