We have summarized the functional interface of Java SE 8 and the primitive specialization of Stream. The purpose is to prepare for the Java Gold exam.
In java, primitives cannot be given to type arguments such as T, R of R apply (T t) of Function \ <T, R>. In functional interfaces and Streams, some classes are individually defined so that int, long, and double can be operated like generics (without boxing in the wrapper class). This is called a type with primitive specialization. If you remember only int, there is no difference between long and double.
| Basic class name | Specialization to int | Specialization to long | Specialization to double | Basic method name |
|---|---|---|---|---|
| Function<T,R> | IntFunction<R> | LongFunction<R> | DoubleFunction<R> | apply() |
| Function<T,R> | IntToDoubleFunction | LongToDoubleFunction | DoubleToIntFunction | apply() |
| Function<T,R> | IntToLongFunction | LongToIntFunction | DoubleToLongFunction | apply() |
| Function<T,R> | ToIntFunction<T> | ToLongFunction<T> | ToDoubleFunction<T> | apply() |
| UnaryOperator<T> | IntUnaryOperator | LongUnaryOperator | DoubleUnaryOperator | apply() |
| BiFunction<T,U,R> | ToIntBiFunction<T,U> | ToLongBiFunction<T,U> | ToDoubleBiFunction<T,U> | apply() |
| BinaryOperator<T> | IntBinaryOperator | LongBinaryOperator | DoubleBinaryOperator | apply() |
| Predicate<T> | IntPredicate | LongPredicate | DoublePredicate | test() |
| BiPredicate<T,U> | - | - | - | test() |
| Consumer<T> | IntConsumer | LongConsumer | DoubleConsumer | accept() |
| BiConsumer<T,U> | ObjIntConsumer<T> | ObjLongConsumer<T> | ObjDoubleConsumer<T> | accept() |
| Supplier<T> | IntSupplier | LongSupplier | DoubleSupplier | get() |
| BooleanSupplier | - | - | - | getAsBoolean() |
--The primitive-specialized functional interface appears as an argument to the primitive-specialized Stream method.
--The method name of the functional interface should be ** "basic method name As type name" ** only if the return value is a primitive.
--The target of specialization is basically only int, long, double, and BooleanSupplier is special. ――Is BooleanSupplier supposed to be used when writing tests and assertions? -"Bad float, this package is for 3 people"
--Function primitive specialization corresponds to each of the following patterns. --Take a primitive argument and return a primitive --Take an object argument and return a primitive --Take a primitive argument and return an object -Is there IntToIntFunction in the 2nd and 3rd rows of the table? It looks like it will be IntUnaryOperator.
--In accept () of ObjIntConsumer corresponding to BiConsumer, argument 1 is an object and argument 2 is an int. This is used as an accumulator for [IntStream.collect ()] 2.
The following 3 classes are primitive specialized types from Stream \
Here, we will call it a primitive specialization stream.
--sum (): Take the sum.
--avage (): Take the arithmetic mean. The return value is OptionalDouble.
--max (): Since the order is self-explanatory, it takes no arguments unlike Stream \
| this | Return value | Method name | argument |
|---|---|---|---|
| IntStream | LongStream | mapToLong *1 | IntToLongFunction |
| IntStream | DoubleStream | mapToDouble *1 | IntToDoubleFunction |
| IntStream | Stream<T> | mapToObj *2 | IntFunction<? extends T> |
| LongStream | IntStream | mapToInt | LongToIntFunction |
| LongStream | DoubleStream | mapToDouble *1 | LongToDoubleFunction |
| LongStream | Stream<T> | mapToObj *2 | LongFunction<? extends T> |
| DoubleStream | IntStream | mapToInt | DoubleToIntFunction |
| DoubleStream | LongStream | mapToLong | DoubleToLongFunction |
| DoubleStream | Stream<T> | mapToObj *2 | DoubleFunction<? extends T> |
| this | Return value | Method name | argument |
|---|---|---|---|
| IntStream | DoubleStream | asDoubleStream | - |
| IntStream | LongStream | asLongStream | - |
| LongStream | DoubleStream | asDoubleStream | - |
| this | Return value | Method name | argument |
|---|---|---|---|
| IntStream | Stream<Integer> | boxed | - |
| LongStream | Stream<Long> | boxed | - |
| DoubleStream | Stream<Double> | boxed | - |
| this | Return value | Method name | argument |
|---|---|---|---|
| Stream<T> | IntStream() | mapToInt | ToIntFunction<? super T> |
| Stream<T> | LongStream() | mapToLong | ToLongFunction<? super T> |
| Stream<T> | DoubleStream() | mapToDouble | ToDoubleFunction<? super T> |
A combination of flattening and primitive specialization. ** The one who seems to be in the exam **.
| this | Return value | Method name | argument |
|---|---|---|---|
| Stream<T> | IntStream() | flatMapToInt | Function<? super T,? extends IntStream> |
| Stream<T> | LongStream() | flatMapToLong | Function<? super T,? extends LongStream> |
| Stream<T> | DoubleStream() | flatMapToDouble | Function<? super T,? extends DoubleStream> |
Recommended Posts