Maison > Java > javaDidacticiel > Comprendre les opérateurs terminaux et intermédiaires dans les flux Java : différences clés et exemples

Comprendre les opérateurs terminaux et intermédiaires dans les flux Java : différences clés et exemples

Barbara Streisand
Libérer: 2024-10-11 10:11:02
original
656 Les gens l'ont consulté

Understanding Terminal vs Intermediate Operators in Java Streams: Key Differences and Examples

1. Overview of Java Streams

Java Streams, introduced in Java 8, allow for functional-style operations on sequences of elements. They provide a powerful way to process collections of data in a more declarative and readable manner.

1.1 What Are Terminal Operators?

Terminal Operators are operations that mark the end of a stream pipeline. They trigger the processing of the data within the stream and produce a result or a side effect. Once a terminal operator is called, the stream is considered consumed, and no further operations can be performed on it.

Common Examples of Terminal Operators:

  • forEach(): Iterates over each element and performs an action.
  • collect(): Gathers the elements of the stream into a collection or other forms.
  • reduce(): Combines elements of the stream into a single result.
  • count(): Counts the number of elements in the stream.

Example Code:

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class TerminalOperatorExample {
    public static void main(String[] args) {
        List<String> names = Arrays.asList("Alice", "Bob", "Charlie");

        // Terminal operator: forEach
        names.stream().forEach(name -> System.out.println("Name: " + name));

        // Terminal operator: collect
        List<String> filteredNames = names.stream()
                                           .filter(name -> name.startsWith("A"))
                                           .collect(Collectors.toList());
        System.out.println("Filtered Names: " + filteredNames);
    }
}
Copier après la connexion

Demo Results:

Name: Alice
Name: Bob
Name: Charlie
Filtered Names: [Alice]
Copier après la connexion

1.2 What Are Intermediate Operators?

Intermediate Operators are operations that transform a stream into another stream. They do not trigger any processing until a terminal operator is invoked. These operators are used to build a pipeline of operations, allowing for efficient data processing and manipulation.

Common Examples of Intermediate Operators:

  • filter(): Filters elements based on a predicate.
  • map(): Transforms each element using a function.
  • sorted(): Sorts elements according to a comparator.
  • distinct(): Removes duplicate elements.

Example Code:

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class IntermediateOperatorExample {
    public static void main(String[] args) {
        List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "Alice");

        // Intermediate operators: filter and map
        List<String> transformedNames = names.stream()
                                              .filter(name -> name.startsWith("A"))
                                              .map(String::toUpperCase)
                                              .distinct()
                                              .collect(Collectors.toList());
        System.out.println("Transformed Names: " + transformedNames);
    }
}
Copier après la connexion

Demo Results:

Transformed Names: [ALICE]
Copier après la connexion

2. Key Differences Between Terminal and Intermediate Operators

Understanding the differences between these operators is crucial for effective stream processing.

2.1 Purpose and Functionality

  • Terminal Operators : End the stream pipeline, trigger processing, and produce a result or side effect. They are designed to perform actions and generate output from the stream.
  • Intermediate Operators : Transform the stream into another stream without performing any processing on the data. They are used to create a pipeline of operations that are executed once a terminal operator is invoked.

2.2 Execution and Performance

  • Terminal Operators : Cause the processing of the entire stream pipeline. They are executed only once and can be computationally expensive depending on the operation.
  • Intermediate Operators : Are lazy and do not perform any processing until a terminal operator is invoked. This allows for efficient chaining of operations without unnecessary computations.

2.3 Mutability and State

  • Terminal Operators : May produce side effects or modify external state. For example, forEach() can print elements to the console or update an external collection.
  • Intermediate Operators : Are stateless and do not modify external state. They only work on the data within the stream and produce a new stream as output.

3. Conclusion

Understanding Terminal and Intermediate Operators in Java Streams is crucial for writing efficient and readable code. Terminal Operators complete the stream processing pipeline, while Intermediate Operators build and transform the pipeline. By leveraging these operators effectively, you can handle data processing tasks in a more declarative and functional manner.

If you have any questions or need further clarification, feel free to comment below!

Read posts more at : Understanding Terminal vs Intermediate Operators in Java Streams: Key Differences and Examples

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

source:dev.to
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Derniers articles par auteur
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal