• 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

to Venkat: the way to go?

 
Bartender
Posts: 5465
212
  • 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: 135
10
  • 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: 5465
212
  • 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: 135
10
  • 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>>.
 
If you were a tree, what sort of tree would you be? This tiny ad is a poop beast.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic