分组、去重、排序、求和、过滤、集合拷贝、过滤、等
List 集合
package com.xxx.xxx.service.alarm.impl;
@Data
public class Demo {
private String id;
private String name;
private String age;
private String rowIndex;
public Demo(String id, String name, String age, String rowIndex) {
this.id = id;
this.name = name;
this.age = age;
this.rowIndex = rowIndex;
}
@Override
public String toString() {
return "Demo{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", age='" + age + '\'' +
", rowIndex='" + rowIndex + '\'' +
'}';
}
List<Demo> allData = Arrays.asList(
new Demo("1", "2", "30", null),
new Demo("2", "1", "20", null),
new Demo("2", "1", "20", null),
new Demo("3", "xzj", "20", null));
去重
List<Demo> demo1 = allData.stream()
.collect(Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(
Comparator.comparing(data -> data.getAge()))), ArrayList::new));
System.out.println(userList);
List collect = Stream.of(parseArray, pmNumbers)
.flatMap(Collection::stream)
.distinct()
.collect(Collectors.toList());
取差集
List<PmNumber> collect = list1.stream().filter(e -> {
return !list2.contains(e);
}).collect(Collectors.toList());
过滤(根据某一属性)
List<Demo> demo2 =allData.stream().filter(e -> e.getAge().equals(30)).collect(toList());
List<Demo> demo = cffjzhbDataList.stream().filter(e -> StringUtils.isNotEmpty(e.getGx()) && StringUtils.isNotEmpty(e.getClf())).collect(toList());
分组(根据某一属性)
Map<String, List<Demo>> groupBySex = allData
.stream().collect(Collectors.groupingBy(Demo::getAge));
Map<String, List<Demo>> groupBySex = allData
.stream().collect(Collectors.groupingBy(e -> e.getName() + "_" + e.getAge()));
Function<Demo, List<Object>> compositeKey = allData->
Arrays.<Object>asList(user.getAge(), user.getName());
Map<Object, List<Demo>> map =
allData.stream().collect(Collectors.groupingBy(compositeKey, Collectors.toList()));
排序 (单字段排序,多字段排序)
list.stream().sorted()
list.stream().sorted(Comparator.reverseOrder())
List<Demo> a = allData.stream().sorted(Comparator.comparing(Demo::getName)).collect(toList());
List<Demo> b = allData.stream().sorted(Comparator.comparing(Demo::getName).reversed()
).collect(toList());
List<Demo> sortList = allData.stream()
.sorted(Comparator.comparing(Demo::getName, Comparator.reverseOrder())
.thenComparing(Demo::getAge)
.thenComparing(Demo::getId, Comparator.reverseOrder()))
.collect(Collectors.toList());
求和、最大、最小、平均值计算
DecimalFormat df = new DecimalFormat("0.00");
double sum = allData.stream().mapToDouble(Demo::getAge).sum();
System.out.println("身高 总和:" + df.format(sum));
double max = allData.stream().mapToDouble(Demo::getHeight).max().getAsDouble();
System.out.println("身高 最大:" + df.format(max));
double min = allData.stream().mapToDouble(Demo::getHeight).min().getAsDouble();
System.out.println("身高 最小:" + df.format(min));
double average = allData.stream().mapToDouble(Demo::getHeight).average().getAsDouble();
System.out.println("身高 平均:" + df.format(average));
a对象的内容拷贝到b
List<Demo> copyDemo = allData.stream().map(p -> {
Demo e = new Demo();
e.setAge(p.getAge());
e.setName(p.getName());
return e;
}).collect(Collectors.toList());
集合中属性拼接 (取出集合中名字属性并拼接)
String stationCodes = " in ('" + allData.stream().map(x -> x.getName()).distinct().collect(Collectors.joining("','")) + "')";
![在这里插入图片描述](https://img-blog.csdnimg.cn/5695e489ce674f01810d9200e4f08e50.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5p2w5ouJ5b63wrfnmq7lhYvCt-iwog==,size_20,color_FFFFFF,t_70,g_se,x_16)