• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is it possible to group this way using java streams? grouping together based on multiple fields

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
let us consider a sample object Car ->





I would like to group the objects in carList and collect something similar to [[Car{make='honda', model='civic', year='2000'}, Car{make='honda', model='civic', year='2002'}], [Car{make='honda', model='accord', year='1999'}], [Car{make='dodge', model='charger', year='1966'}]] and that type would be List<List<Car>> list;

Here the logic would be cars belonging to same make and model will be grouped together and added in one list, example : [Car{make='honda', model='civic', year='2000'}, Car{make='honda', model='civic', year='2002'}]

honda accord would totally be added in a new list , same applies to dodge charger.

 
Bartender
Posts: 390
47
Firefox Browser MySQL Database Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not much use away from my computer, but you group like that using Collectors.groupingBy().

You would group on a particular field, and would get a corresponding Map instance (like Map<String, List<Car>>).

You are doing more than one grouping, though, first by make and then by mofel. Take note that the collectors can take a downstream collector for further processing. I hope that is enough to point your efforts in the right direction
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can do that in one grouping, using stream.collect(Collectors.groupingBy( ... )).  The first argument to groupingBy() would be a function to create the key you want to group by - in this case, you can create a String that concatenates the make and model, using a separator, like "Honda|Civic".  Everything with the same key will be grouped together...
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To add to what JJ said:

I added a method: public Pair<String, String> getMakeModel to the Car class, returning a Pair<make, model>. Then it is easy to create a Map<Pair, List<Car>>, Then take the values of this map and turn these into a list. The Pair class is from JavaFX.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would give your Car a constructor taking all the fields, and I would get rid of the setXXX() methods. Why would you want setXXX() methods in the first place, since those data should never change? You might like to extend the process I have started and make the Car object completely immutable.

As an alternative to Pair, you can create a class called MakeModel or something like that. In order to work as a “K” in a Map, that class needs correctly‑overridden hashCode() and equals() methods.

[edit]Additional: I presume this is the Pair class that Piet mentioned. It already appears to have overridden hashCode() and equals() methods.
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joel Babu wrote:List<Car> carList


Try to avoid things naming as "somethingList", that may add some confusion if you appear to have Car[] carList, in either case, variable name encodes an implementation detail i.e. used data structure, and that isn't considered as a good practice.

Simply just call cars or maybe carsOnSale if that's what it is, that way an intention would be more clear.
reply
    Bookmark Topic Watch Topic
  • New Topic