From 8a40b0fcb35e80dba385f411943fb9e88e64f7e4 Mon Sep 17 00:00:00 2001 From: jlhde123 <472327024@qq.com> Date: Thu, 9 Mar 2017 13:24:26 +0800 Subject: [PATCH 1/2] =?UTF-8?q?hashset=20=E5=AD=A6=E4=B9=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/jlh/viewer/HashSetTestList.java | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 jlh/src/main/java/com/jlh/viewer/HashSetTestList.java diff --git a/jlh/src/main/java/com/jlh/viewer/HashSetTestList.java b/jlh/src/main/java/com/jlh/viewer/HashSetTestList.java new file mode 100644 index 0000000..c028b67 --- /dev/null +++ b/jlh/src/main/java/com/jlh/viewer/HashSetTestList.java @@ -0,0 +1,70 @@ +package com.jlh.viewer; + +import java.util.HashSet; + +/** + * Created by jlh + * On 2017/3/9 0009. + */ +public class HashSetTestList { + public static void main(String[] args) { + //通过hashmap 来实现,所以基本上和hashmap差不多 ,,每次add的时候其实就是调用了hashmap.put("key",PERCENT) PERCENT 是一个静态Object对象用于标识这个Key存在了 + HashSet hashSet = new HashSet<>(); + //hashset 判断key是否存在的标准是 equal 和hashcode 的相等与否 缺一不可 + hashSet.add(new Tuple("jlh","123")); + hashSet.add(new Tuple("jlh","123")); + hashSet.stream().forEach(System.out::println); + } + + public static class Tuple{ + private String name; + private String address; + + public Tuple(String name, String address) { + this.name = name; + this.address = address; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Tuple tuple = (Tuple) o; + + if (name != null ? !name.equals(tuple.name) : tuple.name != null) return false; + return address != null ? address.equals(tuple.address) : tuple.address == null; + } + + @Override + public int hashCode() { + int result = name != null ? name.hashCode() : 0; + result = 31 * result + (address != null ? address.hashCode() : 0); + return result; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + @Override + public String toString() { + return "Tuple{" + + "name='" + name + '\'' + + ", address='" + address + '\'' + + '}'; + } + } +} -- Gitee From 7c374ea6fb2b1f9460d786484bd4e8307dd0b17f Mon Sep 17 00:00:00 2001 From: jlhde123 <472327024@qq.com> Date: Thu, 9 Mar 2017 13:25:20 +0800 Subject: [PATCH 2/2] =?UTF-8?q?hashset=20=E5=AD=A6=E4=B9=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jlh/src/main/java/com/jlh/viewer/HashSetTestList.java | 1 + 1 file changed, 1 insertion(+) diff --git a/jlh/src/main/java/com/jlh/viewer/HashSetTestList.java b/jlh/src/main/java/com/jlh/viewer/HashSetTestList.java index c028b67..5a706d9 100644 --- a/jlh/src/main/java/com/jlh/viewer/HashSetTestList.java +++ b/jlh/src/main/java/com/jlh/viewer/HashSetTestList.java @@ -11,6 +11,7 @@ public class HashSetTestList { //通过hashmap 来实现,所以基本上和hashmap差不多 ,,每次add的时候其实就是调用了hashmap.put("key",PERCENT) PERCENT 是一个静态Object对象用于标识这个Key存在了 HashSet hashSet = new HashSet<>(); //hashset 判断key是否存在的标准是 equal 和hashcode 的相等与否 缺一不可 + // 因为hashmap找bucket的时候需要hash 然后找到bucket的时候找entry需要equal 所以两者都要 hashSet.add(new Tuple("jlh","123")); hashSet.add(new Tuple("jlh","123")); hashSet.stream().forEach(System.out::println); -- Gitee