Is the method of the primitive specialized IntFunction apply or applyAsInt?

Introduction

When I was studying Java SE 8 Gold, it was a complicated place, so I summarized it. It also touches on functional interfaces of other primitive special types. Note) Contents for Gold test takers

In fact, it can be solved in one word

The rule that solves this problem is ** "A functional interface with one return type is the corresponding method = basic method + As + return type" ** It is summarized in one word.

It's a word with implications that you can learn about the functional interface in java.util.function (I made it myself), so let's unravel it.

Correspondence between basic functional interface and method provided by java.util.function

First of all, the basics of the basics. If you don't remember this, it's quite difficult to get Gold. On the contrary, if you understand this, you can understand half of the previous sentence.

Basic functional interface Method Description
Consumer<T> void accept(T) Receives arguments, processes and does not return results,
"consumer"
Function<T,R> R apply(T t) Receives an argument and the specified type(R)Returns the result of
"processing"
Predicate<T> boolean test(T t) Take an argument and evaluate it with a boolean value,
"Affirmation"
Supplier<T> T get() Returns only the result without taking any arguments,
"Supplier"

The story of the premise that can be understood from here

By the way, looking back at the previous rule, it says, "The return type has been decided to be one." Please take a closer look at the table above. Do not return a return value in the first place You don't have to type the return value You will notice that there is a functional interface. The former is Consumer <T> and the latter is Predicate <T>. What will happen to these two?

Looking at the Java SE API documentation for java.util.function, there are functional interfaces such as IntConsumer and IntPredicate, which are primitive specialized types such as IntFunction. So, are there methods like acceptAsInt and testAsInt like applyAsInt?

The answer is no.

** The problem this time is the related methods of Function \ <T, R > and Supplier \ <T > ** First of all, please keep in mind. (In Gold's problem, acceptAsInt and testAsInt don't even appear as options.)

Try to solve the title problem

Let's get back to the main subject here. Is the method of IntFunction, which is a primitive specialization type of Function interface, just apply or applyAsInt ... As you can see from reading the rules, the point is whether the return type is fixed. This can be seen by looking at the definition of functional interfaces up to generics. There are three basic patterns for Function-related primitive specialization types, so let's check them here.

IntFunction<R>
ToIntFunction<T>
DoubleToIntFunction

A rough summary of the relationship between the meaning of the above generics and the types of arguments and return values <R>: The return type can be decided arbitrarily (argument is int type) <T>: Argument type can be decided arbitrarily (return value is int type) No generics: Arguments and return values are of fixed type (arguments are double type, return value is int type) Will be.

In other words, the IntFunction method of the title can decide the return type arbitrarily, so it's just ʻapply`` Since the return type of ToIntFunction and DoubleToIntFunction is determined by the int type, it becomes ʻapplyAsInt``.

Of course, other DoubleFunctions (the method is apply), ToDoubleFunction (the method is applyAsDouble), etc. follow the same rules.

What is the IntSupplier method?

Now let's consider the IntSupplier method. Given that the IntFunction method was just apply You might think that the IntSupplier method is just a get. ** Don't be fooled **

You have to remember who the Supplier was. Supplier \ <T > was a "supplier" that took no arguments and returned only the result. In other words, since there is no argument in the functional interface related to Supplier, the part of the primitive specialized IntSupplier that is determined to be int type can only be the return value.

As a result, the corresponding method of `ʻIntSupplier is getAsInt``. ** Conversely, the corresponding method becomes get only in Supplier \ <T > in the Supplier system. ** ** ~~ (Personally, I want to set the generics of Supplier \ <T > to R) ~~

Application problem

(1) BiFunction, (2) BinaryOperator, (3) IntBinaryOperator, (4) IntUnaryOperator, (5) ToDoubleBiFunction, what are the corresponding methods?

First, ① to ⑤ are specialized types of Fuction. Therefore, the basic method part of "~ corresponding method = basic method + As + return type" is `ʻapply``.

①BiFunction If you write the definition up to generics, BiFunction <T, U, R> Simply put, a version with two more arguments for Function \ <T, R >. Since the return value is not fixed, the corresponding method is `ʻapply`` (however, there are two arguments)

②BinaryOperator If you write the definition up to generics, BinaryOperator <T> Simply put, it is a functional interface when the two arguments of BiFunction and the return type are all the same. Synonymous with BiFunction \ <T, T, T >. Since the return value is not fixed, the corresponding method is `ʻapply`` (however, there are two arguments)

③IntBinaryOperator A version of BinaryOperator whose two arguments and return value are of type int. Since the return value is determined by int type, the corresponding method is `ʻapplyAsInt`` (two arguments are also int type)

④IntUnaryOperator First, ʻUnaryOperator <T> `` is a functional interface when the argument and return type of Function are the same. Synonymous with Function \ <T, T \>. A version of the Unary Operator whose arguments and return value are both int types. Since the return value is determined by int type, the corresponding method is ʻapplyAsInt`` (argument is also int type)

⑤ToDoubleBiFunction If you write the definition up to generics, ToDoubleBiFunction <T, U> Since it is decided that only the return value is double type, the corresponding method is `ʻapplyAsDouble`` (however, there are two arguments)

Finally

I've written so far, but when I implement it, I get a compile error, so I shouldn't have to think about such a confusing thing. I hope this article will help you study for Gold exams and understand the java.util.function package.

reference

Memo on how to use the function interface below java.util.function java.util.function (Java Platform SE 8 )

Recommended Posts

Is the method of the primitive specialized IntFunction apply or applyAsInt?
The order of Java method modifiers is fixed
The question of which is better, if or switch
The permission specification of the FileUtils method is an octal number.
Java: The problem of which is faster, stream or loop
What is the initialize method?
What is @Override or @SuppressWarnings ("SleepWhileInLoop") in front of the function? ?? ??
Which is faster, size or 0, as the argument of List # toArray?
Investigation method when the CPU of the server running java is heavy
'% 02d' What is the percentage of% 2?
About the role of the initialize method
What kind of method is define_method?
[Rails] Regarding the presence or absence of parentheses in the argument of the render method
From Java9, the constructor of the class corresponding to primitive types is deprecated.
What is testing? ・ About the importance of testing
What is the main method in Java?
What is the data structure of ActionText?
Deepened my understanding of the merge method
[Java] Is it unnecessary to check "identity" in the implementation of the equals () method?