Java LongStream filter() Method

The filter() method in Java, part of the java.util.stream.LongStream interface, is used to select elements from a stream that match a given predicate. This method is useful when you need to include only certain elements in the stream based on a condition.

Table of Contents

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

Introduction

The filter() method returns a new stream that contains only the elements that match the specified predicate. This method is an intermediate operation, meaning it returns a new stream and does not modify the original stream.

filter() Method Syntax

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

LongStream filter(LongPredicate predicate)

Parameters:

  • predicate: A LongPredicate that represents the condition to be checked against the elements of the stream.

Returns:

  • A new LongStream that contains only the elements that match the predicate.

Throws:

  • This method does not throw any exceptions.

Understanding filter()

The filter() method processes each element of the stream and includes it in the new stream only if it matches the given predicate. If the element does not match the predicate, it is excluded from the new stream. This method is useful for refining streams based on specific conditions.

Examples

Basic Usage

To demonstrate the basic usage of filter(), we will create a LongStream and use filter() to select only the even numbers.

Example

import java.util.stream.LongStream;

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

        // Use filter() to select only even numbers
        LongStream filteredStream = stream.filter(n -> n % 2 == 0);

        // Print the elements of the filtered stream
        filteredStream.forEach(System.out::println);
    }
}

Output:

2
4

Using filter() with Multiple Conditions

This example shows how to use filter() to select elements that match multiple conditions.

Example

import java.util.stream.LongStream;

public class FilterMultipleConditionsExample {
    public static void main(String[] args) {
        LongStream stream = LongStream.of(10L, 20L, 30L, 40L, 50L);

        // Use filter() to select numbers greater than 20 and even
        LongStream filteredStream = stream.filter(n -> n > 20 && n % 2 == 0);

        // Print the elements of the filtered stream
        filteredStream.forEach(System.out::println);
    }
}

Output:

30
40
50

Real-World Use Case

Filtering Transactions Above a Certain Amount

In real-world applications, the filter() method can be used to filter transaction amounts that exceed a certain threshold from a stream of transaction values.

Example

import java.util.stream.LongStream;

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

        long threshold = 2000L;

        // Use filter() to select transactions above the threshold
        LongStream filteredTransactions = transactionAmounts.filter(amount -> amount > threshold);

        // Print the filtered transactions
        filteredTransactions.forEach(System.out::println);
    }
}

Output:

3000
2500

Conclusion

The LongStream.filter() method is used to select elements from a stream that match a given predicate. This method is particularly useful for refining streams based on specific conditions. By understanding and using this method, you can efficiently manage and process streams of values in your Java applications, ensuring that only elements matching your criteria are included.

Comments