Skip to content
Advertisement

Count Groups of Objects in List

I have a list of objects and need to count the number of objects that have the same date, location and color in Java 8:

JavaScript

So for the above the totals would be for the number of people working each combination of date, location and color. i.e.:

  • 12/12/2021 Los Angeles Red – 3
  • 12/12/2021 Los Angeles Blue – 1
  • 12/12/2021 Los Angeles Green – 2
  • 13/12/2021 San Deigo Green – 2
  • 13/12/2021 San Deigo Red – 1
  • 14/12/2021 Los Angeles Red – 1

How can this be done?

Advertisement

Answer

You can group on the three fields date, location, and color:

JavaScript

It generates the nested Map:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement