• Post Reply Bookmark Topic Watch Topic
  • New Topic

to Venkat: the way to go?

 
Piet Souris
Rancher
Posts: 1625
35
  • Mark post as helpful
  • send pies
  • 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
 
Venkat Subramaniam
Author
Ranch Hand
Posts: 109
5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Piet,

Here's the code for the transformation:



Here is the complete code example:




 
Piet Souris
Rancher
Posts: 1625
35
  • Mark post as helpful
  • send pies
  • 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
Ranch Hand
Posts: 109
5
  • Mark post as helpful
  • send pies
  • 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>>.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!