This is best shown by example.
Let's say you have a list of Books as below:
public class Book{ String author; Date published; int copiesSold; String catagory; public String getAuthor() { return author; } public Date getPublished() { return published; } public int getCopiesSold() { return copiesSold; } public String getCatagory() { return catagory; } }
To group them into a map of authors to books used to be a little bit painful but is now a one liner!
Map<String, List<Book>> authorsToBooks = books .stream() .collect(Collectors.groupingBy(Book::getAuthor));
The only problem with this that you might have is that the default Map implementation returned is a HashMap which of course is unordered and you might well want to order by the key, in this example by the author. Of course, you could always sort the Map in a second step but there's a way to do it in one line.
To fix that let's introduce this static utility function:
public static <T, K extends Comparable<K>> Collector<T, ?, TreeMap<K, List<T>>> sortedGroupingBy(Function<T, K> function) { return Collectors.groupingBy(function, TreeMap::new, Collectors.toList()); }
We can call it like this:
Map<String, List<Book>> authorsToBooks = books .stream() .collect(sortedGroupingBy(Book::getAuthor));
This time the map implementation is a TreeMap which has returned the Map of authors to their books in alphabetical order.
Thanks for your informative post on Java application development. This open source platform assists software developers to create stunning mobile application with ease. Further, they can make use of this platform at free of cost.JAVA Training|Java Courses in Chennai
ReplyDeleteI have read your blog its very Interesting. Thanks for sharing. ERP Providers in Chennai | ERP Software Company Chennai. We provide best quality cost effective ERP software applications and end to end enterprise solutions to all the industry verticals with the affordable cost.
ReplyDeleteThank you for this valuable information. I have got some important suggestions from it. Get your business to the next level in simple steps.
ReplyDeleteERP Solutions in Chennai | ERP Software Solutions in Chennai.
Very good post, it's very useful to all blog readers and Keep sharing.
ReplyDeleteBest Kidney Doctor in Bangalore
Best Urologist in Bangalore
Pediatric nephrologist in Bangalore
Informative and impressive. Keep Updating
ReplyDeleteWebdesign Deutschland
ReplyDeleteThe website is looking bit flashy and it catches the visitors eyes. A design is pretty simple .
regionale stellenbörse
Nice article, I loved it.
ReplyDeletepersonalrekrutierung
Really nice post.Thanks for sharing.
ReplyDeletejobbörse siegerland
Thanks For Sharing content ..CUSTOMIZED ERP SOLUTIONS
ReplyDelete
ReplyDeleteThanks for sharing the predictions on Is very Useful Thanks for sharing the post looking forward for more posts.http://www.chloros.in/customized-erp-solutions.html
I believe there are many more pleasurable opportunities ahead for individuals that looked at your site.
ReplyDeleteEmbedded System training in Chennai | Embedded system training institute in chennai | PLC Training institute in chennai | IEEE final year projects in chennai | VLSI training institute in chennai
SEM SERVICES
ReplyDeleteI am fully satisfied with the information you have posted. Good job. Keep posting:)
CUSTOMIZED ERP SOLUTIONS
PLC Training in Chennai | PLC Training Institute in Chennai | PLC Training Center in Chennai | PLC Training Centre in Chennai | SCADA Training in Chennai | DCS Training in Chennai | PLC Training in Anna Nagar | PLC Training Institute in Anna Nagar | PLC Training Institute in Chennai | PLC Training Center in Chennai | PLC Training in Chennai | Best PLC Training in Chennai | Best PLC Training Institute in Chennai
ReplyDeletePLC Training in Chennai | PLC Training Institute in Chennai | PLC Training Center in Chennai | PLC SCADA Training in Chennai | PLC SCADA DCS Training in Chennai | Best PLC Training in Chennai | Best PLC Training Institute in Chennai | PLC Training Centre in Chennai | PLC SCADA Training in Chennai | DCS Training in Chennai | DCS Training Institute in Chennai | Automation Training Institute in Chennai | Inplant Training in Chennai
ReplyDelete