java stream groupingBy embedded objects

Sandro Rey :

I have couple of objects:

public class RequestHostel {
    private Hostel name;
}

and

public class Hostel {
    private String value;
}

and I would like to know if it is possible to group by the value of Hostel, something like

.stream().collect(Collectors.groupingBy(RequestHostel::getName::getValue, counting()))
Andronicus :

You cannot chain method reference, but you can use lambda:

.stream().collect(Collectors.groupingBy(rh -> rh.getName().getValue(), counting()))

Or if you're not using these objects, you can simply map them:

.stream().map(RequestHostel::getName).collect(Collectors.groupingBy(Hotel::getValue, counting()))

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=306275&siteId=1