Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Lambda equivalent of method references

 
Ranch Hand
Posts: 161
1
jQuery Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am reading the OCP book by Jeanne and i came across a line of code as follows:



I couldnot able to write the above line on code using lambda expression. I tried but i couldnot. Could any body help me plelase ?

Thanks & Regards,

Swapna
 
Bartender
Posts: 4006
156
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look at the API, you see that we need a Supplier<TreeSet>, a BiConsumer<TreeSet, String> and a BiConsumer<TreeSet, TreeSet>. So we get:

 
Marshal
Posts: 69847
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See if you can't find Modern Java Recipes by Ken Kousen (O'Reilly, 2017) (page 281). He explains how a Collector object is built with three generic types:-

. . . the generic types are defined as:
• T , the type of input elements to the reduction operation
• A , the mutable accumulation type of the reduction operation (often hidden as an implementation detail)
• R , the result of the reduction operation

That would appear to correspond to the Collector#of() method, but there is another of() method which looks easier to use. Be sure to read the documentation links, too. The first argument is a Supplier, whose functional method is get(). Let's imagine I want to write a Collector<Integer, Long, Long>.At this point I am probably going to get the whole thing wrong and Stephan will have to correct it for me
Anyway, you can see you have a λ representing a Supplier<Long>. One down, two to go.

You have declared the first argument as a Supplier<Long> and we have declared <A> as representing Long, and the middle functionparameter is a BiConsumer<A, T>, whose functional method is accept(). So we want something implementing the functional method of a BiConsumer<Long, Integer>. So let's tryI am sure you could use boxing and unboxing to reduce line 4 by about 90% of its length. Two down one to go.

Now, what happens with the third argument is that you might be multi‑threading (parallelStream() rather than ordinary stream()), so you get Long results from each of the different threads, and you have to combine them, which you do with a BinaryOperator<A, R> (whose functional method is apply(T, U). So what we are trying to do is multiply the two Longs from each Thread, so let's try this:-The fourth argument is called finisher, but I don't think we need any post‑processing, so let's try passing null. It is a Function<Long, Long>, whose functional method is apply(T), so alternatives would be l -> l or even “Function::identity” if you don't mind a method reference.I can't see that we would need any characteristics, so let's pass a 0‑length array, like this:-Now read the documentation for the of() method and work out what mistake I have made

I am adding this thread to a more general forum in the hope of getting you more attention.

Edits: Various spelling corrections and correct the <A> formal type parameter which was invisible (it is too similar to a common HTML tag). Change function to parameter. Leave my (two) mistakes not corrected
 
Swapna latha
Ranch Hand
Posts: 161
1
jQuery Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:If you look at the API, you see that we need a Supplier<TreeSet>, a BiConsumer<TreeSet, String> and a BiConsumer<TreeSet, TreeSet>. So we get:



Thank you Piet.
 
Piet Souris
Bartender
Posts: 4006
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome, Swapna!
 
please buy this thing and then I get a fat cut of the action:
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
    Bookmark Topic Watch Topic
  • New Topic