java - 从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}
解决方案
tl;博士
做一个Set
你的List
.
Set < Student > studentsDistinct = Set.copyOf( students ) ;
record
在 Java 16 及更高版本中将您的类定义为记录。编译器隐式创建构造函数、getter、equals
&hashCode
和toString
。
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]]
推荐阅读
- azure - 无法使用 datafactory 版本 1 将数据从制表符分隔到表中加载
- latex - 用 LaTeX 写这个符号
- mysql - 防止 MySQL 查询在 Apps 脚本中超时
- mysql - MySQL从满足条件的行中选择字段,并从上面的行中选择字段
- c# - 浏览器中带有 target='_blank' 的 GeckoFX 超链接
- ios - 如何将 UIButton 与自定义类一起使用?
- python-3.x - Telepot中的线程保存串行连接(Python)
- android - 无法在 android 上编译 tensorflowlite C++ API
- angularjs - 如何使用 AngularJS 服务依赖项对 Angular 服务进行单元测试
- c++ - 如何在不使用 & 的情况下传递函数