Java thread API: is there syntactic sugar for an identification function?

We use several Map as a simple DB memory for a list of objects:

 class Person { public String id; public String phone; public String email; // and get/set and other fields... } List<Person> persons; Map<String, Person> emailLookup = persons.stream() .collect(Collectors.toMap(Person::getEmail, p -> p)); Map<String, Person> phoneLookup = persons.stream() .collect(Collectors.toMap(Person::getPhone, p -> p)); Map<String, Person> idLookup = persons.stream() .collect(Collectors.toMap(Person::getId, p -> p)); 

Is there any syntactic sugar or inline functor in Java SE to replace p -> p with something else?

+6
source share
1 answer

You can use Function.identity() , but if you need a short one, I don't think you will beat the existing p -> p .

+8
source

Source: https://habr.com/ru/post/981726/


All Articles