根据某一字段去重

时间:2024-04-15 09:19:36

1.实体类

package com.abc.plan.domain;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;import lombok.*;
import java.util.Date;

@TableName(value = "plan_task")
@Data
@Builder
@ToString(callSuper = true)
@NoArgsConstructor
@AllArgsConstructor
public class PlanTask {
    private static final long serialVersionUID = 1L;

    @TableId(value = "id",type = IdType.AUTO)
    private Long id;
    @Excel(name = "名称")
    private String name;
    @Excel(name = "类型")
    private String type;
}

2.去重

PlanTask task = PlanTask.builder().id(1).name(“abc”).type(1).build();
PlanTask task = PlanTask.builder().id(2).name(“def”).type(2).build();
PlanTask task = PlanTask.builder().id(1).name(“eee”).type(1).build();
list = list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(PlanTask::getId))), ArrayList::new));