<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Grouping on pieterjd.be</title><link>https://pieterjd.github.io/tags/grouping/</link><description>Recent content in Grouping on pieterjd.be</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><copyright>This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.</copyright><lastBuildDate>Mon, 22 May 2023 17:02:49 +0200</lastBuildDate><atom:link href="https://pieterjd.github.io/tags/grouping/index.xml" rel="self" type="application/rss+xml"/><item><title>Advanced Grouping with Java streams</title><link>https://pieterjd.github.io/posts/java-stream-advanced-grouping/</link><pubDate>Mon, 22 May 2023 17:02:49 +0200</pubDate><guid>https://pieterjd.github.io/posts/java-stream-advanced-grouping/</guid><description>The problem I have a list of Person objects. I would like to group them per birth month, and only keep a List of lastnames.
I came up with this code from memory
Map&amp;lt;String, List&amp;lt;Person&amp;gt;&amp;gt; personsByMonth = persons.stream() .collect(Collectors.groupingBy(Person::getBirthMonth)); System.out.println(personsByMonth); And you get the following output (truncated for readability):
{JUNE=[Person{lastName='Doe 11', birthDate=2022-06-07}], JANUARY=[Person{lastName='Doe 10', birthDate=2023-01-26}]} But how do I get rid of the redundant information?
The solution The Collectors class provides quite a lot of variation of the groupingBy method.</description></item></channel></rss>