• 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
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Using collect method of Stream class  RSS feed

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a question about the use of the collect method of the Stream class. For learning I have created a stream with some Person objects, and I try to convert this stream to a List using the difficult way (not using a predefined collector). So I have create the following code first:

This works fine so, to make this code a bit smaller, I rewrote it as follows:

To me this looks functionally the same as the first block of code but the compiler complains about the collect, add and addAll methods. So, I rewrote this sample to explicitly tell the compiler the type of the variables of the lambda's:

Unfortunately, this code also doesn't compile. The compiler still complains about the type of the two BiConsumer arguments. To me it seems that it must be possible to implement the collector this way. Could someone explain what is wrong with this code?

Thanks in advance,
Paul
 
Marshal
Posts: 62819
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Added to our Java8/λs and Streams forum.

You would usually only write separate accumulator and supplier objects if you need them to do something unusual. You usually use this ready‑made Collector:-Please post the full compiler error message.
 
Sheriff
Posts: 21602
101
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The new ArrayList<String>() is not a Supplier but an object. Try using () -> new ArrayList<String>(), or even ArrayList::new.
 
Saloon Keeper
Posts: 2138
80
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Nijssen wrote:... and I try to convert this stream to a List using the difficult way (not using a predefined collector)...


Assuming that you are referring to Collectors.toList() here, why are you avoiding that ? I feel that using a map followed by a Collector is the most intuitive way (Campbell's example).
 
Paul Nijssen
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks all for the quick responses.

Rob Spoor gave me the clue I was looking for; I forgot the () -> in the line where I create the supplier....

About using the complicated way to create a collector: I am studying for OCP, and I try to get used to use the functional interfaces.  So, the creation of the collector myself was just an exercise and has no practical goal.

Best regards,
Paul
 
Rob Spoor
Sheriff
Posts: 21602
101
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
Time is mother nature's way of keeping everything from happening at once. And this is a tiny ad:
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!