标签:容量 去除 介绍 否则 collect col this link clip
Java中的set是一个不包含重复元素的集合,确切地说,是不包含e1.equals(e2)的元素对。Set中允许添加null。Set不能保证集合里元素的顺序。
在往set中添加元素时,如果指定元素不存在,则添加成功。也就是说,如果set中不存在(e==null ? e1==null : e.queals(e1))的元素e1,则e1能添加到set中。
下面以set的一个实现类HashSet为例,简单介绍一下set不重复实现的原理:
- package com.darren.test.overide;
-
- public class CustomString {
- private String value;
-
- public CustomString() {
- this("");
- }
-
- public CustomString(String value) {
- this.value = value;
- }
- }
package com.darren.test.overide;
public class CustomString {
private String value;
public CustomString() {
this("");
}
public CustomString(String value) {
this.value = value;
}
}
- package com.darren.test.overide;
-
- import java.util.HashSet;
- import java.util.Set;
-
- public class HashSetTest {
- public static void main(String[] args) {
- String a = new String("A");
- String b = new String("A");
-
- CustomString c = new CustomString("B");
- CustomString d = new CustomString("B");
-
- System.out.println("a.equals(b) == " + a.equals(b));
- System.out.println("c.equals(d) == " + c.equals(d));
-
- Set<Object> set = new HashSet<Object>();
- set.add(a);
- set.add(b);
- set.add(c);
- set.add(d);
-
- System.out.println("set.size() == " + set.size());
-
- for (Object object : set) {
- System.out.println(object);
- }
- }
- }
package com.darren.test.overide;
import java.util.HashSet;
import java.util.Set;
public class HashSetTest {
public static void main(String[] args) {
String a = new String("A");
String b = new String("A");
CustomString c = new CustomString("B");
CustomString d = new CustomString("B");
System.out.println("a.equals(b) == " + a.equals(b));
System.out.println("c.equals(d) == " + c.equals(d));
Set<Object> set = new HashSet<Object>();
set.add(a);
set.add(b);
set.add(c);
set.add(d);
System.out.println("set.size() == " + set.size());
for (Object object : set) {
System.out.println(object);
}
}
}
运行结果如下:
- a.equals(b) == true
- c.equals(d) == false
- set.size() == 3
- com.darren.test.overide.CustomString@2c39d2
- A
- com.darren.test.overide.CustomString@5795ce
a.equals(b) == true
c.equals(d) == false
set.size() == 3
com.darren.test.overide.CustomString@2c39d2
A
com.darren.test.overide.CustomString@5795ce
也许你已经看出关键来了,没错就是equals方法。这么说还是不恰当,准确的说应该是equals和hashcode方法。为什么这么说呢,让我们改一改CustomString类在进行测试:
- package com.darren.test.overide;
-
- public class CustomString {
- private String value;
-
- public CustomString() {
- this("");
- }
-
- public CustomString(String value) {
- this.value = value;
- }
-
- @Override
- public boolean equals(Object obj) {
- if (this == obj) {
-
- return true;
- } else if (obj instanceof CustomString) {
- CustomString customString = (CustomString) obj;
-
- return customString.value.equals(value);
- } else {
-
- return false;
- }
- }
- }
package com.darren.test.overide;
public class CustomString {
private String value;
public CustomString() {
this("");
}
public CustomString(String value) {
this.value = value;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
} else if (obj instanceof CustomString) {
CustomString customString = (CustomString) obj;
return customString.value.equals(value);
} else {
return false;
}
}
}
测试结果:
- a.equals(b) == true
- c.equals(d) == true
- set.size() == 3
- com.darren.test.overide.CustomString@12504e0
- A
- com.darren.test.overide.CustomString@1630eb6
a.equals(b) == true
c.equals(d) == true
set.size() == 3
com.darren.test.overide.CustomString@12504e0
A
com.darren.test.overide.CustomString@1630eb6
这次的equals返回值都为true,但是set的size还是3
让我们继续改
- package com.darren.test.overide;
-
- public class CustomString {
- private String value;
-
- public CustomString() {
- this("");
- }
-
- public CustomString(String value) {
- this.value = value;
- }
-
- @Override
- public int hashCode() {
-
- return 1;
- }
- }
package com.darren.test.overide;
public class CustomString {
private String value;
public CustomString() {
this("");
}
public CustomString(String value) {
this.value = value;
}
@Override
public int hashCode() {
// return super.hashCode();
return 1;
}
}
再看结果:
- a.equals(b) == true
- c.equals(d) == false
- set.size() == 3
- com.darren.test.overide.CustomString@1
- com.darren.test.overide.CustomString@1
- A
a.equals(b) == true
c.equals(d) == false
set.size() == 3
com.darren.test.overide.CustomString@1
com.darren.test.overide.CustomString@1
A
只重写hashCode方法,不重写equals方法也不行
最后再改一改
- package com.darren.test.overide;
-
- public class CustomString {
- private String value;
-
- public CustomString() {
- this("");
- }
-
- public CustomString(String value) {
- this.value = value;
- }
-
- @Override
- public boolean equals(Object obj) {
- if (this == obj) {
-
- return true;
- } else if (obj instanceof CustomString) {
- CustomString customString = (CustomString) obj;
-
- return customString.value.equals(value);
- } else {
-
- return false;
- }
- }
-
- @Override
- public int hashCode() {
-
- return 1;
- }
- }
package com.darren.test.overide;
public class CustomString {
private String value;
public CustomString() {
this("");
}
public CustomString(String value) {
this.value = value;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
} else if (obj instanceof CustomString) {
CustomString customString = (CustomString) obj;
return customString.value.equals(value);
} else {
return false;
}
}
@Override
public int hashCode() {
// return super.hashCode();
return 1;
}
}
最后结果:
- a.equals(b) == true
- c.equals(d) == true
- set.size() == 2
- com.darren.test.overide.CustomString@1
- A
a.equals(b) == true
c.equals(d) == true
set.size() == 2
com.darren.test.overide.CustomString@1
A
可以了,证明需要重写equals方法和hashCode方法,来看原理:
java.lnag.Object中对hashCode的约定:
1. 在一个应用程序执行期间,如果一个对象的equals方法做比较所用到的信息没有被修改的话,则对该对象调用hashCode方法多次,它必须始终如一地返回同一个整数。
2. 如果两个对象根据equals(Object o)方法是相等的,则调用这两个对象中任一对象的hashCode方法必须产生相同的整数结果。
3. 如果两个对象根据equals(Object o)方法是不相等的,则调用这两个对象中任一个对象的hashCode方法,不要求产生不同的整数结果。但如果能不同,则可能提高散列表的性能。
在HashSet中,基本的操作都是有HashMap底层实现的,因为HashSet底层是用HashMap存储数据的。当向HashSet中添加元素的时候,首先计算元素的hashcode值,然后用这个(元素的hashcode)%(HashMap集合的大小)+1计算出这个元素的存储位置,如果这个位置位空,就将元素添加进去;如果不为空,则用equals方法比较元素是否相等,相等就不添加,否则找一个空位添加。
如下是HashSet的部分源码:
- package java.util;
-
- public class HashSet<E> extends AbstractSet<E>
- implements Set<E>, Cloneable, java.io.Serializable
- {
- static final long serialVersionUID = -5024744406713321676L;
-
-
- private transient HashMap<E,Object> map;
-
-
- private static final Object PRESENT = new Object();
-
-
- public HashSet() {
- map = new HashMap<E,Object>();
- }
-
-
- public HashSet(Collection< extends E> c) {
- map = new HashMap<E,Object>(Math.max((int) (c.size()/.75f) + 1, 16));
- addAll(c);
- }
-
-
- public HashSet(int initialCapacity, float loadFactor) {
- map = new HashMap<E,Object>(initialCapacity, loadFactor);
- }
-
-
- public HashSet(int initialCapacity) {
- map = new HashMap<E,Object>(initialCapacity);
- }
-
-
- HashSet(int initialCapacity, float loadFactor, boolean dummy) {
- map = new LinkedHashMap<E,Object>(initialCapacity, loadFactor);
- }
-
-
- @Override
- public Iterator<E> iterator() {
- return map.keySet().iterator();
- }
-
-
- @Override
- public int size() {
- return map.size();
- }
-
-
- @Override
- public boolean isEmpty() {
- return map.isEmpty();
- }
-
-
- @Override
- public boolean contains(Object o) {
- return map.containsKey(o);
- }
-
-
- @Override
- public boolean add(E e) {
- return map.put(e, PRESENT)==null;
- }
-
-
- @Override
- public boolean remove(Object o) {
- return map.remove(o)==PRESENT;
- }
-
-
- @Override
- public void clear() {
- map.clear();
- }
- }
(转)JAVA HashSet 去除重复值原理
标签:容量 去除 介绍 否则 collect col this link clip
原文地址:https://www.cnblogs.com/Dar-/p/9054666.html