public class NoticeVO{
private String id;
private String summary;
private String userId;
private String description;
private Long startTime;
private Long endTime;
private Integer type;
private List<String> attendees;
private List<StaffVO> attendeeVOList;
private List<TagVO> tagList;
private Integer commentNum;
private Integer attachmentNum;
private List<Attachment> attachmentList;
private Boolean wholeDay;
private Integer scheduleType;
private Boolean listName;
private String appId;
private AppVo appInfo;
private Integer status;
private Long finishTime;
private Long gmtCreate;
}
List<NoticeVO> noticeList = Lists.newArrayList();
//单list遍历
noticeList.stream().forEach(n ->{
System.out.println(n.getAppId());
});
noticeList.stream().map(n ->{
System.out.println(n.getAppId());
return null;
});
//双重list遍历
noticeList.stream().map(n -> {
return n.getTagList().stream().map(tag -> {
System.out.println(n.getAppId() + " tag:" + tag.getId());
return null;
});
});
//提取对象属性集合
List<String> collect = noticeList.stream().map(NoticeVO::getAppId);
//如果要提取的属性是List集合,可以这样
List<String> allTags = Lists.newArrayList();
noticeList.stream().forEach(s -> allTags.addAll(s.getTagList()));
//转Map,以id为key
Map<String, NoticeVO> collectMap = noticeList.stream()
.collect(Collectors
.toMap(NoticeVO::getId, Function.identity()));
//过滤
noticeList = noticeList.stream()
.filter(n -> n.getFinishTime() > System.currentTimeMillis()
&& "1".equals(n.getType())).collect(
Collectors.toList());
noticeList.stream().filter(n ->{
if (n.getFinishTime() > 13110192837210L){
return true;
}else {
return false;
}
}).collect(Collectors.toList());
//分组按type分组
Map<Integer, List<NoticeVO>> typeList = noticeList.stream()
.collect(Collectors.groupingBy(comment -> comment.getType()));
//多重分组,先根据type分组再根据appId分组
noticeList.stream().collect(Collectors.groupingBy(NoticeVO::getType,
Collectors.groupingBy(NoticeVO::getAppId)));
//排序
//排序:开始时间顺序,开始时间为空,结束时间顺序,都为空创建时间倒序
futureList = futureList.stream().sorted(Comparator
.comparing(NoticeVO::getStartTime,
Comparator.nullsLast(Long::compareTo))
.thenComparing(NoticeVO::getEndTime,
Comparator.nullsLast(Long::compareTo))
.thenComparing(NoticeVO::getGmtCreate,
Comparator.nullsLast(Comparator.reverseOrder()))
//完成时间倒序,结束时间倒序,null值往后排
noticeList = noticeList.stream()
.sorted(Comparator.comparing(NoticeVO::getFinishTime,
Comparator.nullsLast(Comparator.reverseOrder()))
.thenComparing(NoticeVO::getEndTime,
Comparator.nullsLast(Comparator.reverseOrder()))
).collect(Collectors.toList());
//startTime顺序,null值后排
todayList = todayList.stream().sorted(Comparator
.comparing(NoticeVO::getStartTime,
Comparator.nullsLast(Long::compareTo))
).collect(Collectors.toList());
//根据属性去重
public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
Map<Object,Boolean> seen = new ConcurrentHashMap<>();
return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}
//使用-根据appId去重
List<NoticeVO> collect = noticeList.stream()
.filter(distinctByKey(c -> c.getAppId)).collect(Collectors.toList())
//最大完成时间
Long maxFinished = noticeList.stream().map(NoticeVO::getFinishTime)
.collect(Collectors.maxBy(Long::compare)).get();
java8 stream流使用集锦
于 2021-08-30 11:24:07 首次发布