POJO类
class FriendInfo {
private long id;
private String name;
private Integer age;
private String deptId;
public String getDeptId() { return deptId; }
public void setDeptId(String deptId) { this.deptId = deptId; }
public long getId() { return id; }
public void setId(long id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public Integer getAge() { return age; }
public void setAge(Integer age) { this.age = age; }
public FriendInfo(long id, String name, Integer age, String deptId) {
this.id = id;
this.name = name;
this.age = age;
this.deptId = deptId;
}
}
一些Stream操作
class Streamtest{
public static void main(String[] args) {
FriendInfo daxiong = new FriendInfo(123123231, "daxiong", 11, "1");
FriendInfo wangzai = new FriendInfo(123124122, "wangzai", 16, "1");
FriendInfo panghu = new FriendInfo(1231234112, "panghu", 10, "2");
List<FriendInfo> friendInfoList = new ArrayList<>();
friendInfoList.add(wangzai);
friendInfoList.add(daxiong);
friendInfoList.add(panghu);
//Collect收集器,前一个数据获取,后面还可以跟一个下游收集器,可以进行一些处理,下游还能有下游.
//对比谁的年龄最大,先收集age,下游处理取最大
Integer c = friendInfoList.stream().collect(Collectors.mapping(FriendInfo::getAge, Collectors.reducing((s1, s2) -> s1 > s2 ? s1 : s2))).get();
//名字长度累加 ,join也行
Object s = friendInfoList.stream().collect(Collectors.mapping(FriendInfo::getName , Collectors.toList() )).stream().reduce( (x,y) -> x+y).get().length();
System.out.println(s);
//过滤条件:age>10 的留下
//结果:wangzai ,daxiong
friendInfoList.stream().filter(n -> n.getAge() > 10).collect(Collectors.toList()).forEach(n -> System.out.println(n.getName()));
//类型转换为Int
// 结果:16 ,11 ,10
friendInfoList.stream().mapToInt(FriendInfo::getAge).boxed().collect(Collectors.toList()).forEach(System.out::println);
//升序排序
//结果:panghu ,daxiong, wangzai
friendInfoList.stream().sorted(Comparator.comparing(FriendInfo::getAge)).collect(Collectors.toList()).forEach(n -> System.out.println(n.getName()));
//collect()生成Map
Map<Long, FriendInfo> friendInfoMap = new HashMap<>();
friendInfoMap = friendInfoList.stream().collect(Collectors.toMap(n -> n.getId(), n -> n));
//结果:{1231234112=com.wangzai.servicezuul.FriendInfo@224aed64, 123123231=com.wangzai.servicezuul.FriendInfo@c39f790, 123124122=com.wangzai.servicezuul.FriendInfo@71e7a66b}
System.out.println(friendInfoMap);
//Group分组,以deptId为键,FriendInfo为值,分组
//{1=[com.wangzai.servicezuul.FriendInfo@71e7a66b, com.wangzai.servicezuul.FriendInfo@c39f790], 2=[com.wangzai.servicezuul.FriendInfo@224aed64]}
Map<String, List<FriendInfo>> deptToInfo = friendInfoList.stream().collect(Collectors.groupingBy(FriendInfo::getDeptId));
System.out.println(deptToInfo);
//Group分组数量统计
Map<String, Long> deptToNumber = friendInfoList.stream().collect(Collectors.groupingBy(FriendInfo::getDeptId, Collectors.counting()));
System.out.println(deptToNumber);
//Group分组,deptId为键,name为值
Map<String, List<String>> deptToName = friendInfoList.stream().collect(Collectors.groupingBy(FriendInfo::getDeptId, Collectors.mapping(FriendInfo::getName, Collectors.toList())));
System.out.println(deptToName);
//字符串拼接 name拼接起来
String nameJoin = friendInfoList.stream().collect(Collectors.mapping(FriendInfo::getName, Collectors.joining()));
System.out.println(nameJoin);
}
}
网友评论