Sobes.tech
Middle

Millised operatsioonid on voogudes olemas ja millised operatsioonitüübid on olemas?

sobes.tech AI

Vastus AI-lt

In Java 8+, streams provide a set of operations for processing collections of data.

Types of operations:

  1. Intermediate: Return a new stream. Can be executed lazily. Allow chaining of operations. Include:

    • filter()
    • map()
    • flatMap()
    • distinct()
    • sorted()
    • peek()
    • limit()
    • skip()
  2. Terminal: Complete the stream's work, perform processing, and return a result or cause a side effect. After executing a terminal operation, the stream cannot be reused. Include:

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

Example of a chain of operations:

// Intermediate operations (filter, map)
// Terminal operation (collect)
List<String> filteredList = originalList.stream()
    .filter(s -> s.startsWith("A")) // Filter elements starting with "A"
    .map(String::toUpperCase) // Convert to uppercase
    .collect(Collectors.toList()); // Collect results into a list