• 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

Question to Streams and groupingBy()

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




The Output is:
{Physiotherapie=[Verena Buchner], Development=[Michael Meyer], Marketing=[Dominic Jansen], Business=[Max Mustermann]}

I want ordered output by subjects:
{Business=[Max Mustermann], Development=[Michael Meyer], Marketing=[Dominic Jansen], Physiotherapie=[Verena Buchner]}

The Output does not change, when i add following line before .collect


My reasoning is, that the returned type is maybe a HashMap without a guarantee for ordered values. Do i need collect the stream as LinkedHashMap?
Im open for Suggestions with Examples.

Greetings AZ
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your reasoning is correct, but the solution is not to use a LinkedHashMap. The reason for this is that it's generally not a good idea to use the .sorted() operation on streams unless you REALLY need to. It will cause the stream to buffer all elements, which at best (for finite streams) will double your memory requirements before you collect all items, and at worst (for infinite streams) will cause your application to crash.

Instead, let the collect() operation sort the elements by telling it to collect to a sorted map:

As an aside, you could have simplified your sorted() operation like this:
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try adding...to your code, and that will help with your question. But remember what the documentation for groupingBy() says:-

There are no guarantees on the type, mutability, serializability, or thread-safety of the Map or List objects returned.

Nor does it say anything about the order of the keys you are grouping by. You could consider putting the entire Map into a SortedMap or similar. Please read that link to see how a sorted map orders its entries. You will see the workings of groupingBy() better if you have several Student objects with the same department name. Consider using an enum containing the different departments instead of text.

Please don't call a Map xyzSet. Call it studentsMap or similar.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic