Junior — Middle
What methods of Stream API do you use most often in your projects?
sobes.tech AI
Answer from AI
The most commonly used methods in projects with Stream API are:
filter()— for filtering elements based on a condition.map()— for transforming elements into another type or view.collect()— for gathering results into collections, such as a list or set.forEach()— for performing actions on each element, such as printing to the screen.sorted()— for sorting elements.distinct()— for removing duplicates.limit()andskip()— for limiting and skipping elements.
Example:
List<String> names = Arrays.asList("Anna", "Bob", "Alice", "Brian");
List<String> filtered = names.stream()
.filter(name -> name.startsWith("A"))
.sorted()
.collect(Collectors.toList());
System.out.println(filtered); // [Alice, Anna]