Java 8 Stream Map

Java 8 Stream map

In this post, we will see about Java 8 Stream map function.

stream‘s map method takes single element from input stream and produces single element to output stream. Type of Single input Stream and Output stream can differ.

Java 8 Stream Map function

Map function returns a stream consisting of the results of applying the given function to the elements of this stream.
Function is a functional interface available in Java 8 java.util.function.Function accepts one input and produce a result.

Few important points about Map

  • Map is intermediate operation and returns stream as a result.
  • Map operation takes Function as parameter and this function is called on each element of the stream.
  • Map() method is used to convert Stream to Stream
  • You can also use map method to convert it from Stream to Stream.
    For example: Calling toUppercase() method in Map method which will again return Stream.
  • Let’s understand with the help of a simple example.

    Java 8 Stream Map example

    Let’s say you have a list of employees, now you want to convert it to list of employee names, so you will map each employee object to employee name.

    Java 8 Stream map

    You will give input as Stream of employee objects and get an output as Stream of String.

    1. Create a class named Employee.java

    2. Create main class named Java8StreamMapMain.java

    When you run above program, you will get below output:

    John
    Martin
    Mary
    Steve

    Here is logical representation of above program.
    MapEmployee
    You can use method reference to at line no.17 as below

    Another example

    Let’s say you have list of integers and you want to find sum of double of even numbers.

    When you run above program, you will get below output:

    60

    If you notice, we have used map function to double of input number in above example.

    That’s all about Java 8 Stream map example.


    import_contacts

    You may also like:

Related Posts

  • Get current year in java
    13 July

    Get current year in Java

    Table of ContentsUsing Date and SimpleDateFormatUsing CalendarUsing LocalDateUsing LocalDateTimeusing ZonedDateTimeUsing OffsetDateTimeUsing Instant and ZoneIdUsing Instant and ZoneOffsetConclusion In this post, we will see how to get current year in Java. Using Date and SimpleDateFormat The Date class was introduced in Java 1.0 and provided different approaches to work with time. The SimpleDateFormat inherits from DateFormat […]

  • 27 March

    Top Object-Oriented Programming Languages to Follow in 2021

    Table of ContentsJavaProsConsWhere ApplicableC/C++ProsConsWhere ApplicablePythonProsConsWhere ApplicableJavaScriptProsConsWhere ApplicableR LanguageProsConsWhere ApplicableConclusion Wherever you look, you face object-oriented programming languages. Well, many of the popular coding languages are object-oriented. The moment you decide to learn an object-oriented programming language, it might be quite overwhelming to pick the only one from this big pool of options. But don’t worry! […]

  • How to end program in java
    08 March

    How to end program in java

    Table of ContentsUsing System.exit() to end java programExample to demonstrate System.exit() methodHow to end java program in an if statement in javaUsing return to end current method Ho In this post, we will see how to end program in java. You can use System.exit(0) to end program in java. Using System.exit() to end java program […]

  • 02 January

    System.out.println shortcut in eclipse

    Learn about System.out.println shortcut in eclipse.

  • 19 September

    Java 9 – Anonymous Inner classes and Diamond Operator

    Table of ContentsAnonymous class: Java 9 ImprovementExample: Anonymous class with diamond operator Type inference is a feature of Java that was introduced in Java 7. Now, Java compiler can infer type of a data automatically. For example, if we create an ArrayList then we use the code like: [crayon-60ef229b6714b543597281/] You can see that Java 7 […]

  • 06 February

    Java FileWriter Example

    Table of ContentsConstructorsMethodsExampleCode:Output: The FileWriter class of java.io package makes writing to files in Java as easy as A-B-C. The FileWriter class writes to files as a stream of characters instead of bytes(as in FileOutputStream). Like its input-oriented counterpart FileReader, the FileWriter class is intended for writing “text” based files one character at a time. […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.