Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

get a list of unique date from the stream grouping

I have data:

id|date
1 |12-12-2021
2 |12-12-2021
3 |13-12-2021

Want to get a list of dates: ["12-12-2021", "13-12-2021"]

Using stream, I can get a map:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

txs.stream().collect(Collectors.groupingBy(g -> g.getDate()));

I would like to convert to list in from the stream above.

>Solution :

groupingBy is not the best choice in your case. Use distinct instead. It will automatically filter out all duplicates.

txs.stream().map(g -> g.getDate()).distinct().collect(Collectors.toList());
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading