Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java stream API: are there syntax sugar for identity functor?

We use several Map as simple memory DB over 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));

Are there any syntax sugar or built-in functor in Java SE to replace p -> p with something else?

like image 772
gavenkoa Avatar asked Jan 29 '15 19:01

gavenkoa


1 Answers

You could use Function.identity() but if you want short then I don't think you'll beat your existing p -> p.

like image 133
Ian Roberts Avatar answered Nov 15 '22 01:11

Ian Roberts