Java Tutorial

Operators

Flow Control

String

Number and Date

Built-in Classes

Array

Class and Object

Inheritance and Polymorphism

Exception Handling

Collections, Generics and Enumerations

Reflection

Input/Output Stream

Annotation

Use Java 8's New Stream To Manipulate Collections

Java 8 introduced the Stream API, which allows for functional-style operations on collections, arrays, and other data sources. The Stream API provides a more concise, expressive, and readable way to manipulate collections. Here's a tutorial on using Java 8's Stream API to manipulate collections.

  • Import necessary classes:

First, import the necessary classes from the java.util package.

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
  • Create a sample collection:

Create a sample collection, such as a List of integers, to demonstrate the use of the Stream API.

List<Integer> numbers = new ArrayList<>();
numbers.add(1);
numbers.add(2);
numbers.add(3);
numbers.add(4);
numbers.add(5);
  • Filter a collection:

You can use the filter() method with a lambda expression to create a new collection containing elements that meet a specific condition. For example, create a new list containing only the even numbers from the numbers list.

List<Integer> evenNumbers = numbers.stream()
                                   .filter(n -> n % 2 == 0)
                                   .collect(Collectors.toList());

System.out.println(evenNumbers); // Output: [2, 4]
  • Transform a collection:

The map() method applies a function to each element in the collection and returns a new collection with the transformed elements. For example, create a new list containing the squares of the numbers in the numbers list.

List<Integer> squaredNumbers = numbers.stream()
                                      .map(n -> n * n)
                                      .collect(Collectors.toList());

System.out.println(squaredNumbers); // Output: [1, 4, 9, 16, 25]
  • Reduce a collection:

The reduce() method can be used to aggregate elements in a collection based on a specific operation. For example, calculate the sum of all numbers in the numbers list.

int sum = numbers.stream()
                 .reduce(0, (a, b) -> a + b);

System.out.println(sum); // Output: 15
  • Sort a collection:

You can use the sorted() method to sort elements in a collection. By default, the sorted() method uses the natural order of elements. You can also provide a custom comparator as an argument.

List<Integer> sortedNumbers = numbers.stream()
                                     .sorted()
                                     .collect(Collectors.toList());

System.out.println(sortedNumbers); // Output: [1, 2, 3, 4, 5]

List<Integer> sortedInReverse = numbers.stream()
                                       .sorted((a, b) -> b - a)
                                       .collect(Collectors.toList());

System.out.println(sortedInReverse); // Output: [5, 4, 3, 2, 1]
  • Find elements in a collection:

You can use methods such as findFirst() and findAny() to find elements in a collection that meet a specific condition.

int firstEven = numbers.stream()
                       .filter(n -> n % 2 == 0)
                       .findFirst()
                       .orElse(-1);

System.out.println(firstEven); // Output: 2

This tutorial demonstrated how to use Java 8's Stream API to manipulate collections. By leveraging the Stream API and lambda expressions, you can write more concise and readable code when filtering, transforming, reducing, sorting, and finding elements in collections.