• 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Lambdas and Streams - Shortest way todo to every element of collection

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want todo something to every element of a collection ( so I get returned a new collection or overwrite the existing collection )

Is there a short way todo this ,  for example in Javascript I could do this:



I know I can do like this in Java ( but wondering if there's a shorter way in terms of lines of code) :


 
Saloon Keeper
Posts: 12992
281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, that looks good.
 
Master Rancher
Posts: 3889
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An annoying feature of how Java implemented streams is that their API is somewhat separate from collections... so when you use stream operations with collections, you almost always have to start with .stream() to go from collection to stream, and often end with something like .collect(Collections.toList()) to convert to List again.  It's a bit annoying, but normal in Java.

Note that under Java 16, this can become nums.stream().map(num -> num*2).toList() - they added a convenience method to shorten that last call.
 
Luke Moloney
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks guys thats good to know

 
Saloon Keeper
Posts: 4500
166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note thar you can reduce the typing a little by using the var keyword, and by importing static Collectors.*.
 
Luke Moloney
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Piet

Will try that out too, I keep forgetting about var in Java  !
 
You showed up just in time for the waffles! And this tiny ad:
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
reply
    Bookmark Topic Watch Topic
  • New Topic