• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

into method in the Stream class

 
Ranch Hand
Posts: 146
1
IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am learning the use of stream API on Java collections by following this blog: http://java.amitph.com/2012/10/java-collections-api-enhancements.html

I've stumbled upon this piece of code that I can't seem to get to work,


I am trying to reproduce the behavior with the following code,


But, IntellijIDEA says that it can not resolve method 'into'. I looked into Sream class, and I could not find a method named 'into'.

So, how do I make the code work? The blog is from 2012 - did the API change?
 
Saloon Keeper
Posts: 10930
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've never see "into" before. You can use .collect() with a toList() Collector. Note that you should use "List" instead of "ArrayList". You can find the documentation for Collectors HERE.
 
Quazi Irfan
Ranch Hand
Posts: 146
1
IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you!
 
Rancher
Posts: 517
15
Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ArrayList<String> list = new ArrayList<>();
       list.add("a");
       list.add("b");
       list.add("c");
       ArrayList<String> filteredList = list.stream().filter(name -> name.equals("a")).into(new ArrayList<>());
   }
}

But, IntellijIDEA says that it can not resolve method 'into'. I looked into Sream class, and I could not find a method named 'into'.



As I see there is no into method in the java.util.stream.Stream. That's the reason the IDE is complaining.  The correct way to do the reduction operation (writing the result to a List collection) is:

 
Bartender
Posts: 5562
213
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you do want an ArrayList, it can be done with this:
 
Marshal
Posts: 79967
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Quazi Irfan wrote:. . . following this blog: . . . The blog is from 2012 - did the API change?

That tutorial isn't very helpful, even to people who can actually read the white text on dark grey

That link wrote:. . . . The upcoming additions to the Java 8’s Collections API will make it to support Internal Iterations. . . .

It appears to have been written before Java8 became available, and was predicting changes which were not actually implemented. The into() method doesn't exist; as Prayad Saya said. You would have to use collect(...)
 
You have to be odd to be #1 - Seuss. An odd little ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic