删除列表 Java 中的重复对象

我有课:


class MyClass {

    String s1;

    String s2;

}

列表对象包括


[{s1: "a", s2: "b"}, {s1: "a", s2: "c"}, {s1: "a", s2: "b"}, {s1: "a", s2:“d”}]


我想删除对象重复的 2 个属性 s1 和 s2


预期结果:


[{s1: "a", s2: "b"}, {s1: "a", s2: "c"}, {s1: "a", s2: "d"}]


如果能用Java 8更好


帮我 :(


九州编程
浏览 146回答 3
3回答

潇湘沐

首先像这样覆盖equals和hashCode方法MyClass,@Overridepublic int hashCode() {&nbsp; &nbsp; return 31 * s1.hashCode() + s2.hashCode();}@Overridepublic boolean equals(Object obj) {&nbsp; &nbsp; return obj instanceof MyClass && ((MyClass) obj).getS1().equals(getS1())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; && ((MyClass) obj).getS2().equals(getS2());}那么消除重复的最简单方法是使用Set.new HashSet<>(instances)

慕标琳琳

实现 hashcode() 或 equals() 不是必须的。这是一种方法:Map<List<String>, MyClass> map = new HashMap<>();List<MyClass> distinctList = myCollection.stream()&nbsp; &nbsp; .collect(Collectors.toMap(c -> Arrays.asList(c.s1, c.s2), Function.identity(), (x,y)->x))&nbsp; &nbsp; .values()&nbsp; &nbsp; .stream()&nbsp; &nbsp; .collect(Collectors.toList());

手掌心

您需要覆盖equal和hash code函数,然后使用java 8的Streaming API的distinct函数。import java.util.ArrayList;import java.util.List;import java.util.Objects;import java.util.stream.Collectors;public class MyClass {&nbsp; private String s1;&nbsp; private String s2;&nbsp; public MyClass() {&nbsp; }&nbsp; public MyClass(String s1, String s2) {&nbsp; &nbsp; this.s1 = s1;&nbsp; &nbsp; this.s2 = s2;&nbsp; }&nbsp; public String getS1() {&nbsp; &nbsp; return s1;&nbsp; }&nbsp; public MyClass setS1(String s1) {&nbsp; &nbsp; this.s1 = s1;&nbsp; &nbsp; return this;&nbsp; }&nbsp; public String getS2() {&nbsp; &nbsp; return s2;&nbsp; }&nbsp; public MyClass setS2(String s2) {&nbsp; &nbsp; this.s2 = s2;&nbsp; &nbsp; return this;&nbsp; }&nbsp; @Override&nbsp; public boolean equals(Object o) {&nbsp; &nbsp; if (this == o) {&nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; }&nbsp; &nbsp; if (o == null || getClass() != o.getClass()) {&nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }&nbsp; &nbsp; MyClass myClass = (MyClass) o;&nbsp; &nbsp; return Objects.equals(getS1(), myClass.getS1()) &&&nbsp; &nbsp; &nbsp; &nbsp; Objects.equals(getS2(), myClass.getS2());&nbsp; }&nbsp; @Override&nbsp; public int hashCode() {&nbsp; &nbsp; return Objects.hash(getS1(), getS2());&nbsp; }&nbsp; @Override&nbsp; public String toString() {&nbsp; &nbsp; return "MyClass{" + "s1='" + s1 + '\'' + ", s2='" + s2 + '\'' + '}';&nbsp; }&nbsp; public static void main(String[] args) {&nbsp; &nbsp; List<MyClass> list = new ArrayList<>();&nbsp; &nbsp; list.add(new MyClass("a", "b"));&nbsp; &nbsp; list.add(new MyClass("a", "c"));&nbsp; &nbsp; list.add(new MyClass("a", "b"));&nbsp; &nbsp; list.add(new MyClass("a", "d"));&nbsp; &nbsp; System.out.println("Original size is : " + list.size()); // Size is 4&nbsp; &nbsp; list.forEach(System.out::println);&nbsp; &nbsp; //Now remove duplicate&nbsp; &nbsp; List<MyClass> expected = list.stream().distinct().collect(Collectors.toList());&nbsp; &nbsp; System.out.println(&nbsp; &nbsp; &nbsp; &nbsp; "After remove duplication size is : " + expected.size()); // Here is your expected result size is 3&nbsp; &nbsp; expected.forEach(System.out::println);&nbsp; }}这是输出:原尺寸为:4我的类{s1='a', s2='b'}我的类{s1='a', s2='c'}我的类{s1='a', s2='b'}我的类{s1='a', s2='d'}删除重复后大小为:3我的类{s1='a', s2='b'}我的类{s1='a', s2='c'}我的类{s1='a', s2='d'}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java