java.util.stream


1. Filter a List of Strings

List<String> names = Arrays.asList("John", "Mary", "Bob", "Alice");
List<String> filteredNames = names.stream()
    .filter(name -> name.startsWith("M"))
    .collect(Collectors.toList());

2. Map a List of Integers to Squares

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> squares = numbers.stream()
    .map(num -> num * num)
    .collect(Collectors.toList());

3. Find the Maximum Value in a List

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
int max = numbers.stream()
    .max(Comparator.naturalOrder())
    .get();

4. Find the First Element in a List that Matches a Condition

List<String> names = Arrays.asList("John", "Mary", "Bob", "Alice");
String firstMatchingName = names.stream()
    .filter(name -> name.startsWith("M"))
    .findFirst()
    .orElse(null);

5. Check if Any Element in a List Matches a Condition

6. Count the Number of Elements in a List that Match a Condition

7. Reduce a List of Numbers to a Single Value (Sum)

8. Reduce a List of Strings to a Single String (Concatenation)

9. Group Elements by a Key

10. Partition Elements into Two Groups

11. FlatMap a List of Lists into a Single List

12. Sort a List of Objects

13. Limit the Number of Results

14. Skip a Number of Results

15. Convert a Stream to an Array

16. Convert a Stream to a List

17. Convert a Stream to a Set

18. Convert a Stream to a Map

19. Parallel Stream

20. Sequential Stream

21. Unordered Stream

22. Distinct Stream

23. Sorted Stream

24. Find First

25. Find Any

26. Max

27. Min

28. Count

29. Reduce

30. Collectors

31. Joining