Java IntStream filter() Method

The filter() method in Java, part of the java.util.stream.IntStream interface, is used to produce a new IntStream that contains only those elements of the original stream that match a given predicate. This method is useful for removing unwanted elements from a stream based on a specified condition.

Table of Contents

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

Introduction

The filter() method returns a stream consisting of the elements of the original stream that match a given 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:

IntStream filter(IntPredicate predicate)

Parameters:

  • predicate: An IntPredicate that represents the condition to be checked against the elements of the stream.

Returns:

  • A new IntStream consisting of the elements of the original stream that match the given 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 resulting stream only if it matches the given predicate. This allows for selective inclusion of elements based on a specified condition.

Examples

Basic Usage

To demonstrate the basic usage of filter(), we will create an IntStream and use filter() to include only even numbers.

Example

import java.util.stream.IntStream;

public class FilterExample {
    public static void main(String[] args) {
        IntStream intStream = IntStream.of(1, 2, 3, 4, 5, 6);

        // Use filter() to include only even numbers
        IntStream evenStream = intStream.filter(n -> n % 2 == 0);

        // Print the filtered elements
        evenStream.forEach(System.out::println);
    }
}

Output:

2
4
6

Using filter() with Custom Predicates

This example shows how to use filter() with a custom predicate to include only numbers greater than 3.

Example

import java.util.stream.IntStream;

public class CustomPredicateExample {
    public static void main(String[] args) {
        IntStream intStream = IntStream.of(1, 2, 3, 4, 5, 6);

        // Use filter() to include only numbers greater than 3
        IntStream greaterThanThreeStream = intStream.filter(n -> n > 3);

        // Print the filtered elements
        greaterThanThreeStream.forEach(System.out::println);
    }
}

Output:

4
5
6

Real-World Use Case

Filtering Sensor Readings

In real-world applications, the filter() method can be used to filter sensor readings that exceed a certain threshold, ensuring that only significant readings are processed.

Example

import java.util.stream.IntStream;

public class SensorReadingsExample {
    public static void main(String[] args) {
        IntStream sensorReadings = IntStream.of(50, 60, 45, 70, 30, 80, 55);

        // Use filter() to include only readings above 50
        IntStream significantReadings = sensorReadings.filter(reading -> reading > 50);

        // Print the significant readings
        significantReadings.forEach(reading -> System.out.println("Significant reading: " + reading));
    }
}

Output:

Significant reading: 60
Significant reading: 70
Significant reading: 80
Significant reading: 55

Conclusion

The IntStream.filter() method is used to produce a new stream containing only those elements of the original stream that match a given predicate. This method is particularly useful for selectively including elements based on a specified condition. By understanding and using this method, you can efficiently manage and process streams of integer values in your Java applications.

Comments