From 83bccab822f2019c1a7d21251630b84ee890abc7 Mon Sep 17 00:00:00 2001 From: zaker-666 <1075151259china@gmail.com> Date: Mon, 6 Jan 2020 22:32:24 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=BA=86=E6=B1=82?= =?UTF-8?q?=E4=BA=A4=E9=9B=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/com/github/hcsp/collection/MainTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/com/github/hcsp/collection/MainTest.java b/src/test/java/com/github/hcsp/collection/MainTest.java index 1697f32..e6e992a 100644 --- a/src/test/java/com/github/hcsp/collection/MainTest.java +++ b/src/test/java/com/github/hcsp/collection/MainTest.java @@ -3,6 +3,7 @@ import java.util.Arrays; import java.util.HashSet; import java.util.List; + import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; From f6b16203a47a3130e603141ab399bb7cf7b9e199 Mon Sep 17 00:00:00 2001 From: zaker-666 <1075151259china@gmail.com> Date: Tue, 7 Jan 2020 14:46:34 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=BA=86=E6=B1=82?= =?UTF-8?q?=E4=BA=A4=E9=9B=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/github/hcsp/collection/Main.java | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 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..07ee8bc 100644 --- a/src/main/java/com/github/hcsp/collection/Main.java +++ b/src/main/java/com/github/hcsp/collection/Main.java @@ -1,12 +1,16 @@ package com.github.hcsp.collection; -import java.util.Arrays; -import java.util.List; -import java.util.Set; +import java.util.*; +import java.util.stream.Collectors; public class Main { // 请编写一个方法,获得a和b集合中的公共元素。 - public static Set commonElementsIn(List a, List b) {} + public static Set commonElementsIn(List a, List b) { + ArrayList personsOne = new ArrayList<>(a); + ArrayList personsTwo = new ArrayList<>(b); + personsOne.retainAll(personsTwo); + return new HashSet(personsOne); + } // Person类,如果两个Person对象的name相等,则认为这两个对象相等。 public static class Person { @@ -23,11 +27,27 @@ public String getName() { public void setName(String name) { this.name = name; } + + @Override + public boolean equals(Object obj) { + if(obj instanceof Person) { + Person person = (Person) obj; + return this.getName().equals(person.getName()); + } + return false; + } + + @Override + public int hashCode() { + return name.hashCode(); + } } public static void main(String[] args) { List list1 = Arrays.asList(new Person("张学友"), new Person("周杰伦")); List list2 = Arrays.asList(new Person("周润发"), new Person("周杰伦")); + System.out.println(list1); + System.out.println(list2); System.out.println(commonElementsIn(list1, list2)); } } From b7ef4fc1121e3f592bf9a852c46363c31c9d32b8 Mon Sep 17 00:00:00 2001 From: zaker-666 <1075151259china@gmail.com> Date: Tue, 7 Jan 2020 14:46:56 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=BA=86=E6=B1=82?= =?UTF-8?q?=E4=BA=A4=E9=9B=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/github/hcsp/collection/Main.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/github/hcsp/collection/Main.java b/src/main/java/com/github/hcsp/collection/Main.java index 07ee8bc..2c27e95 100644 --- a/src/main/java/com/github/hcsp/collection/Main.java +++ b/src/main/java/com/github/hcsp/collection/Main.java @@ -9,7 +9,7 @@ public static Set commonElementsIn(List a, List b) { ArrayList personsOne = new ArrayList<>(a); ArrayList personsTwo = new ArrayList<>(b); personsOne.retainAll(personsTwo); - return new HashSet(personsOne); + return new HashSet<>(personsOne); } // Person类,如果两个Person对象的name相等,则认为这两个对象相等。 From 7d5d7801fa674b0908fb456aa66f875af3187b16 Mon Sep 17 00:00:00 2001 From: zaker-666 <1075151259china@gmail.com> Date: Tue, 7 Jan 2020 15:36:04 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E6=9B=B4=E6=94=B9=E4=BA=86=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/github/hcsp/collection/Main.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/github/hcsp/collection/Main.java b/src/main/java/com/github/hcsp/collection/Main.java index 2c27e95..a376317 100644 --- a/src/main/java/com/github/hcsp/collection/Main.java +++ b/src/main/java/com/github/hcsp/collection/Main.java @@ -1,7 +1,6 @@ package com.github.hcsp.collection; import java.util.*; -import java.util.stream.Collectors; public class Main { // 请编写一个方法,获得a和b集合中的公共元素。 From 8c0fc23ea1de9a8cf51d60b8be07a4764206983f Mon Sep 17 00:00:00 2001 From: zaker-666 <1075151259china@gmail.com> Date: Tue, 7 Jan 2020 15:44:08 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E6=9B=B4=E6=94=B9=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/github/hcsp/collection/Main.java | 3 ++- src/test/java/com/github/hcsp/collection/MainTest.java | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/github/hcsp/collection/Main.java b/src/main/java/com/github/hcsp/collection/Main.java index a376317..a7c0337 100644 --- a/src/main/java/com/github/hcsp/collection/Main.java +++ b/src/main/java/com/github/hcsp/collection/Main.java @@ -36,9 +36,10 @@ public boolean equals(Object obj) { return false; } + @Override public int hashCode() { - return name.hashCode(); + return Objects.hash(name); } } diff --git a/src/test/java/com/github/hcsp/collection/MainTest.java b/src/test/java/com/github/hcsp/collection/MainTest.java index e6e992a..271fbf9 100644 --- a/src/test/java/com/github/hcsp/collection/MainTest.java +++ b/src/test/java/com/github/hcsp/collection/MainTest.java @@ -1,12 +1,12 @@ package com.github.hcsp.collection; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + import java.util.Arrays; import java.util.HashSet; import java.util.List; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - public class MainTest { @Test public void test() {