Streams

Java streams are a powerful and expressive feature introduced in Java 8 to facilitate functional-style programming and to enable efficient and parallel processing of collections.

Intermediate Operations:

Intermediate operations are operations that transform or manipulate the stream and return a new stream. They are often used in a chain to build a pipeline of operations.

  1. Filter:

    • Syntax: stream.filter(predicate)
    • It filters the elements of the stream based on the provided predicate.
  2. Map:

    • Syntax: stream.map(mapper)
    • It transforms each element of the stream using the provided mapper function.
  3. FlatMap:

    • Syntax: stream.flatMap(mapper)
    • It flattens the elements after applying a function that produces a stream of values for each element.
  4. Sorted:

    • Syntax: stream.sorted(comparator)
    • It returns a stream with elements sorted according to the specified comparator.
  5. Distinct:

    • Syntax: stream.distinct()
    • It returns a stream with unique elements, eliminating duplicates.
  6. Peek:

    • Syntax: stream.peek(consumer)
    • It allows you to perform an action on each element of the stream without modifying the elements.

Terminal Operations:

Terminal operations are operations that produce a result or a side-effect. They trigger the execution of the stream pipeline.

  1. forEach:

    • Syntax: stream.forEach(consumer)
    • It performs an action for each element in the stream.
  2. toArray:

    • Syntax: stream.toArray()
    • It converts the stream elements into an array.
  3. reduce:

    • Syntax: stream.reduce(identity, accumulator, combiner)
    • It performs a reduction on the elements using an associative accumulation function and returns an Optional.
  4. collect:

    • Syntax: stream.collect(collector)
    • It performs a mutable reduction and transforms the elements of the stream into a different form, usually a collection like a list or a set.
  5. count:

    • Syntax: stream.count()
    • It returns the count of elements in the stream.
  6. findFirst:

    • Syntax: stream.findFirst()
    • It returns the first element of the stream as an Optional.
  7. anyMatch, allMatch, noneMatch:

    • Syntax: stream.anyMatch(predicate), stream.allMatch(predicate), stream.noneMatch(predicate)
    • These operations check if any, all, or none of the elements match the given predicate.
  8. min and max:

    • Syntax: stream.min(comparator), stream.max(comparator)
    • These operations return the minimum and maximum element of the stream based on the provided comparator.
  9. findAny:

    • Syntax: stream.findAny()
    • It returns any element of the stream as an Optional.
  10. toArray:

    • Syntax: stream.toArray()
    • It converts the stream elements into an array.

Example:

List<String> words = Arrays.asList("hello", "world", "java", "stream", "example");

// Example using stream operations
long count = words.stream()
                .filter(word -> word.length() > 4)
                .map(String::toUpperCase)
                .sorted()
                .count();

System.out.println("Count: " + count);