继承于Set接口,
1、HashSet不能重复存储equals相同的数据 。原因就是equals相同,数据的散列码也就相同(hashCode必须和equals兼容)。大量相同的数据将存放在同一个散列单元所指向的链表中,造成严重的散列冲突,对查找效率是灾难性的。
2、HashSet的存储是无序的 ,没有前后关系,他并不是线性结构的集合。
3、hashCode必须和equals必须兼容, 这也是为了第1点。
1. 继承结构
java.lang.Object
|_ java.util.AbstractCollection<E>
|_ java.util.AbstractSet<E>
|_ java.util.HashSet<E>
2. 主要方法
add(Object)
addAll(Collection)
remove(object)
removeAll(Collection)
size()
iterator()
toArray()
clear()
isEmpty()
contain(object)
containAll(Collection)
3. 不允许出现相同的项
Set集合中不允许出现相同的项,Set集合在用Add()方法添加一个新项时,首先会调用equals(Object o)来比较新项和已有的某项是否相等,而不是用==来判断相等性,所以对于字符串等已重写equals方法的类,是按值来比较相等性的
- Set<String> setA=(Set<String>)new HashSet();
- setA.add(new String("ABC"));
- setA.add(new String("CC"));
- setA.add(new String("ABC"));
- setA.add(new String("BB"));
- setA.add(new String("ABC"));
- System.out.println("size="+setA.size()); //3, 相同的项不存储
- Iterator<String> ite=setA.iterator();
- while(ite.hasNext()){
- stem.out.println(ite.next());//CC BB ABC
- }
Set<String> setA=(Set<String>)new HashSet();
setA.add(new String("ABC"));
setA.add(new String("CC"));
setA.add(new String("ABC"));
setA.add(new String("BB"));
setA.add(new String("ABC"));
System.out.println("size="+setA.size()); //3, 相同的项不存储
Iterator<String> ite=setA.iterator();
while(ite.hasNext()){
System.out.println(ite.next());//CC BB ABC
}
4. 哈希算法
在set类型的集合中,如何判断元素是否重复呢,这就需要使用Object.equals方法,但如果元素很多了,添加一个新元素时,比较的次数 就很多,例如已经有100个元素了,添加第101个元素时,就要和前面的元素比较100次,效率很低。
JAVA中采用哈希表的原理,哈希是个人名,它提出了哈希算法的概念,哈希算法也称为散列算法,是将数据依据酸法直接指定到一个地址上,
hascode实际上是返回的对象存储的物理地址
HashSet类按照哈希算法来存取对象,当向集合中加入一个新对象时,会调用对象的HashCode()方法得到对象的哈希码,然后根据这个码计算出对象在集合中存储的位置。
Object类中定义了hashCode()和equals(Object o)方法,如果object1.equals(object2),那么说明这两个引用变量指向同一个对象,那么object1 and object2的hashCode也一定相等
为了保证HashSet能正常工作,要求当两个对象用equals比较相等时,hashCode也要相等,否则就会有可能加入两个相同的项。
- public class Person {
- private String name;
- private Integer age;
- public Integer getAge() {
- return age;
- }
- public void setAge(Integer age) {
- this.age = age;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public boolean equals(Object o){
- if(this==o)return true;
- if(!(o instanceof Person))
- return false;
- final Person p=(Person)o;
- if(this.name.equals(p.getName()))
- return true;
- else
- return false;
- }
- }
- Set<Person> setA=(Set<Person>)new HashSet();
- Person A=new Person();
- A.setAge(24);
- A.setName("Jack");
- Person B=new Person();
- B.setAge(24);
- B.setName("Jack");
- setA.add(A);
- setA.add(B);
- System.out.println("size="+setA.size()); //2
public class Person {
private String name;
private Integer age;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean equals(Object o){
if(this==o)return true;
if(!(o instanceof Person))
return false;
final Person p=(Person)o;
if(this.name.equals(p.getName()))
return true;
else
return false;
}
}
Set<Person> setA=(Set<Person>)new HashSet();
Person A=new Person();
A.setAge(24);
A.setName("Jack");
Person B=new Person();
B.setAge(24);
B.setName("Jack");
setA.add(A);
setA.add(B);
System.out.println("size="+setA.size()); //2
虽然A与B用equals比较相等,但因为HashCode不同,HashSet为A和B计算出了不同的存储位置,于是把他们放到了集合的不同位置
可以重写hascode和equas方法
例如:
- public int hasCode(){
- int result;
- result=(this.name==null?0:name.hashCode());
- result=37*result+(this.age==null?0:age.hashCode());
- return result;