Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning 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
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

map method in Streams

 
Ranch Hand
Posts: 161
1
jQuery Eclipse IDE
  • Mark post as helpful
  • send pies
  • 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: 9674
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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):
 
Ranch Hand
Posts: 95
1
VI Editor Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 9674
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
Forget Steve. Look at 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