Junior
What intermediate methods of working with streams do you know?
sobes.tech AI
Answer from AI
Intermediate methods in Java Stream API are operations that return a new stream and allow chaining of data processing. They are lazy, meaning they do not execute until a terminal operation is invoked.
Main intermediate methods:
filter(Predicate)— filters elements based on a condition.map(Function)— transforms elements by applying a function.flatMap(Function)— transforms each element into a stream and flattens the result.distinct()— removes duplicates.sorted()/sorted(Comparator)— sorts elements.limit(long)— limits the number of elements.skip(long)— skips the first N elements.
Example:
List<String> names = Arrays.asList("Anna", "Bob", "Alice", "Brian");
List<String> filtered = names.stream()
.filter(s -> s.startsWith("A"))
.map(String::toUpperCase)
.sorted()
.collect(Collectors.toList());
// filtered will be ["ALICE", "ANNA"]