I want to use a method reference based off another method reference. It's kind of hard to explain, so I'll give you an example:
Person.java
public class Person{
Person sibling;
int age;
public Person(int age){
this.age = age;
}
public void setSibling(Person p){
this.sibling = p;
}
public Person getSibling(){
return sibling;
}
public int getAge(){
return age;
}
}
Given a list of Person
s, I want to use method references to get a list of their sibling's ages. I know this can be done like this:
roster.stream().map(p -> p.getSibling().getAge()).collect(Collectors.toList());
But I'm wondering if it's possible to do it more like this:
roster.stream().map(Person::getSibling::getAge).collect(Collectors.toList());
It's not terribly useful in this example, I just want to know what's possible.