Java LongStream forEach() Method

The forEach() method in Java, part of the java.util.stream.LongStream interface, is used to perform an action for each element of the stream. This method is a terminal operation that processes each element of the stream, typically for side-effect operations like printing or modifying external data structures.

Table of Contents

  1. Introduction
  2. forEach() Method Syntax
  3. Understanding forEach()
  4. Examples
    • Basic Usage
    • Using forEach() with Side-Effects
  5. Real-World Use Case
  6. Conclusion

Introduction

The forEach() method allows you to iterate over all elements of the stream and perform an action on each element. It is a terminal operation, meaning that it consumes the stream and performs the specified action on each element.

forEach() Method Syntax

The syntax for the forEach() method is as follows:

void forEach(LongConsumer action)

Parameters:

  • action: A LongConsumer that represents the action to be performed on each element of the stream.

Returns:

  • This method does not return a value.

Throws:

  • This method does not throw any exceptions.

Understanding forEach()

The forEach() method processes each element of the stream and applies the specified action to it. This is useful for performing operations that have side-effects, such as printing elements, updating external data structures, or logging information.

Examples

Basic Usage

To demonstrate the basic usage of forEach(), we will create a LongStream and use forEach() to print each element.

Example

import java.util.stream.LongStream;

public class ForEachExample {
    public static void main(String[] args) {
        LongStream stream = LongStream.of(1L, 2L, 3L, 4L, 5L);

        // Use forEach() to print each element
        stream.forEach(System.out::println);
    }
}

Output:

1
2
3
4
5

Using forEach() with Side-Effects

This example shows how to use forEach() to perform side-effects, such as updating a shared data structure.

Example

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

public class ForEachWithSideEffectsExample {
    public static void main(String[] args) {
        List<Long> list = new ArrayList<>();
        LongStream stream = LongStream.of(10L, 20L, 30L, 40L, 50L);

        // Use forEach() to add each element to the list
        stream.forEach(list::add);

        // Print the elements of the list
        list.forEach(System.out::println);
    }
}

Output:

10
20
30
40
50

Real-World Use Case

Logging Transaction Amounts

In real-world applications, the forEach() method can be used to log transaction amounts from a stream of transaction values.

Example

import java.util.stream.LongStream;

public class ForEachTransactionExample {
    public static void main(String[] args) {
        LongStream transactionAmounts = LongStream.of(1000L, 2000L, 1500L, 3000L, 2500L);

        // Use forEach() to log each transaction amount
        transactionAmounts.forEach(amount -> System.out.println("Transaction Amount: " + amount));
    }
}

Output:

Transaction Amount: 1000
Transaction Amount: 2000
Transaction Amount: 1500
Transaction Amount: 3000
Transaction Amount: 2500

Conclusion

The LongStream.forEach() method is used to perform an action for each element of the stream. This method is particularly useful for iterating over elements and performing side-effects, such as printing or updating external data structures. By understanding and using this method, you can efficiently manage and process streams of values in your Java applications, ensuring that actions are performed on each element as needed.

Comments