This week's giveaway is in the Spring forum.
We're giving away four 3 month subscriptions to Marco Behler Videos and have Marco Behler on-line!
See this thread for details.
Win a 3 month subscription to Marco Behler Videos this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Java 8 Lambda  RSS feed

 
diao ni
Greenhorn
Posts: 2
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, assuming I have the following two classes:



In the main method, all I want is to group the collection albums in a new object Map<String, Double>, which key is the album name and the value is the average rating for the tracks related to its ablum.  I am requested to use Java 8 new features to solve above problem (I know it is very easy if I write the code in staandard way).  I also know I need to use Stream.collect and Collectors.groupingBy, e.g.:



Would you please help me to solve this problem?

Thanks in advanced
 
Knute Snortum
Sheriff
Posts: 3837
91
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at java.util.stream.Collectors.toMap and then post what you have done.
 
Junilu Lacar
Sheriff
Posts: 10929
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and Welcome to the Ranch!
 
Piet Souris
Rancher
Posts: 1915
66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would make life very easy if you add a method 'getRating' to your Album class. You can use a java 8 way for this. The resulting map is then easily obtained by using the toMap, as Knute suggested.
 
diao ni
Greenhorn
Posts: 2
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all, I found the solution:



 
Paul Clapham
Sheriff
Posts: 22271
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for posting back with your answer, diao ni! I gave you a cow for that.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!