Steam()是jdk1.8新增的一个对集合操作的类,这里的Stream和I/O流不同,它更像具有Iterable的集合类,但行为和集合类又有所不同。
源码介绍
支持顺序和并行聚合的元素序列
操作。下面的示例演示了使用
int sum=widgets.stream()
.filter(w->w.getColor()=红色)
.mapToInt(w->w.getWeight())
.sum();
}
Collection.stream()创建顺序流,
Collection。parallelStream()创建并行流;
一:构造流的几种常见方法
//1.第一种,Steam.of(T... values)
Stream stream = Stream.of("a1", "b1", "c1");
// 2.list.steam()使用较多
List<User) list = new ArrayList()
list.steam()
二.常用方法,
//filter()-过滤,sorted()-排序,map()-执行函数,collect()-返回一个集合
List<String> list2 = list.parallelStream().filter(t -> t.getValue() > 0)
.sorted(Comparator.comparing(Matter::getValue).reversed())
.map(Matter::getName)
.collect(Collectors.toList());
3.List转map
//查询自己签收的图片
pubic void test(){
List<Photos> photosList = photosDao.findByEnquiry_ReciverAndStateOrderByCreatimeDesc(Long.valueOf(userId), 0);
/**
* 按日期分组
*/
Map<String,List<Photos>> map= photosList.parallelStream().collect(Collectors.groupingBy(this::fetchGroupKey));
}
/**
* 日期是Date类型这里需要转化为String
*/
private String fetchGroupKey(Photos photos){
return photos.getCreatime().toString();
}