From 91d20bdcb9229d1b46ffce6e293e455f26c23237 Mon Sep 17 00:00:00 2001 From: weiranyi <168578203@qq.com> Date: Sun, 3 Jan 2021 16:43:53 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E9=87=8D=E5=86=99=E4=BA=86equals=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E5=92=8CHashCode=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/github/hcsp/collection/Main.java | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/github/hcsp/collection/Main.java b/src/main/java/com/github/hcsp/collection/Main.java index ae0f842..33b550f 100644 --- a/src/main/java/com/github/hcsp/collection/Main.java +++ b/src/main/java/com/github/hcsp/collection/Main.java @@ -1,12 +1,19 @@ package com.github.hcsp.collection; -import java.util.Arrays; +import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.Arrays; +import java.util.Objects; public class Main { // 请编写一个方法,获得a和b集合中的公共元素。 - public static Set commonElementsIn(List a, List b) {} + public static Set commonElementsIn(List a, List b) { + Set setA = new HashSet<>(a); + Set setB = new HashSet<>(b); + setA.retainAll(setB); + return setA; + } // Person类,如果两个Person对象的name相等,则认为这两个对象相等。 public static class Person { @@ -23,6 +30,19 @@ public String getName() { public void setName(String name) { this.name = name; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Person person = (Person) o; + return Objects.equals(name, person.name); + } + + @Override + public int hashCode() { + return Objects.hash(name); + } } public static void main(String[] args) { @@ -31,3 +51,4 @@ public static void main(String[] args) { System.out.println(commonElementsIn(list1, list2)); } } + From 72f77104e287d203e0567cd2e082b776f5cbc646 Mon Sep 17 00:00:00 2001 From: weiranyi <168578203@qq.com> Date: Sun, 3 Jan 2021 16:55:53 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E9=87=8D=E5=86=99=E4=BA=86equals=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E5=92=8CHashCode=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/github/hcsp/collection/Main.java | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/github/hcsp/collection/Main.java b/src/main/java/com/github/hcsp/collection/Main.java index 33b550f..79d5ec7 100644 --- a/src/main/java/com/github/hcsp/collection/Main.java +++ b/src/main/java/com/github/hcsp/collection/Main.java @@ -9,10 +9,11 @@ public class Main { // 请编写一个方法,获得a和b集合中的公共元素。 public static Set commonElementsIn(List a, List b) { - Set setA = new HashSet<>(a); - Set setB = new HashSet<>(b); - setA.retainAll(setB); - return setA; + Set set = new HashSet<>(); + set.addAll(a); + set.retainAll(b); + return set; + } // Person类,如果两个Person对象的name相等,则认为这两个对象相等。 @@ -33,8 +34,12 @@ public void setName(String name) { @Override public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + if (this == o){ + return true; + } + if (o == null || getClass() != o.getClass()){ + return false; + } Person person = (Person) o; return Objects.equals(name, person.name); } From 4fe9e871d9d74ab4f6d14413afbdd61cbcb6a711 Mon Sep 17 00:00:00 2001 From: weiranyi <168578203@qq.com> Date: Sun, 3 Jan 2021 16:57:57 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E9=87=8D=E5=86=99=E4=BA=86equals=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E5=92=8CHashCode=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/github/hcsp/collection/Main.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/github/hcsp/collection/Main.java b/src/main/java/com/github/hcsp/collection/Main.java index 79d5ec7..900e80c 100644 --- a/src/main/java/com/github/hcsp/collection/Main.java +++ b/src/main/java/com/github/hcsp/collection/Main.java @@ -34,10 +34,10 @@ public void setName(String name) { @Override public boolean equals(Object o) { - if (this == o){ + if (this == o) { return true; } - if (o == null || getClass() != o.getClass()){ + if (o == null || getClass() != o.getClass()) { return false; } Person person = (Person) o;