lambda 各種操作list 合集
使用hibernate validator出現(xiàn)上面的錯誤齐苛, 需要 注意
@NotNull 和 @NotEmpty 和@NotBlank 區(qū)別
@NotEmpty 用在集合類上面
@NotBlank 用在String上面
@NotNull 用在基本類型上
在枚舉類上不要加非空注解
日期排序
升序
msgList.sort((Message m1, Message m2) -> m2.getSendDate().compareTo(m1.getSendDate()));
降序
msgList.sort(Comparator.comparing(Message::getSendDate, Comparator.nullsLast(Date::compareTo)));
交集剿骨、并集静陈、差集
// 交集
List<String> intersection = list1.stream().filter(item -> list2.contains(item)).collect(toList());
System.out.println("---得到交集 intersection---");
intersection.parallelStream().forEach(System.out :: println);
// 差集 (list1 - list2)
List<String> reduce1 = list1.stream().filter(item -> !list2.contains(item)).collect(toList()); System.out.println("---得到差集 reduce1 (list1 - list2)---");
reduce1.parallelStream().forEach(System.out :: println);
// 差集 (list2 - list1)
List<String> reduce2 = list2.stream().filter(item -> !list1.contains(item)).collect(toList());
System.out.println("---得到差集 reduce2 (list2 - list1)---");
reduce2.parallelStream().forEach(System.out :: println);
// 并集
List<String> listAll = list1.parallelStream().collect(toList());
List<String> listAll2 = list2.parallelStream().collect(toList());
listAll.addAll(listAll2);
System.out.println("---得到并集 listAll---");
listAll.parallelStream().forEach(System.out :: println);
// 去重并集
List<String> listAllDistinct = listAll.stream().distinct().collect(toList());
System.out.println("---得到去重并集 listAllDistinct---"); listAllDistinct.parallelStream().forEach(System.out :: println);
System.out.println("---原來的List1---");
list1.parallelStream().forEach(System.out :: println);
System.out.println("---原來的List2---");
list2.parallelStream().forEach(System.out :: println);
// 一般有filter 操作時蓬网,不用并行流parallelStream ,如果用的話可能會導(dǎo)致線程安全問題
id相同賦值
List<UserCourseKnowledgeDetailVO> finalList = new ArrayList<>();
if(CollectionUtils.isNotEmpty(validityList)){
validityList.forEach(operateCourseKnowledgeUserValidity->
userCourseKnowledgeDetailVOList.stream()
.filter(userCourseKnowledgeDetailVO->userCourseKnowledgeDetailVO.getId().equals(operateCourseKnowledgeUserValidity.getKnowledgeId()))
.forEach(userCourseKnowledgeDetailVO-> userCourseKnowledgeDetailVO.setLastLearnTime(operateCourseKnowledgeUserValidity.getUpdateTime()))
);
排除自己
排除自己
list = list.stream().filters -> !"a".equals(s)).collect(Collectors.toList());
return Optional.ofNullable(matchRole).map(MatchRole::getInitScore).orElse(null);
MAP TO LONG
String ids= "1,2,3,4,5,6";
List<Long> listIds = Arrays.asList(ids.split(",")).stream().map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
System.out.println(Arrays.toString(listIds .toArray()));//[1,2,3,3,4,5,6]
去重
List<String> dataList = list.stream().distinct().collect(Collectors.toList());
//根據(jù)id去重
personList = personList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(
// 利用 TreeSet 的排序去重構(gòu)造函數(shù)來達(dá)到去重元素的目的
// 根據(jù)firstName去重
() -> new TreeSet<>(Comparator.comparing(Person::getName))), ArrayList::new));
取幾條排序
//取學(xué)習(xí)時長最大的兩條 從高到低排序
List<OperateCourseKnowledgeUserValidity> topTwoList = list.stream()
.sorted(Comparator.comparing(OperateCourseKnowledgeUserValidity::getStudyTimes).reversed())
.limit(2)
.collect(Collectors.toList());
分組院溺,遍歷分組
//分組
Map<String, List<User>> groupBySex = userList.stream().collect(Collectors.groupingBy(User::getSex));
//遍歷分組
for (Map.Entry<String, List<User>> entryUser : groupBySex.entrySet()) {
String key = entryUser.getKey();
List<User> entryUserList = entryUser.getValue();
}
合并userId 相同的courseNum
/**
* 合并userId 相同的courseNum
* @param oldList
* @return
*/
public static List<UserCourseTimeStatisVO> getNewList(List<UserCourseTimeStatisVO> oldList) {
HashMap<Long, UserCourseTimeStatisVO> tempMap = new HashMap<Long, UserCourseTimeStatisVO>();
// 去掉重復(fù)的key
for (UserCourseTimeStatisVO userCourseTimeStatisVO : oldList) {
Long userId = userCourseTimeStatisVO.getUserId();
// containsKey(Object key)該方法判斷Map集合中是否包含指定的鍵名楣嘁,如果包含返回true,不包含返回false
// containsValue(Object value)該方法判斷Map集合中是否包含指定的鍵值珍逸,如果包含返回true逐虚,不包含返回false
if (tempMap.containsKey(userId)) {
UserCourseTimeStatisVO newList = new UserCourseTimeStatisVO();
newList.setUserId(userId);
// 合并相同key的value
newList.setCourseNum(tempMap.get(userId).getCourseNum() + userCourseTimeStatisVO.getCourseNum());
// HashMap不允許key重復(fù),當(dāng)有key重復(fù)時谆膳,前面key對應(yīng)的value值會被覆蓋
tempMap.put(userId, newList);
} else {
tempMap.put(userId, userCourseTimeStatisVO);
}
}
List<UserCourseTimeStatisVO> newList = new ArrayList<UserCourseTimeStatisVO>();
Iterator iter = tempMap.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
UserCourseTimeStatisVO newPerson = (UserCourseTimeStatisVO) entry.getValue();
newList.add(newPerson);
}
return newList;
}
獲取屬性
List<Integer> idList = list1.stream().map(User::getId).collect(Collectors.toList());
List<User> userList1 = list1.stream().filter(user2 -> idList.contains(user2.getId())).collect(Collectors.toList());