Skip to content
  • Facebook
GeekCer Logo

GeekCer

The geek's Coding education and Review centre

  • Home
  • Tutorials
    • Java
    • Servlet
    • JSP
    • Python
    • C Tutorial
    • Spring
    • Spring Boot
    • MongoDB
    • Hibernate
    • Data Structure
  • General Knowledge
  • Biography
  • Grammar
  • Festival (त्योहार)
  • Interview
  • Differences
  • Important
  • Toggle search form

Home » Java » Java 8 Stream map example | Java 8 map(function) parameter

  • World Earth Day in Hindi | पृथ्वी दिवस कब और क्यों मनाया जाता है?
    Earth Day in Hindi, Theme | पृथ्वी दिवस कब और क्यों मनाया जाता है? General Knowledge
  • Republic day गणतंत्र दिवस | Happy Republic Day
    Republic day गणतंत्र दिवस कब और क्यों मनाया जाता है? Festival
  • जन्माष्टमी व्रत पूजा विस्तार से | दही हांडी | Krishna Janmashtami Puja
    जन्माष्टमी व्रत पूजा विस्तार से, दही हांडी: Krishna Janmashtami Puja Festival
  • What is Tense in Hindi (काल क्या है)?
    What is Tense in Hindi (काल क्या है)? Grammar
  • National Doctors Day in India and Other Countries, July 1, 2022
    National Doctors Day in India and Other Countries, July 1, 2022 General Knowledge
  • Subhas Chandra Bose Biography in Hindi, Essay, Paragraph
    Subhas Chandra Bose Biography in Hindi, Essay, Paragraph Biography
  • Apj Abdul Kalam biography in Hindi, Life, Missile Man of India
    Apj Abdul Kalam biography in Hindi, Life, Missile Man of India Biography
  • Amitabh Bachchan biography in hindi | Family, wife, awards
    Amitabh Bachchan biography in hindi | Family, wife, awards Biography

Java 8 Stream map example | Java 8 map(function) parameter

Posted on June 1, 2022June 1, 2022 By GeekCer Education No Comments on Java 8 Stream map example | Java 8 map(function) parameter
Java 8 Stream map example | Java 8 map(function) parameter

The Stream.map() function in Java 8 converts a stream to another form. The java Stream.map() acts as a bridge between two operations. In other words, it is an intermediate operation.

The map() accepts an input stream and returns a result to a stream which is sent the output stream. If we have a type X input stream and use the Stream.map() method, that input stream will be converted to a type Y output stream.

Table of Contents

  • Convert ArrayList to Uppercase java by using Stream
  • Get values from list object in Java 8
  • Convert List<Object> into comma separated String
  • Java-stream map to Array
  • Convert int[] to Integer[] by using Stream.map()
  • Java 8 stream map multiple statements

Convert ArrayList to Uppercase java by using Stream

Suppose we have a list of strings and we need to convert all the strings list to upper case.


import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class ConvertListToUpperCase {
	public static void main(String[] args) {
		List<String> listOfStrings = new ArrayList<>();
		listOfStrings.add("java");
		listOfStrings.add("c#");
		listOfStrings.add("html");
		listOfStrings.add("angular js");

		List<String> upperCaseListUsingLoop = new ArrayList<>();
		// By using loop
		for (String str : listOfStrings) {
			upperCaseListUsingLoop.add(str.toUpperCase());
		}
		System.out.println("Using Loop: " + upperCaseListUsingLoop);

		// By using stream
		List<String> upperCaseListUsingStream = new ArrayList<>();
		upperCaseListUsingStream = listOfStrings.stream().map(String::toUpperCase).collect(Collectors.toList());
		System.out.println("Using Stream: " + upperCaseListUsingStream);

	}
}

Output:
Using Loop: [JAVA, C#, HTML, ANGULAR JS]
Using Stream: [JAVA, C#, HTML, ANGULAR JS]

Get values from list object in Java 8

Here is an Employee class in which we have single constructor. We are creating a list of Employee object.

From the list of Employee object we have to find the list of name and collect in the list.


import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

class Employee {
	private int empId;
	private String ename;
	private double salary;

	public Employee(int empId, String ename, double salary) {
		super();
		this.empId = empId;
		this.ename = ename;
		this.salary = salary;
	}

	public int getEmpId() {
		return empId;
	}

	public String getEname() {
		return ename;
	}

	public double getSalary() {
		return salary;
	}

	@Override
	public String toString() {
		return "Employee [empId=" + empId + ", ename=" + ename + ", salary=" + salary + "]";
	}
}

public class ListOfObjectToListOfString {
	public static void main(String[] args) {
		List<Employee> employeeList = Arrays.asList(new Employee(1, "SCOTT", 30000), new Employee(2, "RASHID", 72000),
				new Employee(3, "VIRAT", 50000));

		List<String> employeeNameList = employeeList.stream().map(emp -> emp.getEname()).collect(Collectors.toList());
		System.out.println(employeeNameList);
	}
}

Output:
[SCOTT, RASHID, VIRAT]

Convert List<Object> into comma separated String

We have a list of Employee objects, and we need to gather all of the names and need to separate by commas.


import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

class Employee {
	private int empId;
	private String ename;
	private double salary;

	public Employee(int empId, String ename, double salary) {
		super();
		this.empId = empId;
		this.ename = ename;
		this.salary = salary;
	}

	public int getEmpId() {
		return empId;
	}

	public String getEname() {
		return ename;
	}

	public double getSalary() {
		return salary;
	}

	@Override
	public String toString() {
		return "Employee [empId=" + empId + ", ename=" + ename + ", salary=" + salary + "]";
	}
}

public class ListOfObjectToCommaSepartedString {
	public static void main(String[] args) {
		List<Employee> employeeList = Arrays.asList(new Employee(1, "SCOTT", 30000), new Employee(2, "RASHID", 72000),
				new Employee(3, "VIRAT", 50000), new Employee(4, "SMITH", 51000));

		String commaSeparatedString = employeeList.stream().map(emp -> emp.getEname()).collect(Collectors.joining(","));
		System.out.println(commaSeparatedString);
	}
}

Output:
SCOTT,RASHID,VIRAT,SMITH

Java-stream map to Array

In this example we have a line of strings. We are converting the lines into an array of strings using Stream.map(). This is a simple example of how to convert the whole String to upper case


import java.util.Arrays;

public class StreamMapToArray {
	public static void main(String[] args) {

		String lineString = "Hi, This is an example Stream Map to Array!";

		String[] result = Arrays.stream(lineString.split("\\s+")).map(String::toUpperCase).toArray(String[]::new);

		System.out.println(Arrays.toString(result));
	}
}

Output:
[HI,, THIS, IS, AN, EXAMPLE, STREAM, MAP, TO, ARRAY!]

Convert int[] to Integer[] by using Stream.map()

This example is very useful for interview question. In this example we Convert primitive type array to wrapper type array.

Here we will have an array of type int (int[]) and using Stream.map() we will convert to array of type Integer(Integer[]).


import java.util.Arrays;

public class PrimitiveToWrapper{
	public static void main(String[] args) {

		int[] intArray = { 10, 20, 30, 40, 50 };
		Integer[] integerArray = Arrays.stream(intArray).map(x -> x * 2).boxed().toArray(Integer[]::new);

		System.out.println(Arrays.toString(integerArray));
	}
}

Java 8 stream map multiple statements

In a single statement, we may use multiple map functions. Here’s an example of a multiple map function in operation.

In this example the first map() function multiplies the value by 2 and the second map() function multiplies the value of the first map() by 3.


import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class MultipleMapFunction {
	public static void main(String[] args) {

		List<Integer> valueList = Arrays.asList(1, 2, 3, 4, 5);

		List<Integer> result = valueList.stream().map(e -> e * 2).map(e -> e * 3).collect(Collectors.toList());

		System.out.println(result);
	}
}

Output:
[6, 12, 18, 24, 30]

Recommended Articles

  • Basic Description of Java 8 Lambda Expressions
  • Interview Questions and Answers Java 8
  • Features in Java 11

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • More
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)

Also Read

Java Tags:get string from list of object java 8, how to get values from list object in java 8, java stream map list of objects, Map multiple statements, string::touppercase

Post navigation

Previous Post: Fork/Join Framework in Java | RecursiveTask, RecursiveAction
Next Post: Sequential and Parallel Stream in Java | Example, Differences

More Related Articles

Importance of Thread Synchronization in Java Thread Synchronization in Java | Synchronized Method & Block Java
Java Learning Tutorial Java Tutorial for Advanced Learning, Buzzwords, Parts of Java Java
Method Overloading in Java Method Overloading in Java Java
Java Comments Java Comments : Types of java comments, Syntax & Examples Java
What is Array in Java? Types of array in Java What is Array in Java? Types of array in Java Java
Difference between final, finally and finalize Difference between final, finally and finalize Differences

Leave a Reply Cancel reply

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

  • National Farmers Day in Hindi | राष्ट्रीय किसान दिवस पर निबंध | चौधरी चरण सिंह जयंती
  • Human rights day in Hindi: 10 दिसंबर ह्यूमन राइट्स डे
  • Unicef day is celebrated on December 11 | Speech on unicef day
  • Indian Navy Day: जल सेना दिवस कब और क्यों मनाया जाता है?
  • P V Sindhu Biography in Hindi, Badminton, State, Caste पी. वी. सिंधु जीवन परिचय, कहानी, राज्य, जाति
  • Draupadi Murmu Biography In Hindi | द्रौपदी मुर्मू की जीवनी
  • IPv4 Vs IPv6 | Difference between IPv4 and IPv6
    IPv4 Vs IPv6 | Difference between IPv4 and IPv6 Differences
  • Difference between Internet and Intranet
    Difference between Internet and Intranet Differences
  • Similarities and difference between OSI and TCP/IP model
    OSI vs TCP/IP Model, Similarities and difference between OSI and TCP/IP model Networking
  • TCP/IP Model, Full Form, Layers and their Functions
    TCP/IP Model, Full Form, Layers and their Functions Networking
  • OSI Model | 7 Layers of OSI Model in Computer network
    OSI Model | 7 Layers of OSI Model in Computer network, Functions Networking
  • Network kya hai (नेटवर्क क्या है)
    Network kya hai (नेटवर्क क्या है) Networking
  • Difference between TCP and UDP
    Difference between TCP and UDP | TCP vs UDP examples Differences
  • Java Tutorial
  • Servlet Tutorial
  • JSP Tutorial
  • Maven Tutorial
  • HTML Tutorial
  • Programs
  • Hindi/English Grammar
  • Difference Between ... and ...
  • HR Interview
  • Important Articles

Write to Us:
geekcer.code@gmail.com

  • About Us
  • Privacy and Policy
  • Disclaimer
  • Contact Us
  • Sitemap

Copyright © GeekCer 2022 All Rights reserved