Junior — Middle
Could you name different types of methods used in data streams?
sobes.tech AI
Answer from AI
In Java, when working with data streams (Streams API), different types of methods are used, which can be conditionally divided into intermediate and terminal methods.
Intermediate methods return a new stream and allow chaining operations, for example:
filter(Predicate)— filtering elements based on a conditionmap(Function)— transforming elementssorted()— sorting elementsdistinct()— removing duplicates
Terminal methods complete the processing and return a result or side effect:
collect(Collector)— collecting elements into a collection or other structureforEach(Consumer)— performing an action for each elementreduce(BinaryOperator)— folding elements into a single valuecount()— counting the number of elements
Example of usage:
List<String> names = Arrays.asList("Anna", "Bob", "Alice", "Brian");
long count = names.stream()
.filter(name -> name.startsWith("A"))
.count();
System.out.println("Number of names starting with A: " + count);