List集合中的对象判断某个字段是否重复
public static void main(String[] args) { //判断是否有重复的问题添加 List<HealthQuestionInfoReq> healthQuestionInfoReqs = new ArrayList<>(); HealthQuestionInfoReq healthQuestionInfoReq1 = new HealthQuestionInfoReq(); HealthQuestionInfoReq healthQuestionInfoReq2 = new HealthQuestionInfoReq(); healthQuestionInfoReq1.setQuestionName("123"); healthQuestionInfoReq2.setQuestionName("123"); healthQuestionInfoReqs.add(healthQuestionInfoReq1); healthQuestionInfoReqs.add(healthQuestionInfoReq2); HashSet<String> uidSet = new HashSet<>(); //这里泛型里面的类型和你需要判断是字段类型一致 healthQuestionInfoReqs.stream().forEach(t->{ uidSet.add(t.getQuestionName()); }); //如果为true说明不相等,如果为false则为相等 System.out.println( healthQuestionInfoReqs.size() == uidSet.size()); }
附上:List集合差集(基于java8新特性)
* 求List1中有的但是List2中没有的元素
public static List<String> subList2(List<String> list1, List<String> list2) { Map<String, String> tempMap = list2.parallelStream().collect(Collectors.toMap(Function.identity(), Function.identity(), (oldData, newData) -> newData)); return list1.parallelStream().filter(str -> { return !tempMap.containsKey(str); }).collect(Collectors.toList()); }
本文来自博客园,作者:迷糊桃,转载请注明原文链接:https://www.cnblogs.com/mihutao/p/15465239.html