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.

    Was this post helpful?


    import_contacts

    You may also like:

Related Posts

  • 11 June

    Remove Parentheses From String in Java

    Table of ContentsJava StringsRemove Parentheses From a String Using the replaceAll() MethodRemove Parentheses From a String by TraversingConclusionWas this post helpful? Java uses the Strings data structure to store the text data. This article discusses methods to remove parentheses from a String in Java. Java Strings Java Strings is a class that stores the text […]

  • 25 May

    Get Variable Name as String in JavaScript

    Table of ContentsUsing Object.keys()Using toString() methodUsing hash tableWas this post helpful? In this post, we will see how to get variable name as String in JavaScript. There are multiple ways to get Get variable name as String in JavaScript. Let’s go through them. Using Object.keys() To get variable name as String in JavaScript, use Object.keys() […]

  • 25 May

    Format Date to yyyy-MM-dd in java

    Table of ContentsUsing DateTimeFormatter with LocalDate (Java 8)Using SimpleDateFormatUsing Calendar classWas this post helpful? In this post, we will see how to Format Date to yyyy-MM-dd in java. There are multiple ways to format Date to yyyy-MM-dd in java. Let’s go through them. Using DateTimeFormatter with LocalDate (Java 8) To Format Date to yyyy-MM-dd in […]

  • 25 May

    Remove Comma from String in JavaScript

    Table of ContentsUsing replace()Using replaceAll()Using split() and join() methodWas this post helpful? In this post, we will see how to remove comma from String in Javascript. There are multiple ways to do it. Let’s go through them. Using replace() To remove comma from String in Javascript: Use replace() method with regular expression /\,/g as first […]

  • 25 May

    Escape new line in JavaScript

    Table of ContentsEscape new line in JavaScriptConclusionWas this post helpful? In this post, we will see how to escape new line in JavaScript. There is no well known JavaScript library which can escape all special characters in String. Escape new line in JavaScript To escape new line in JavaScript: Use replace() method with regular expression […]

  • 23 May

    Escape Ampersand in URL in JavaScript

    Table of ContentsGeneric solution to escape special character in JavaScriptWas this post helpful? In this post, we will see how to escape ampersand in JavaScript. To escape Ampersand in JavaScript, use encodeURIComponent() in JavaScript. [crayon-62b91ae93fca5151544623/] Output [crayon-62b91ae93fcad914429473/] As you can see that when we called encodeURIComponent, it encoded & to %26. You can use % […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.