Core Java Concepts and Syntax

Learn about Map Stream in Java

Learn about Map Stream in Java

Introduction

Map is a higher-order function in java 8. It converts the elements of an object to another type of element. Stream is a data structure that provides a way to represent data on which one can perform sequential operations such as mapping and reducing. The map() method of java stream is used to map the elements of one stream into another stream, this will be helpful in understanding how streams work and how they can be used for various purposes.

We'll discuss how to convert/map objects of various types with Streams using the map() operation.

What is Stream?

A Stream is a collection of objects with different methods combined to produce desired results. You can create a stream from various data sources, including collections, Arrays, primitive data types, and I/O channels.

It's crucial to note streams do not store any data, so they are not data structures.

It's just a data source wrapper that enables us to work with data more quickly, efficiently, and with cleaner code. Furthermore, streams do not modify their original data structures; instead, they return the results of their pipelined methods.

Types of Streams

There are two types of streams: sequential (created with stream()) and parallel (created with parallelStream ()). Parallel streams can run multiple threads, whereas sequential streams cannot.

There are two types of operations on streams: intermediate and terminal:

Streams return streams while performing intermediate operations. Because of this, we can chain multiple intermediate operations without using semicolons. The following methods are used for intermediate operations:

  • map(op) -

This function creates a new stream that applies the provided op function to each element in the original stream.

  • filter(cond) -

This function returns a new stream only containing original stream elements that satisfy the condition passed as parameter cond and determined by the predicate.

  • sorted() -

This function returns the original stream with sorted elements.

The result of terminal operations is either void or not a stream. They're called terminals because once you've used a terminal stream, you can't chain any more operations without starting from scratch.

The following are some terminal operations:

  • collect() -

This function returns an intermediate operation result for the original stream.

  • forEach() -

The void method helps for iterating through the stream element.

  • reduce() -

This function provides a result based on a sequence of elements in the original stream.

Stream.map() Examples

Here are some examples that illustrate how the map() method works.

1. A List of Strings to Uppercase
Here is a simple Java example showing how to convert a list of Strings to uppercase.

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

public class TestJava8 {

    public static void main(String[] args) {

        List<String> alpha = Arrays.asList("a", "b", "c", "d");
List<String> collect = alpha.stream().map(String::toUpperCase).collect(Collectors.toList());
        System.out.println(collect);
    }
}

Output:
[A, B, C, D]

2. Counting String Length

Simple Java example to map string length in place of string.

import java.util.*;
public class lengt_count {
public static void main(String[] args)
{
System.out.println("Output : ");
List<String> list = Arrays.asList("Hello", "world", "boardinfinity",
"Computer", "machine" );
list.stream().map(str -> str.length()).forEach(System.out::println);
}
}

Output :
5
5
13
8
7

write your code here: Coding Playground