Sobes.tech
Junior — Middle

Can a Stream in Java be considered an example of applying functional programming principles?

sobes.tech AI

Answer from AI

Yes, Stream in Java can be considered an example of implementing functional programming principles.

Main features:

  • Immutability: operations on Stream do not modify the original data but create new streams.
  • Lazy evaluation: operations are performed only when necessary (laziness).
  • Functional interfaces: methods accept lambda expressions and functions as parameters (e.g., map, filter).
  • Function composition: you can apply a chain of operations sequentially, forming a data processing pipeline.

Example:

List<String> names = Arrays.asList("Anna", "Bob", "Charlie");
List<String> filtered = names.stream()
    .filter(name -> name.startsWith("A"))
    .map(String::toUpperCase)
    .collect(Collectors.toList());

Thus, the Stream API allows writing declarative, clean, and expressive code in the style of functional programming.