• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

to Venkat: the way to go?

 
Bartender
Posts: 4729
183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Venkat,

thanks for a very interesting speech on youtube!

My question is about where to go with Java, now we have Java 8.
I would like to start with an example.

Suppose we have a class 'Pair', consisting of a pair of Integers.

I have the following pairs: (1,1), (1,2), (1,3), (2,10, (2,20), (2,30). (3,100), (3,200), (3,300).

The idea is to come up with the following map:

1 -> {1, 2, 3}
2 -> {10, 20, 30}
3 -> {100, 200, 300}

In Scala, I came up with a solution that was pretty easy to find and program:

(list is the list of pairs)


and I think it is very elegant.

So I tried to achieve the same in Java 8, and I found, at last, after very much effort,
the following far less comprehensible way:


And now I'm wondering: should Java go towards the power and ease of use of Scala,
or do you see other directions for Java to go?

Piet
 
Author
Posts: 109
5
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Piet,

Here's the code for the transformation:



Here is the complete code example:




 
Piet Souris
Bartender
Posts: 4729
183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see I could learn quite some from your book. I guess I never quite understood that second parameter
(the mapping).

Thank you for this example!

Piet
 
Venkat Subramaniam
Author
Posts: 109
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The second parameter says after performing the grouping, instead of collecting the pairs for each group, instead collect the (in this example, the second) specific value from within the pair. Without the mapping, we will get a Map<Integer, List<Pair>> instead of Map<Integer, List<Integer>>.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic