【SpringBoot】List<实体类>如何去重

it2023-02-11  48

嵌套for循环去重

for (int i = projectList.size() - 1; i >= 0; i--) { for (int j = projectList.size() - 1; j >= 0; j--) { if (projectList.get(i).getId().equals(projectList.get(j).getId())) { projectList.remove(j); i-=1; } } }

根据equals方法去重

list

HashSet去重

/// 去重 Set<Project> userSet = new HashSet<>(projectFatherList); projectFatherList = new ArrayList<>(userSet);

stream流去重

projectList=projectList.stream().distinct().collect(Collectors.toList());

根据某个字段去重

projectList = projectList.stream().filter(distinctByKey((p) -> (p.getT_custId()))).collect(Collectors.toList());
最新回复(0)