过滤
//根据指定sn,过滤出符合的数据: List
List
//筛选出工资大于10000的职员
List
return item.getSalary().compareTo(new BigDecimal(10000)) > 0 && !item.getWorkType().equals("项目经理");
}).collect(Collectors.toList());
分组
// 按照sn分组: List
Map
//按照职员部分分组: List
Map
//多条件分组
Map
//按年龄分组,年龄相同的是一组
Map
//按年龄分组后按工资分组,多级分组
Map
return x.getSalary() > 3000 ? "高" : "低";
})));
// 分组排序 ,拿已经排好序的过来分组
LinkedHashMap
// 分组排序,集合没排序,我们自己按我们想要的排序
LinkedHashMap
分区
//List
//单层分区
Map
//分区 满足条件的一个区,不满足条件的一个区
Map
//多层分区
Map
拼接
//将某个字段,按照某个字符串拼接: List
String sns = deviceMapList.stream()
.map((m)->m.get("sn")+"").collect(Collectors.joining(","));
//使用场景很多,在sql里面用于组织in的值.比如:
SELECT sn,time,value FROM electric_real_time WHERE FIND_IN_SET(sn,?)
List
List
//连接所有内容
String str = strs.stream().collect(Collectors.joining());
System.out.println(str);
//输出:abcd
//连接所有内容,中间加一个逗号隔开
String str1 = strs.stream().collect(Collectors.joining(","));
System.out.println(str1);
//输出:a,b,cd
//连接所有内容,中间加一个逗号隔开,两边加上括号
String str2 = strs.stream().collect(Collectors.joining(",","(",")"));
System.out.println(str2);
//输出:(a,b,cd)
list转map
// (k1,k2)->k2 避免键重复 k1-取第一个数据;k2-取最后一条数据
//key和value,都可以根据传入的值返回不同的Map
Map
//
Map
.collect(Collectors.toMap(i -> i.getEmpName() + i.getUnitName(), j -> j, (k1, k2) -> k1));
map转list
//在.map里面构造数据 return什么数据就转成什么类型的list
List
Employee employee = new Employee();
employee.setId(item.getKey());
employee.setEmpName(item.getValue());
return employee;
}).collect(Collectors.toList());
去重
//去重之后进行拼接: List
Srting deviceNodeStr = deviceNodeList.stream().distinct().collect(Collectors.joining("','"));
//直接去重返回list
// List
List
排序
//按照时间排序 1升 -1降
Collections.sort(listFast, (p1, p2) -> {
return String.valueOf(p1.get("time")).compareTo(p2.get("time") + "");
});
// s1-s2 升序 s2-s1降序
Collections.sort(list,(s1,s2) -> s1.getSalary().compareTo(s2.getSalary()));
//多条件排序: List
list.sort(Comparator.comparing(Employee::getSalary).reversed().thenComparing(Employee::getId).reversed());
求和/极值
//在egyList里面求cols的和
public static BigDecimal getSumBig(List
BigDecimal consuBig = egyList.stream()
.filter((Map m)->StringUtils.isNotEmpty(m.get(cols)+"") && !"null".equals(String.valueOf(m.get(cols)))
&& !"-".equals(String.valueOf(m.get(cols))))
.map((Map m)->new BigDecimal(m.get(cols)+""))
.reduce(BigDecimal.ZERO,BigDecimal::add);
return consuBig;
}
//List
//Bigdecimal求和/极值:
BigDecimal sum = list.stream().map(Employee::getSalary).reduce(BigDecimal.ZERO,BigDecimal::add);
BigDecimal max = list.stream().map(Employee::getSalary).reduce(BigDecimal.ZERO,BigDecimal::max);
//基本数据类型求和/极值:
Integer sum = list.stream().mapToInt(Employee::getId).sum();
OptionalInt optionalMax = list.stream().mapToInt(Employee::getId).max();
optionalMax.getAsInt();
统计
//统计:和、数量、最大值、最小值、平均值: List
IntSummaryStatistics collect = list.stream().collect(Collectors.summarizingInt(Employee::getId));
System.out.println("和:" + collect.getSum());
System.out.println("数量:" + collect.getCount());
System.out.println("最大值:" + collect.getMax());
System.out.println("最小值:" + collect.getMin());
System.out.println("平均值:" + collect.getAverage());
求最大/最小值的对象
Optional
if (optional.isPresent()) { // 判断是否有值
Employee user = optional.get();
}
return optional.orElse(new Employee());
平均值
OptionalDouble average = list.stream().mapToInt(Employee::getId).average();
average.getAsDouble();
某个值的数量
//List
Map
//List
long count = egyList.stream()
.filter((Map m)->StringUtils.isNotEmpty(m.get(cols)+""))
.map((Map m)->new BigDecimal(m.get(cols)+""))
.count();
收集
//取出所有年龄放到list集合中
List
.collect(Collectors.toList());
//取出所有年龄放到set集合中
Set
.collect(Collectors.toSet());
//取出所有年龄放到hashSet集合中
HashSet
.collect(Collectors.toCollection(HashSet::new));
//获取集合中元素总和
Long count = list.stream().collect(Collectors.counting());
//获取年龄平均值
Double avg = list.stream().collect(Collectors.averagingInt(Person::getAge));
//获取工资总和
Double sum = list.stream().collect(Collectors.summingDouble(Person::getSalary));
//获取工资最大值的人
Optional
System.out.println(max.get());
//获取工资最小值的人
Optional
System.out.println(min.get());
//获取元素个数、总和、最小值、平均值、最大值
DoubleSummaryStatistics collect = list.stream().collect(Collectors.summarizingDouble(Person::getSalary));
System.out.println(collect);
//输出结果:DoubleSummaryStatistics{count=5, sum=34024.000000, min=99.000000, average=6804.800000, max=9999.000000}
全部转大写
List
//中间操作:不会执行任何操作
Stream
.map(x -> x.toUpperCase());
//终止操作:一次性执行全部内容,惰性求值
stream.forEach(System.out::println);
查找与匹配
List
new Person(18,3939),
new Person(38,9999),
new Person(17,9999),
new Person(19,9988),
new Person(38,99)
);
//是否匹配所有元素 此处返回false
boolean b = list.stream().allMatch(e -> e.getAge() == 18);
System.out.println(b);
//至少匹配一个元素,此处返回true
boolean b1 = list.stream().anyMatch(e -> e.getAge() == 19);
System.out.println(b1);
//流中是否没有匹配元素,此处返回false
boolean b2 = list.stream().noneMatch(e -> e.getAge() == 19);
System.out.println(b2);
//排序后获取第一个元素
Optional
System.out.println(first);
//获取流中任意一个元素
list.stream().findAny();
//返回流中元素的总个数
list.stream().count();
//返回流中最大值 此处根据年龄比较
Optional
System.out.println(max.get());
//返回流中最小值 此处根据年龄比较
Optional
System.out.println(min.get());
//获取最小的年龄
Optional
System.out.println(age.get());
//获取一个并行流,并行流会使用多个线程操作流,stream()获取的是串行流,单个线程操作流
list.parallelStream();
//查找第一个元素
Optional
跳过
List
//中间操作:不会执行任何操作
Stream
.skip(5);
//终止操作:一次性执行全部内容,惰性求值
stream.forEach(System.out::println);
截断
List
//中间操作:不会执行任何操作
Stream
.filter(e -> {
System.out.println("过滤 中间操作");
return e>3;
})
.limit(2);
//终止操作:一次性执行全部内容,惰性求值
stream.forEach(System.out::println);