3

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()))
0

2 Answers 2

2

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()))
Sign up to request clarification or add additional context in comments.

Comments

2

You cannot chain method references as you did. But yes, you can use lambda for that:

Map<String, Long> countGroupedByValue = requestHostels.stream()
        .collect(Collectors.groupingBy(requestHostel -> requestHostel.getName().getValue(),
                Collectors.counting()));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.