Java基础笔记-集合-List根据对象的属性去重-《Java笔记》

admin 2025-10-19 01:31:51 编程 来源:ZONE.CI 全球网 0 阅读模式

Java List

一、去除List中重复的String

  1. public List<String> removeStringListDupli(List<String> stringList) {
  2. Set<String> set = new LinkedHashSet<>();
  3. set.addAll(stringList);
  4. stringList.clear();
  5. stringList.addAll(set);
  6. return stringList;
  7. }

或使用Java8的写法:

  1. List<String> unique = list.stream().distinct().collect(Collectors.toList());

二、List中对象去重

比如现在有一个 Person类:

  1. public class Person {
  2. private Long id;
  3. private String name;
  4. public Person(Long id, String name) {
  5. this.id = id;
  6. this.name = name;
  7. }
  8. public Long getId() {
  9. return id;
  10. }
  11. public void setId(Long id) {
  12. this.id = id;
  13. }
  14. public String getName() {
  15. return name;
  16. }
  17. public void setName(String name) {
  18. this.name = name;
  19. }
  20. @Override
  21. public String toString() {
  22. return "Person{" +
  23. "id=" + id +
  24. ", name='" + name + '\'' +
  25. '}';
  26. }
  27. }

重写Person对象的equals()方法和hashCode()方法:

  1. @Override
  2. public boolean equals(Object o) {
  3. if (this == o) return true;
  4. if (o == null || getClass() != o.getClass()) return false;
  5. Person person = (Person) o;
  6. if (!id.equals(person.id)) return false;
  7. return name.equals(person.name);
  8. }
  9. @Override
  10. public int hashCode() {
  11. int result = id.hashCode();
  12. result = 31 * result + name.hashCode();
  13. return result;
  14. }

下面对象去重的代码:

  1. Person p1 = new Person(1l, "jack");
  2. Person p2 = new Person(3l, "jack chou");
  3. Person p3 = new Person(2l, "tom");
  4. Person p4 = new Person(4l, "hanson");
  5. Person p5 = new Person(5l, "胶布虫");
  6. List<Person> persons = Arrays.asList(p1, p2, p3, p4, p5, p5, p1, p2, p2);
  7. List<Person> personList = new ArrayList<>();
  8. // 去重
  9. persons.stream().forEach(
  10. p -> {
  11. if (!personList.contains(p)) {
  12. personList.add(p);
  13. }
  14. }
  15. );
  16. System.out.println(personList);

List 的contains()方法底层实现使用对象的equals方法去比较的,其实重写equals()就好,但重写了equals最好将hashCode也重写了。

三、根据对象的属性去重

下面要根据Person对象的id去重,那该怎么做呢?写一个方法吧:

  1. public static List<Person> removeDupliById(List<Person> persons) {
  2. Set<Person> personSet = new TreeSet<>((o1, o2) -> o1.getId().compareTo(o2.getId()));
  3. personSet.addAll(persons);
  4. return new ArrayList<>(personSet);
  5. }

通过Comparator比较器,比较对象属性,相同就返回0,达到过滤的目的。再来看比较炫酷的Java8写法:

  1. import static java.util.Comparator.comparingLong;
  2. import static java.util.stream.Collectors.collectingAndThen;
  3. import static java.util.stream.Collectors.toCollection;
  4. // 根据id去重
  5. List<Person> unique = persons.stream().collect(
  6. collectingAndThen(
  7. toCollection(() -> new TreeSet<>(comparingLong(Person::getId))), ArrayList::new)
  8. );

还有一种写法:

  1. public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
  2. Map<Object, Boolean> map = new ConcurrentHashMap<>();
  3. return t -> map.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
  4. }
  5. // remove duplicate
  6. persons.stream().filter(distinctByKey(p -> p.getId())).forEach(p -> System.out.println(p));
以太坊cppgolang区别 编程

以太坊cppgolang区别

以太坊是一种去中心化的开源平台,它采用智能合约技术,旨在构建和运行不受干扰的分布式应用程序。作为目前最受欢迎的区块链平台之一,以太坊提供了多种编程语言的支持,其
progolang 编程

progolang

Go语言(Golang)是由Google开发的一门静态类型编程语言。作为一名专业的Golang开发者,我深知这门语言的优势和特点。在本文中,我将介绍Golang
golangn个发送者 编程

golangn个发送者

Golang是一种开源的编程语言,由Google团队开发,旨在提高程序的并发性和简化软件开发过程。在Go语言中,有时需要向多个接收者发送信息。本文将介绍如何在G
golang技能图谱 编程

golang技能图谱

从互联网行业的快速发展到人工智能技术的日益成熟,各种编程语言也应运而生。而在这众多的编程语言中,Golang(即Go)作为一门强大且高效的开发语言备受关注。Go
评论:0   参与:  6