0
点赞
收藏
分享

微信扫一扫

去重列表对象(根据对象字段)

落拓尘嚣 2022-07-04 阅读 127

一 实体类



@Data
@AllArgsConstructor
public class ChannalRobotDTO {

/**
* 机器人配置id
*/
private String robotId;

/**
* 城市code
*/
private String cityCode;

/**
* 保司code
*/
private String companyCode;

/**
* 机器人配置名
*/
private String name;

/**
* 重写equals和hashCode完成散列存储去重
*/
public boolean equals(Object obj){
ChannalRobotDTO c=(ChannalRobotDTO) obj;
return companyCode.equals(c.companyCode);
}

public int hashCode(){
String str=companyCode;
return str.hashCode();
}
}


二 测试



public class RemovalDemo {
public static void main(String[] args) {
List<ChannalRobotDTO> list=new ArrayList<>();
list.add(new ChannalRobotDTO("1111","1001","TTC","建行"));
list.add(new ChannalRobotDTO("2222","1002","MIT","建行"));
list.add(new ChannalRobotDTO("3333","1003","TTC","建行"));
list.add(new ChannalRobotDTO("4444","1004","MIT","建行"));
list.add(new ChannalRobotDTO("5555","1005","TTC","建行"));
/**
* 对象去重(根据companyCode去重)
*/
Set<ChannalRobotDTO> set=new HashSet<>(list);
List<ChannalRobotDTO> dtoList=new ArrayList<>(set);
System.out.println(dtoList);
/**
* 去重结果
* [ChannalRobotDTO(robotId=1111, cityCode=1001, companyCode=TTC, name=建行),
* ChannalRobotDTO(robotId=2222, cityCode=1002, companyCode=MIT, name=建行)]
*/
}
}



举报

相关推荐

0 条评论