一.常见API
1.流的创建
List<String> list = Arrays.asList("hello","world","stream");
//创建顺序流
Stream<String> stream = list.stream();
//创建并行流
Stream<String> parallelStream = list.parallelStream();
//数组
String[] array = {"h", "e", "l", "l", "o"};
Stream<String> arrayStream = Arrays.stream(array);
2.筛选与切片
filter:过滤流中的某些元素
limit(n):获取n个元素
skip(n):跳过n元素,配合limit(n)可实现分页
distinct:通过流中元素的 hashCode() 和 equals() 去除重复元素
Stream<Integer> stream = Stream.of(6, 4, 6, 7, 3, 9, 8, 10, 12, 14, 14);
Stream<Integer> newStream = stream.filter(s -> s > 5) //6 6 7 9 8 10 12 14 14
.distinct() //6 7 9 8 10 12 14
.skip(2) //9 8 10 12 14
.limit(2); //9 8
3.常用方法
Student s1 = new Student("aa", 10,1);
Student s2 = new Student("bb", 20,2);
Student s3 = new Student("cc", 10,3);
List<Student> list = Arrays.asList(s1, s2, s3);
//装成list
List<Integer> ageList = list.stream().map(Student::getAge).collect(Collectors.toList()); // [10, 20, 10]
//转成set
Set<Integer> ageSet = list.stream().map(Student::getAge).collect(Collectors.toSet()); // [20, 10]
//转成map,注:key不能相同,否则报错
Map<String, Integer> studentMap = list.stream().collect(Collectors.toMap(Student::getName, Student::getAge)); // {cc=10, bb=20, aa=10}
//字符串分隔符连接
String joinName = list.stream().map(Student::getName).collect(Collectors.joining(",", "(", ")")); // (aa,bb,cc)
//聚合操作
//1.学生总数
Long count = list.stream().collect(Collectors.counting()); // 3
//2.最大年龄 (最小的minBy同理)
Integer maxAge = list.stream().map(Student::getAge).collect(Collectors.maxBy(Integer::compare)).get(); // 20
//3.所有人的年龄
Integer sumAge = list.stream().collect(Collectors.summingInt(Student::getAge)); // 40
//4.平均年龄
Double averageAge = list.stream().collect(Collectors.averagingDouble(Student::getAge)); // 13.333333333333334
//分组
Map<Integer, List<Student>> ageMap = list.stream().collect(Collectors.groupingBy(Student::getAge));
//多重分组,先根据类型分再根据年龄分
Map<Integer, Map<Integer, List<Student>>> typeAgeMap = list.stream().collect(Collectors.groupingBy(Student::getType, Collectors.groupingBy(Student::getAge)));
//分区
//分成两部分,一部分大于10岁,一部分小于等于10岁
Map<Boolean, List<Student>> partMap = list.stream().collect(Collectors.partitioningBy(v -> v.getAge() > 10));
//规约
Integer allAge = list.stream().map(Student::getAge).collect(Collectors.reducing(Integer::sum)).get(); //40
二.Java8 List集合中的部份数据,按条件根据某个字段进行合并
@Data
public class Product {
/**id*/
private String productId;
/**库存*/
private double stockCount;
/**采购数量*/
private double purchaseCount;
/**单位*/
private String unit;
}
————————————————
方法一:通过Lamba表达式的groupingBy来实现
List<Product> list = new ArrayList<>();
list.add(new Product(1, 10, 10));
list.add(new Product(2, 11, 11));
list.add(new Product(1, 12, 12));
List<Product > newList = new ArrayList<>();
list.parallelStream().collect(Collectors.groupingBy(Product::getProductId, Collectors.toList()))
.forEach((id, stu) -> {
stu.stream().reduce((a, b) -> new Product (a.getProductId(), a.getStockCount() + b.getStockCount(), a.getpurchaseCount() + b.getpurchaseCount())).ifPresent(newList ::add);
});
方法三:使用Lamba表达式的toMap来实现
List<WaterStatistics> newList =
newDataList.stream().collect(Collectors.toMap(WaterStatistics::getEnergyDate, a -> a, (o1, o2) -> {
o1.setUsedData(o1.getUsedData().add(o2.getUsedData()));
o1.setWaterRate(o1.getWaterRate().add(o2.getWaterRate()));
return o1;
})).values().stream().collect(Collectors.toList());
三.java 8 list对象根据某个属性进行排序
// 根据时间从小到大排序
Collections.sort(newList, Comparator.comparing(WaterStatistics::getEnergyDate));
// 根据数值大小从大到小排序
List<WaterStatistics> rankingList =
rankingDataList.stream().sorted(Comparator.comparing(WaterStatistics::getUsedData)
.reversed()).collect(Collectors.toList());
四.java8 list对象某个属性组成新的list
List<String> deviceCodeList =
dataList.stream().map(WaterStatistics::getDeviceCode).collect(Collectors.toList());
五.java8 list对象根据某个属性分组
Map<Integer, List<WaterOpenCard>> openCardTypeMap = WaterOpenCardList.stream().collect(Collectors.groupingBy(EWaterOpenCard::getRechargeType));