Java Primitive Specialization Summary

Preface

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.

What is primitive specialization?

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.

Organize functional interfaces

[java.util.function] 1 full-function interface

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()

point

--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.

[java.util.stream] Organize primitive specializations in 0

type

The following 3 classes are primitive specialized types from Stream \ .

Here, we will call it a primitive specialization stream.

Primitive specialized stream-specific termination operations

--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 \ .max (Comparator). --min (): Since the order is self-explanatory, it does not take an argument unlike Stream \ .min (Comparator).

Mutual conversion between Stream \ and primitive specialized streams

From a primitive specialization stream

mapToXxx () method

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>

* 1 A simple upcast can be done with the asXxx () method.

this Return value Method name argument
IntStream DoubleStream asDoubleStream -
IntStream LongStream asLongStream -
LongStream DoubleStream asDoubleStream -

* 2 Simple boxing can be done with the boxed () method

this Return value Method name argument
IntStream Stream<Integer> boxed -
LongStream Stream<Long> boxed -
DoubleStream Stream<Double> boxed -

From Stream \ to a primitive specialized stream

mapToXxx () method

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>

flatMapToXxx () method

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>

Please let me know if there is something to add.

Recommended Posts

Java Primitive Specialization Summary
Java knowledge summary
Java Generics Summary
Java related summary
Java 11 document summary
Java 12 new feature summary
[Summary] Java environment preparation
effective java 3rd summary
Java 13 new feature summary
Java static [Personal summary]
Thread safe summary ~ Java ~
Java development link summary
Personal summary about Java
Java 10 new feature summary
java regular expression summary
Java 14 new feature summary
Summary of Java support 2018
Java design pattern summary
Java reserved word summary
Java8 Stream Rough Summary
What is Java Assertion? Summary.
[Java11] Stream Summary -Advantages of Stream-
Progate Java (Beginner) Review & Summary
[Java] Summary of regular expressions
I investigated Java primitive types
[Java] Summary of operators (operator)
Java8 stream, lambda expression summary
Object-oriented summary by beginners (Java)
Summary of Java language basics
Java tips --Spring execution Summary
[Java] Summary of for statements
Summary of Java Math class
[Java11] Stream Usage Summary -Basics-
[Java] Summary of control syntax
Summary of java error processing
[Java] Summary of design patterns
[Java] Summary of mathematical operations
Consideration on Java Persistence Framework 2017 (Summary) -1
[For beginners] Summary of java constructor
Java release date and EOL summary
Java primitive types, reference types, Immutable, Mutable
Summary
AtCoder 400 points algorithm summary (Java edition)
Java
About Java primitive types and reference types
Java "pass by reference" problem summary
Java EE 8 (Jakarta EE 8) New feature summary
Java
Summary of object-oriented programming using Java
[Java Silver] Summary of access modifier points
Summary of in-house newcomer study session [Java]
How to make a Java calendar Summary
[java] Summary of how to handle char
Summary of changes other than JEP of Java10
[Java] Personal summary of conditional statements (basic)
[Java] [Maven3] Summary of how to use Maven3
Java Summary of frequently searched type conversions
[Java] Instance method, instance field, class method, class field, constructor summary
Summary of Java Math.random and import (Calendar)