bi是binary的简写,二元的,表示两个参数
unary,一元的,表示一个参数
1.函数式接口
Supplier 无入参,出参可任意类型
IntSupplier 无入参,出参固定是int型
LongSupplier 无入参,出参固定是long型
DoubleSupplier 无入参,出参固定是double型
2.函数式接口
Consumer void accept(T t),接受一个参数,不返回
IntConsumer,void accept(int value),接收一个int型参数,不返回值
LongConsumer,void accept(long value),接收一个long型参数,不返回值
DoubleConsumer,void accept(double value),接收一个double型参数,不返回
3.函数式接口
Function R apply(T t),接收一个参数,有返回值
IntFunction,R apply(int value),接收一个int型参数,有返回值
ToIntFunction,int applyAsInt(T value),接收一个参数,返回一个int型结果
LongFunction,R apply(long value),接收一个long型参数,有返回值
ToLongFunction,long applyAsLong(T value),接收一个参数,返回一个long型结果
DoubleFunction,R apply(double value),接收一个double型参数,有返回值
ToDoubleFunction,double applyAsDouble(T value),接收一个参数,返回一个double型结果
4.函数式接口UnaryOperator,是Function的子接口。接收一个参数,有返回值,且返回值的类型与参数类型一样
IntUnaryOperator,int applyAsInt(int operand),接收一个int型参数,返回一个int型结果
LongUnaryOperator,long applyAsLong(long operand),接收一个long型参数,返回一个long型结果
DoubleUnaryOperator,double applyAsDouble(double operand),接收一个double型参数,返回一个double型结果
5.函数式接口BiConsumer void accept(T t, U u),接收两个参数,不返回
6.函数式接口BiFunction R apply(T t, U u),接收两个参数,有返回值
ToIntBiFunction,int applyAsInt(T t, U u),接收两个参数,返回一个int型结果
ToLongBiFunction,long applyAsLong(T t, U u),接收两个参数,返回一个long型结果
ToDoubleBiFunction,double applyAsDouble(T t, U u),接收两个参数,返回一个double型结果
7.函数式接口BinaryOperator,是BiFunction的子接口。接收两个参数,有返回值,且两个参数和返回值的类型都一样
IntBinaryOperator,int applyAsInt(int left, int right),接收两个int型参数,返回一个int型结果
LongBinaryOperator,long applyAsLong(long left, long right),接收两个long型参数,返回一个long型结果
DoubleBinaryOperator,double applyAsDouble(double left, double right),接收两个double型参数,返回一个double型结果
8.函数式接口Predicate boolean test(T t),接收一个参数,返回true或者false
9.接口Stream、IntStream、LongStream、DoubleStream
接口Stream
static Stream<T> of(T... values),入参是多个同一种类型的值或者数组,返回一个Stream对象
static Stream generate(Supplier s),生成一个无穷的、无序的Stream对象
static Stream iterate(T seed, UnaryOperator<T> f),生成一个无穷的、有序的Stream对象,生成规则就好像迭代函数一样,seed是第一个元素,第二个元素是传seed进去生成的结果,第三个元素是传第二个元素进去生成的结果。。。以此类推
Stream distinct(),返回一个新的去重后的Stream对象
Stream sorted(Comparator comparator),入参是一个排序器,返回一个排好序的Stream对象
Stream<R> map(Function<T, R> mapper),入参是一个Function对象,返回一个新的Stream对象。
Stream<R> flatMap(Function<T, Stream<R>> mapper),入参也是一个Function对象,返回一个新的Stream对象。与map()方法不同的是,flatMap()方法的Function对象的出参固定是Stream类型,Stream的泛型与flatMap()方法的返回值Stream实例的泛型一致。flatMap()被成为扁平化流操作,可以把多个集合或者数组的值融入到一个集合或者数组中。
示例1:需求:把数组["hello", "world"]转成数组["h", "e", "l", "o", "w", "r", "d"]。
public static void main(String[] args) { String[] arr = new String[] {"hello", "world"}; List<String> ss = Arrays.stream(arr) .flatMap(p -> Arrays.stream(p.split(""))) .distinct() .collect(Collectors.toList()); arr = ss.toArray(new String[] {}); Arrays.stream(arr).forEach(System.out::println); }
示例2:有一个集合,里面元素也是集合类型,集合里面元素是整型,把这些整型元素都放入一个新的集合中。 List<List<Integer>> -->List<Integer>
public static void main(String[] args) { Map<String, List<Integer>> map = new HashMap<>(4); map.put("a", Arrays.asList(1, 2, 3)); map.put("b", Arrays.asList(4, 5, 6)); List<Integer> list = map.values().stream().flatMap(Collection::stream).collect(Collectors.toList()); System.out.println(list); }
Stream skip(long n),返回一个出去前n个元素之后的Stream对象
Stream limit(long maxSize),返回一个最多包含maxSize个元素的新的Stream对象
Stream filter(Predicate predicate),返回一个过滤后的新的Stream对象
IntStream mapToInt(ToIntFunction mapper),把一个Stream对象转换成一个IntStream对象
Optional findFirst(),返回包含第一个元素的Optional对象
long count(),返回Stream对象包含的元素的个数
Object[] toArray(),返回一个数组,可转换至具体类型的数组
void forEach(Consumer action),传入一个Consumer对象
Optional max(Comparator comparator),返回Stream对象包含的最大的元素,怎么算大,由传入的Comparator对象决定
collect(Collector collector),传入一个Collector对象,Collector对象可以调用Collectors的各种静态方法生成,如toList(),toCollection(Supplier collectionFactory),toSet(),reducing()的三个重载、joining()的三个重载、groupingBy()的三个重载等等,还有好多使用的方法
接口IntStream
static IntStream range(int startInclusive, int endExclusive),返回一个IntStream对象,元素包含第一个参数,不包含第二个参数
static IntStream rangeClosed(int startInclusive, int endInclusive),返回一个IntStream对象,两边都包含
IntSummaryStatistics summaryStatistics(),返回一个IntSummaryStatistics对象,进而可以得到最大值、最小值这些。
int sum(),求和
reduce()方法,有2个重载
第1个重载,OptionalInt reduce(IntBinaryOperator op),入参是个IntBinaryOperator对象,返回一个OptionalInt对象
第2个重载,int reduce(int identity, IntBinaryOperator op),第一个参数是个int值,第二个参数是个IntBinaryOperator对象,返回一个int型结果
10.final类Optional、OptionalInt
Optional
T get(),如果值存在,返回原值,否则抛出NoSuchElementException异常
T orElse(T other),如果值存在,返回原值,否则返回传入的值
void ifPresent(Consumer consumer),如果值存在的话,就处理,否则什么都不做。
OptionalInt
int getAsInt(),如果值存在,返回原值,否则抛出NoSuchElementException异常
int orElse(int other),如果值存在,返回原值,否则返回传入的值
void ifPresent(IntConsumer consumer),如果值存在的话,就处理,否则什么都不做。
11.IntSummaryStatistics,IntConsumer的实现类
LongSummaryStatistics,LongConsumer的实现类
DoubleSummaryStatistics,DoubleConsumer的实现类
用于汇总统计的类,通过调用getMin()、getMax()、getAverage()、getCount()、getSum()实例方法获取最小值、最大值、平均值、个数、求和。