首页 > 解决方案 > 从arraylist中删除重复的对象元素

问题描述

我有一个名为 studentList 的数组列表,它在列表中有一些重复的学生元素。如何删除重复元素?

studentList: 
Student{id=S101312, name='Alex Mike', birthdate=10/13/1998}
Student{id=S102732, name='Mark Duong', birthdate=8/28/2001}
Student{id=S103723, name='Hai Hoang Vu', birthdate=4/25/2000}
Student{id=S101312, name='Alex Mike', birthdate=10/13/1998}

标签: java

解决方案


tl;博士

做一个Set你的List.

Set < Student > studentsDistinct = Set.copyOf( students ) ;

record

在 Java 16 及更高版本中将您的类定义为记录。编译器隐式创建构造函数、getter、equals&hashCodetoString

public record Student ( String id , String name , LocalDate birthDate ) {}

equals方法和可能的hashCode方法是比较对象和检测重复项所必需的。

class

如果使用常规类而不是记录,您将需要编写自己的这两种方法的覆盖实现。搜索 Stack Overflow 以了解更多信息;这已经被讨论过很多次了。

final class Student
{
    private final String id;
    private final String name;
    private final LocalDate birthDate;

    Student ( String id , String name , LocalDate birthDate )
    {
        this.id = id;
        this.name = name;
        this.birthDate = birthDate;
    }

    public String id ( ) { return id; }

    public String name ( ) { return name; }

    public LocalDate birthDate ( ) { return birthDate; }

    @Override
    public boolean equals ( Object obj )
    {
        if ( obj == this ) return true;
        if ( obj == null || obj.getClass() != this.getClass() ) return false;
        var that = ( Student ) obj;
        return Objects.equals( this.id , that.id ) &&
                Objects.equals( this.name , that.name ) &&
                Objects.equals( this.birthDate , that.birthDate );
    }

    @Override
    public int hashCode ( )
    {
        return Objects.hash( id , name , birthDate );
    }

    @Override
    public String toString ( )
    {
        return "Student[" +
                "id=" + id + ", " +
                "name=" + name + ", " +
                "birthDate=" + birthDate + ']';
    }
}

List

列出你的清单。

List<Student> students = List.of(
    new Student ( "S101312" , "Alex Mike" , LocalDate.of( 1998, 10 , 13 ) , 
    new Student ( "S102732" , "Mark Duong" , LocalDate.of( 2001, 8 , 28 ) , 
    new Student ( "S103723" , "Hai Hoang Vu" , LocalDate.of( 2000, 4 , 25 ) , 
    new Student ( "S101312" , "Alex Mike" , LocalDate.of( 1998, 10 , 13 ) 
);

Set

要消除重复项,请制作一个Set. 根据定义,集合不允许重复。

Set < Student > studentsDistinct = Set.copyOf( students ) ;

示例代码

把这段代码放在一起。

record Student(String id , String name , LocalDate birthDate)
{
}

List < Student > students = List.of(
        new Student( "S101312" , "Alex Mike" , LocalDate.of( 1998 , 10 , 13 ) ) ,
        new Student( "S102732" , "Mark Duong" , LocalDate.of( 2001 , 8 , 28 ) ) ,
        new Student( "S103723" , "Hai Hoang Vu" , LocalDate.of( 2000 , 4 , 25 ) ) ,
        new Student( "S101312" , "Alex Mike" , LocalDate.of( 1998 , 10 , 13 ) )
);

Set < Student > studentsDistinct = Set.copyOf( students );

System.out.println( "students = " + students );
System.out.println( "studentsDistinct = " + studentsDistinct );

跑的时候。

students = [Student[id=S101312, name=Alex Mike, birthDate=1998-10-13], Student[id=S102732, name=Mark Duong, birthDate=2001-08-28], Student[id=S103723, name=Hai Hoang Vu, birthDate=2000-04-25], Student[id=S101312, name=Alex Mike, birthDate=1998-10-13]]
studentsDistinct = [Student[id=S103723, name=Hai Hoang Vu, birthDate=2000-04-25], Student[id=S101312, name=Alex Mike, birthDate=1998-10-13], Student[id=S102732, name=Mark Duong, birthDate=2001-08-28]]

推荐阅读