Java -Stream API Introduction with Example

Java 8 introduced the concept of stream, Stream represents a sequence of objects from a source, which supports aggregate operations. 

Following are the characteristics of a Stream −

  1. It takes input from the Collections, Arrays, or I/O channels.
  2. It never stores the elements.
  3. Stream supports intermediate operations like filter, map, limit, reduce, find, match, and so on.
  4. Streams only provide the result as per the pipelined methods.
  5. Stream operations do the iterations internally over the source elements provided.

Different Operations On Streams-

 forEach

The stream has provided a new method ‘forEach’ to iterate each element of the stream
 
List<Integer> obj = new ArrayList<>();
obj.add(7);
obj.add(12);
obj.add(3);
obj.add(5);
obj.forEach((o) -> System.out.println("Item : " + o));

map

The map method is used to map the items in the collection to other objects according to the Function passed as the argument.

List<String> obj = Arrays.asList("h", "bh", "ca", "d");
List<String> collectUpperCaseLetters = obj.stream().
map(String::toUpperCase).collect(Collectors.toList());
System.out.println(collectUpperCaseLetters);

filter

The ‘filter’ method is used to eliminate elements based on criteria.
 
List<String> names = Arrays.asList("Sibin", "Rahul", "Jose");
List<String> filteredNames = names.stream().filter(s -> s.startsWith("S")).
collect(Collectors.toList());
System.out.println(filteredNames); 

sorted

The sorted method is used to sort the stream.
 
List<String> names = Arrays.asList("Sibin", "Rahul", "Jose");
List<String> result = names.stream().sorted().collect(Collectors.toList());
System.out.println(result);

reduce

The reduce method is used to reduce the elements of a stream to a single value.

int[] array = {2,4,5,7,2};
Arrays.stream(array).reduce((x,y) -> x+y).ifPresent(s -> System.out.println(s));


Program to reveal the use of Stream

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

public class StreamTest {

public static void main(String[] args) {
// demonstration of forEach method
List<Integer> obj = new ArrayList<>();
obj.add(7);
obj.add(12);
obj.add(3);
obj.add(5);
obj.forEach((o) -> System.out.println(o));
/* End */

// demonstration of map method
List<String> alplalist = Arrays.asList("h", "bh", "ca", "d");
List<String> collectUpperCaseLetters = alplalist.stream().
map(String::toUpperCase).collect(Collectors.toList());
System.out.println(collectUpperCaseLetters);
/* End */

// demonstration of filter method
List<String> names = Arrays.asList("Sibin", "Rahul", "Jose");
List<String> filteredNames = names.stream().
filter(s -> s.startsWith("S")).collect(Collectors.toList());
System.out.println(filteredNames);
/* End */

// demonstration of sorted method
List<String> namelist = Arrays.asList("Sibin", "Rahul", "Jose");
List<String> result = namelist.stream().sorted().
collect(Collectors.toList());
System.out.println(result);
/* End */

// demonstration of reduce method
int[] array = { 2, 4, 5, 7, 2 };
Arrays.stream(array).reduce((x, y) -> x + y).
ifPresent(s -> System.out.println(s));
/* End */
}
}

Output: 

7
12
3
5
[H, BH, CA, D]
[Sibin]
[Jose, Rahul, Sibin]
20

Comments

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

ReactJS - Bootstrap - Buttons

Spring Core | BeanFactoryPostProcessor | Example

Spring Boot 3 + Spring Security 6 + Thymeleaf - Registration and Login Example

File Upload, Download, And Delete - Azure Blob Storage + Spring Boot Example

Custom Exception Handling in Quarkus REST API

ReactJS, Spring Boot JWT Authentication Example

Java, Spring Boot Mini Project - Library Management System - Download

Top 5 Java ORM tools - 2024

Java - DES Encryption and Decryption example