How to use identity

"I've come to realize that Functional Interface and other things are useful, but what do you use this" identity "for? What on earth do you want to return a function that just returns?" I was told, so it's a very new memo.

//If there is such a class
@Data
class Obj {
    private int id;
    private String data;
}

//Suppose it is a List
List<Obj> list = <some>;

// key:id, value:Generate map of obj(* I feel like I have a better hand)
Map<Integer, Obj> map = list.stream().collect(Collectors.toMap(obj::getId, Function.identity()));

Functional Interface is an interface for receiving "the operation itself". It treats all kinds of operations like that with signatures. However, when I write things using this bang bang, it is not uncommon for me to say, "No, you can just leave it as it is! Don't do anything!" Identity is useful in such cases.

Of course you can do the same thing with lambda, like o-> o, but it's not exactly the same. It's notorious that the entity of lambda is an anonymous local class, which means that the more you use it, the more classes and instances you use. That means a little load. (That's right, being able to pass it as an argument means that it has an entity as an object)

On the other hand (depending on the JVM implementation) identity always returns the same instance: https://stackoverflow.com/questions/28032827/java-8-lambdas-function-identity-or-t-t Certainly, if it surely ends in one shot, it's okay to write it with lambda. However, basically, using Function.identity or UnaryOperator.identity has the advantage that you can rest assured that you will not inadvertently mass-produce useless instances. Let's forgive a little more types and imports.

"By the way, what is identity?"

identity = identity. The method identity returns an identity function. An identity function is a "function that returns the passed arguments" and is the identity element in the combination of functions. In other words, it's like the number 1 (although it depends on the area of the context, but sensuously).

Recommended Posts

How to use identity
How to use Map
How to use with_option
How to use fields_for
How to use java.util.logging
How to use map
How to use collection_select
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use TreeSet
[How to use label]
How to use hashes
How to use JUnit 5
How to use Dozer.mapper
How to use Gradle
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
How to use Map
How to use Chain API
[Java] How to use Map
How to use Priority Queuing
[Rails] How to use enum
How to use java Optional
How to use JUnit (beginner)
How to use Ruby return
[Rails] How to use enum
How to use @Builder (Lombok)
[Swift] How to use UserDefaults
How to use java class
How to use Swift UIScrollView
How to use Big Decimal
How to use String [] args
[Java] How to use string.format
How to use rails join
How to use Java Map
Ruby: How to use cookies
How to use dependent :: destroy
How to use Eclipse Debug_Shell
How to use Apache POI
[Rails] How to use validation
How to use Java variables
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
How to use GC Viewer
[Java] How to use Optional ①
How to use Lombok now
[Creating] How to use JUnit
[Rails] How to use Scope
How to use the link_to method
How to use arrays (personal memorandum)
How to use scope (JSP & Servlet)
How to use the include? method
[Rails] How to use devise (Note)
How to use the form_with method
How to use EventBus3 and ThreadMode
How to use Spring Data JDBC
How to use binding.pry [53 days left]
How to use Java HttpClient (Post)
[Java] How to use join method