1、创建流的6种方法
1.1集合,也是最常见的,
List<Integer> list = new ArrayList();
list.add(1);
list.add(2);
list.stream().collect(Collectors.summaryInt(Integer::intValue));
1.2 boxed(),通过基础流包装而来
IntStream i = IntStream.of(1,2,3);
i.boxed().collect(Collectors.summingInt(Integer::intValue));
1.3 Stream.of()
Stream.of(1,2,3).collect(Collectors.summingInt(Integer::intValue));
1.4 Stream.generate()
Stream.generate(()->{return 1;}).limit(10).collect(Collectors.summingInt(Integer::intValue));
1.5 Stream.itereate(0,x -> x+1).forEach(System.out::println);
1.6 Stream.Builder
Stream.Builder builder = Stream.builder(); builder.accept(5); builder.add(1).add(2).add(3).build().forEach(System.out::println);
2、
3、
4、Collectors
4.1 求平均值,流中元素的个数
对流中的元素求平均值,该函数接受一个函数,将流中的元素转化位double,int ,或者long;
流中的元素不能位null,转换的时候会出现空指针异常。
public class Lambda {
public static void main(String[] args) {
List<Element> list = new ArrayList<>();
list.add(new Element(1));
list.add(new Element(2));
list.add(new Element(3));
System.out.println(list.stream().collect(Collectors.averagingInt(Element::getI)));
System.out.println(list.stream().collect(Collectors.counting()));
List<Integer> list1 = new ArrayList<>();
list1.add(1);
list1.add(2);
list1.add(3);
System.out.println(list1.stream().collect(Collectors.averagingInt(Integer::intValue)));
}
@Data
@AllArgsConstructor
static class Element {
private int i;
}
}
2.0
3
2.0
4.2 分组操作
4.3 字符串的连接join
可以通过map函数抽取指定的字符串列
public static void main(String [] argc) {
List<String> stringList = new ArrayList<>();
stringList.add("1");
stringList.add("2");
stringList.add("3");
System.out.println(stringList.stream().collect(Collectors.joining()));
System.out.println(stringList.stream().collect(Collectors.joining(",")));
System.out.println(stringList.stream().collect(Collectors.joining(",","(",")")));
}
123
1,2,3
(1,2,3)
4.4 求取最大值和最小值
public class Lambda {
public static void main(String[] args) {
List<Element> list = new ArrayList<>();
list.add(new Element(1,1,2));
list.add(new Element(1,2,2));
list.add(new Element(1,3,2));
// 映射成key,原元素集合
Map<Integer,List<Element>> map = list.stream().collect(Collectors.groupingBy(Element::getKey,Collectors.toList()));
System.out.println(list.stream().collect(Collectors.maxBy(Comparator.comparingInt(Element::getKey))));
System.out.println(list.stream().collect(Collectors.minBy(Comparator.comparingInt(Element::getKey))));
}
@Data
@AllArgsConstructor
static class Element {
private int i;
private int key;
private int value;
}
}
Optional[Lambda.Element(i=1, key=3, value=2)]
Optional[Lambda.Element(i=1, key=1, value=2)]
汇总
package com.example.demo.springboot.java8feature;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class Lambda {
public static void main(String[] args) {
List<Element> list = new ArrayList<>();
list.add(new Element(1, 1, 2));
list.add(new Element(1, 2, 2));
list.add(new Element(1, 3, 2));
// 映射成key,原元素集合
Map<Integer, List<Element>> map = list.stream().collect(Collectors.groupingBy(Element::getKey, Collectors.toList()));
// 分组
// 分组,value 按照分组后,抽取元素指定的列,归拢成集合
Map<Integer, List<Integer>> map1 = list.stream().collect(Collectors.groupingBy(Element::getI, Collectors.mapping(Element::getKey, Collectors.toList())));
// 求最大,最小
System.out.println(list.stream().collect(Collectors.maxBy(Comparator.comparingInt(Element::getKey))));
System.out.println(list.stream().collect(Collectors.minBy(Comparator.comparingInt(Element::getKey))));
// 分区
Map<Boolean, List<Element>> booleanListMap = list.stream().collect(Collectors.partitioningBy(e -> {
return e.getKey() % 2 == 0;
}));
// 分区,指定list集合类型
Map<Boolean, List<Element>> booleanListMap1 = list.stream().collect(Collectors.partitioningBy(e -> {
return e.getKey() % 2 == 0;
}, Collectors.toList()));
Map<Boolean, List<Element>> booleanListMap2 = list.stream().collect(Collectors.partitioningBy(e -> {
return e.getKey() % 2 == 0;
}, Collectors.toCollection(LinkedList::new)));
// 规约操作
list.stream().collect(Collectors.reducing(0, Element::getKey, (key1, key2) -> {
return key1 + key2;
}));
// 转换成map
list.stream().collect(Collectors.toMap(Element::getKey, Element::getValue));
list.stream().collect(Collectors.toMap(Element::getKey, Element::getValue, (k1, k2) -> {
return k1;
}));
list.stream().collect(Collectors.toMap(Element::getKey, Element::getValue, (k1, k2) -> {
return k1;
}, LinkedHashMap::new));
// 转换成concurrentHashMap
list.stream().collect(Collectors.toConcurrentMap(Element::getKey, Element::getValue));
list.stream().collect(Collectors.toConcurrentMap(Element::getKey, Element::getValue, (k1, k2) -> {
return k1;
}));
list.stream().collect(Collectors.toConcurrentMap(Element::getKey, Element::getValue, (k1, k2) -> {
return k1;
}, ConcurrentHashMap::new));
// 求和 IntSummaryStatistics{count=3, sum=6, min=1, average=2.000000, max=3}
System.out.println(list.stream().collect(Collectors.summarizingInt(Element::getKey)));
// 求和 6
System.out.println(list.stream().collect(Collectors.summingInt(Element::getKey)));
IntStream i = IntStream.of(1, 2, 3);
i.boxed().collect(Collectors.summingInt(Integer::intValue));
Stream.of(1, 2, 3).collect(Collectors.summingInt(Integer::intValue));
Stream.generate(() -> {
return 1;
}).limit(10).collect(Collectors.summingInt(Integer::intValue));
}
@Data
@AllArgsConstructor
static class Element {
private int i;
private int key;
private int value;
}
}