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

map method in Streams

 
Ranch Hand
Posts: 161
1
jQuery Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I could not able to understand the map method in Streams ?

Could anyone please explain it with example


Thanks & Regards,

Swapna
 
Sheriff
Posts: 9683
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The map method is used to convert a stream of one object to a different object. For example you can convert a stream of some object to stream of String by calling toString on the objects of the steam like this (there are other syntaxes of doing this, I'm just using one of them):
 
Rancher
Posts: 97
4
VI Editor Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would just point out that you don't have to return different type of object, you are free to return the same type of object, even exactly same one without any internal modification to given object. See description directly from method javadoc:

Returns a stream consisting of the results of applying the given function to the elements of this stream.



You also have map method on primitive streams, which return the same primitive (optionally with som modification), eg:

 
Ankit Garg
Sheriff
Posts: 9683
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Miroslav Lehotsky wrote:I would just point out that you don't have to return different type of object, you are free to return the same type of object,


True, good catch.
 
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