List列表运用Java8的stream流按某字段去重

问题
项目中经常会遇到列表去重的问题,一般可使用Java8的stream()流提供的distinct()方法:list.stream().distinct()。 list的类型为List、List,list里的元素为简单包装类型。 或者List,其中Xxx为自定义对象类型,重写equals和hashCode方法,可根据业务情况来实现,如id相同即认为对象相等。 有时会遇到这种情况,需要对按对象里的某字段来去重。
例如:
@NoArgsConstructor
@AllArgsConstructor
@Data
class Book {

public static Book of(Long id, String name, String createTime) {
return new Book(id, name, Date.from(LocalDateTime.parse(createTime, DateTimeFormatter.ofPattern("yyyy-MM-d

List列表运用Java8的stream流按某字段去重最先出现在Python成神之路

版权声明:
作者:玉兰
链接:https://www.techfm.club/p/25501.html
来源:TechFM
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
< <上一篇
下一篇>>