创建测试对象DemoUser:
public class DemoUser {
private String name;
private String pwd;
private Integer age;
private Date birthDay;
//getter/setter...
}
main方法中创建数组,添加数组元素(略),获得stream
List list = new ArrayList();
//...添加DemoUser对象
//转化为流stream()
Stream stream = list.stream();
1、根据字段分组
a、根据pwd分组
//根据pwd分组,返回一个map,collect传入函数Collectors.groupingBy()
Map<String, List> map= stream.collect(Collectors.groupingBy(e -> { return e.getPwd();}));
b、根据自定义的key分组
Map<String, List> collect = stream.collect(Collectors.groupingBy(e -> **fetchGroupKey**(e)));
//定义key规则
String fetchGroupKey(DemoUser demoUser){
return demoUser.getName()+demoUser.getAge();
}
c、先根据age分组,再根据name分组
Map<Integer, Map<String, List>> collect = stream.collect(Collectors.groupingBy(e -> e.getAge(), Collectors.groupingBy(e -> e.getName())));
d、根据age分组,再统计每组数据总数
Map collect = stream.collect(Collectors.groupingBy(e -> e.getAge(), Collectors.counting()));
e、根据名称分组,再根据age求和
Map collect = stream.collect(Collectors.groupingBy(e -> e.getName(), Collectors.summarizingInt(e -> e.getAge())));
System.out.println();
2、根据字段排序
a、根据age从小到大排序
//使用
Collections.sort(list,(e1,e2)->e1.getAge()- e2.getAge());
//或者
list.sort(Comparator.comparing(DemoUser::getAge));
b、多条件组合排序:先根据age,再根据name和pwd排序
list.sort(Comparator.comparing(DemoUser::getAge).thenComparing(DemoUser::getName).thenComparing(DemoUser::getPwd));
c、根据汉字排序
Collections.sort(list, (e1, e2) -> Collator.getInstance(Locale.CHINESE).compare(e1.getName(), e2.getName()));
3、根据字段去重
a、根据名称去重
// 根据name去重
ArrayList collect = stream.collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet(Comparator.comparing(DemoUser::getName))), ArrayList::new)
);
b、多条件去重
// 根据name,age两个属性去重
List unique = stream.collect(
Collectors. collectingAndThen(Collectors.toCollection(() -> new TreeSet(Comparator.comparing(o -> o.getName() + ";" + o.getAge()))), ArrayList::new)
);
c、利用过滤去重
List collect = stream.
filter(distinctByKey(e -> e.getName())).
collect(Collectors.toList());
//distinctByKey
static Predicate distinctByKey(Function keyExtractor) {
Map seen = new ConcurrentHashMap();
return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}
4、根据条件过滤filter
a、过滤获取name为liming,并且age大于12岁的user
List collect = stream.
filter(e -> "liming".equals(e.getName()) && e.getAge() > 12).
collect(Collectors.toList());
5、stream().map归并
//a 将List通过map重新构造并返回List
List collectVo = stream.map(e -> {
DemoUserVo demoUserVo = new DemoUserVo();
demoUserVo.setName(e.getName());
demoUserVo.setAge(e.getAge());
return demoUserVo;
}).collect(Collectors.toList());
对于stream.map(),入参为:Function mapper
,而public interface Function
接口,参数T和R表示:
@param the type of the input to the function 表示函数的输入
@param the type of the result of the function 表示函数结果
则上面的代码也可以改写为:
//构建DemoUserVo
public static Function buildDemoUserVo(){
return e ->{
DemoUserVo demoUserVo = new DemoUserVo();
demoUserVo.setName(e.getName());
demoUserVo.setAge(e.getAge());
return demoUserVo;
};
}
//使用buildDemoUserVo
List collect = stream.map(buildDemoUserVo()).collect(Collectors.toList());