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

  • Set an array to another array in java
    23 April

    Set an Array Equal to Another Array in Java

    Table of ContentsSetting an Array Variable Equal to Another Array VariableSet an Array Equal to Another Array in Java Using the clone() MethodSet an Array Equal to Another Array in Java Using the arraycopy() MethodSet an Array Equal to Another Array in Java Using the copyOf() MethodSet an Array Equal to Another Array in Java […]

  • Java random number between 1 and 100
    07 March

    Generate Random Number Between 1 and 100 in Java

    Table of ContentsUsing Java Util’s Random Class to Generate Random Number Between 1 and 100 in JavaThe nextint() MethodThe ints() MethodUsing Math Class to to Generate Random Number Between 1 and 100 in JavaUsing Apache Commons Library to Generate Random Number Between 1 and 100 in JavaUsing ThreadLocalRandom Library to Generate Random Number Between 1 […]

  • java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList
    22 February

    [Fixed] java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList

    Table of ContentsReason for java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayListFixes for java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayListUse ArrayList’s constructorAssign Arrays.asList() to List reference rather than ArrayList In this post, we will see how to fix java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList. ClassCastException is runtime exception which indicate that code has tried to […]

  • HashMap values cannot be cast to list
    21 February

    [Fixed] java.util.HashMap$Values cannot be cast to class java.util.List

    Table of ContentsWhy HashMap values cannot be cast to list?Fix for java.util.HashMap$Values cannot be cast to class java.util.List In this post, we will see how to fix error java.util.HashMap$Values cannot be cast to class java.util.List. Why HashMap values cannot be cast to list? HashMap values returns java.util.Collection and you can not cast Collection to List […]

  • Format number with Commas in Java
    20 February

    Format Number with Commas in Java

    Table of ContentsHow To Add Commas to Number in Java1. Using DecimalFormat2. Using String’s format() method3. Using System.out.printf4. Using Formatter5. Using NumberFormat In this post, we will see how to format number with commas in java. How To Add Commas to Number in Java There are multiple ways to format number with commas in java. […]

  • Unable to obtain LocalDateTime from TemporalAccessor
    20 February

    [Fixed] Unable to obtain LocalDateTime from TemporalAccessor

    Table of ContentsUnable to obtain LocalDateTime from TemporalAccessor : ReasonUnable to obtain LocalDateTime from TemporalAccessor : FixLocalDate’s parse() method with atStartOfDay()Use LocalDate instead of LocalDateTime In this article, we will see how to fix Unable to obtain LocalDateTime from TemporalAccessor in Java 8. Unable to obtain LocalDateTime from TemporalAccessor : Reason You will generally get […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.