HashSet学习笔记(一)

来源:互联网 发布:淘宝快递打单软件 编辑:程序博客网 时间:2024/06/14 16:38

HashSet判断集合元素相同的标准:两个对象通过equals方法比较相等,并且两个对象的hashCode()方法返回值也相等。

import java.util.*;class A{public boolean equals(Object obj){return true;}}class B{public int hashCode(){return 1;}}class C{public int hashCode(){return 2;}public boolean equals(Object obj){return true;}}public class TestHashSet {public static void main(String[] args){HashSet books=new HashSet();books.add(new A());books.add(new A());books.add(new B());books.add(new B());books.add(new C());books.add(new C());System.out.println(books);}}


 运行结果可能为:

[B@1, B@1, C@2, A@728edb84, A@1224b90]

原创粉丝点击