Java Stream Terminal Operations Examples

In this tutorial, we will learn Java 8 Stream terminal operations with examples.

Stream operations are divided into intermediate and terminal operations. The terminal operations of the Java Stream interface typically return a single value. Terminal operations cannot be chained together, unlike intermediate operations, which return another stream as a result and can be chained together to form a pipeline of operations.

Terminal Stream operations include:

  • anyMatch()
  • allMatch()
  • noneMatch()
  • collect()
  • count()
  • findAny()
  • findFirst()
  • forEach()
  • min()
  • max()
  • reduce()
  • toArray()

1. anyMatch()

The anyMatch() method checks if any element in the stream matches the given predicate. If any match is found, it returns true; otherwise, false.

Code Example

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

public class Main {
    public static void main(String[] args) {
        List<String> stringList = new ArrayList<>();
        stringList.add("Java Guides");
        stringList.add("Python Guides");
        stringList.add("C Guides");

        Stream<String> stream = stringList.stream();
        boolean anyMatch = stream.anyMatch((value) -> value.startsWith("Java"));
        System.out.println(anyMatch);
    }
}

Output

true

Explanation

  • The stream checks if any string in the list starts with "Java". Since "Java Guides" matches the condition, the result is true.

2. allMatch()

The allMatch() method checks if all elements in the stream match the given predicate. It returns true only if all elements satisfy the predicate.

Code Example

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

public class Main {
    public static void main(String[] args) {
        List<String> stringList = new ArrayList<>();
        stringList.add("Java Guides");
        stringList.add("Python Guides");
        stringList.add("C Guides");

        Stream<String> stream = stringList.stream();
        boolean allMatch = stream.allMatch((value) -> value.contains("Guides"));
        System.out.println(allMatch);
    }
}

Output

true

Explanation

  • All strings contain the word "Guides", so the method returns true.

3. noneMatch()

The noneMatch() method checks if no elements in the stream match the given predicate. It returns true if none of the elements satisfy the condition.

Code Example

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

public class Main {
    public static void main(String[] args) {
        List<String> stringList = new ArrayList<>();
        stringList.add("john");
        stringList.add("tom");

        Stream<String> stream = stringList.stream();
        boolean noneMatch = stream.noneMatch((element) -> "Ramesh".equals(element));
        System.out.println("noneMatch = " + noneMatch);
    }
}

Output

noneMatch = true

Explanation

  • Since no element matches "Ramesh", the method returns true.

4. collect()

The collect() method is used to collect the stream elements into a collection, such as a List, Set, or Map.

Code Example

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

public class Main {
    public static void main(String[] args) {
        List<String> stringList = new ArrayList<>();
        stringList.add("one");
        stringList.add("two");
        stringList.add("three");
        stringList.add("four");
        stringList.add("five");

        Stream<String> stream = stringList.stream();
        List<String> uppercaseList = stream.map(String::toUpperCase).collect(Collectors.toList());
        System.out.println(uppercaseList);
    }
}

Output

[ONE, TWO, THREE, FOUR, FIVE]

Explanation

  • The stream converts each string to uppercase and collects them into a new list.

5. count()

The count() method returns the number of elements in the stream.

Code Example

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

public class Main {
    public static void main(String[] args) {
        List<String> stringList = new ArrayList<>();
        stringList.add("one");
        stringList.add("two");
        stringList.add("three");
        stringList.add("four");
        stringList.add("five");

        Stream<String> stream = stringList.stream();
        long count = stream.count();
        System.out.println(count);
    }
}

Output

5

Explanation

  • The stream counts the total number of elements and returns 5.

6. findAny()

The findAny() method returns any one element from the stream. It returns an Optional object.

Code Example

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

public class Main {
    public static void main(String[] args) {
        List<String> stringList = new ArrayList<>();
        stringList.add("one");
        stringList.add("two");
        stringList.add("three");

        Stream<String> stream = stringList.stream();
        Optional<String> anyElement = stream.findAny();
        System.out.println(anyElement.get());
    }
}

Output

one

Explanation

  • The stream retrieves any one element from the list, in this case, "one".

7. findFirst()

The findFirst() method retrieves the first element in the stream.

Code Example

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

public class Main {
    public static void main(String[] args) {
        List<String> stringList = new ArrayList<>();
        stringList.add("one");
        stringList.add("two");
        stringList.add("three");

        Stream<String> stream = stringList.stream();
        Optional<String> firstElement = stream.findFirst();
        System.out.println(firstElement.get());
    }
}

Output

one

Explanation

  • The stream retrieves the first element, which is "one".

8. forEach()

The forEach() method performs an action for each element in the stream. It is typically used for printing or other side effects.

Code Example

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

public class Main {
    public static void main(String[] args) {
        List<String> stringList = new ArrayList<>();
        stringList.add("one");
        stringList.add("two");
        stringList.add("three");

        Stream<String> stream = stringList.stream();
        stream.forEach(System.out::println);
    }
}

Output

one
two
three

Explanation

  • The forEach() method prints each element of the stream.

9. min()

The min() method returns the smallest element from the stream based on the specified comparator.

Code Example

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

public class Main {
    public static void main(String[] args) {
        List<String> stringList = new ArrayList<>();
        stringList.add("one");
        stringList.add("two");
        stringList.add("three");

        Stream<String> stream = stringList.stream();
        Optional<String> minElement = stream.min(String::compareTo);
        System.out.println(minElement.get());
    }
}

Output

one

Explanation

  • The stream returns the lexicographically smallest element, "one".

10. max()

The max() method returns the largest element from the stream based on the specified comparator.

Code Example

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

public class Main {
    public static void main(String[] args) {
        List<String> stringList = new ArrayList<>();
        stringList.add("one");
        stringList.add("two");
        stringList.add("three");

        Stream<String> stream = stringList.stream();
        Optional<String> maxElement = stream.max(String::compareTo);
        System.out.println(maxElement.get());
    }
}

Output

two

Explanation

  • The stream returns the lexicographically largest element, "two".

11. reduce()

The reduce() method combines elements in the stream into a single value based on a binary operation.

Code Example

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

public class Main {
    public static void main(String[] args) {
        List<String> stringList = new ArrayList<>();
        stringList.add("one");
        stringList.add("two");
        stringList.add("three");

        Stream<String> stream = stringList.stream();
        Optional<String> reducedValue = stream.reduce((value, combinedValue) -> combinedValue + " + " + value);
        System.out.println

(reducedValue.get());
    }
}

Output

one + two + three

Explanation

  • The stream reduces the elements by concatenating them with " + ".

12. toArray()

The toArray() method converts the elements of the stream into an array.

Code Example

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

public class Main {
    public static void main(String[] args) {
        List<String> stringList = new ArrayList<>();
        stringList.add("one");
        stringList.add("two");
        stringList.add("three");

        Stream<String> stream = stringList.stream();
        Object[] array = stream.toArray();

        for (Object element : array) {
            System.out.println(element);
        }
    }
}

Output

one
two
three

Explanation

  • The stream elements are converted to an array and printed in sequence.

Comments