[Interview Question] Detailed introduction to the new feature Stream of JDK1.8

Java 8 introduced the Stream API, which provides a functional programming approach to processing collection data. The Stream API provides a wealth of operation methods, which can filter, map, sort and other operations on collections. The following are some commonly used Stream collection operations:

  1. Filter: Use filterthis method to filter out the elements in the collection that meet the specified conditions. For example:
    List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    List<Integer> evenNumbers = numbers.stream()
                                       .filter(n -> n % 2 == 0)
                                       .collect(Collectors.toList());
    

    In the above code, filterthe method filters out even numbers in the list.

  2. Map: Use mapmethods to transform elements in a collection. For example:
    List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
    List<Integer> nameLengths = names.stream()
                                     .map(String::length)
                                     .collect(Collectors.toList());
    

    In the above code, mapthe method converts each element in the string list to its length.

  3. Sort: Use sortedmethods to sort elements in a collection. For example:
    List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
    List<String> sortedNames = names.stream()
                                   .sorted()
                                   .collect(Collectors.toList());
    

    In the above code, sortedthe method alphabetically sorts the string list.

  4. Match: Use the anyMatch, allMatchand noneMatchmethods to determine whether the elements in the collection meet specified conditions. For example:
    List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
    boolean anyMatch = numbers.stream().anyMatch(n -> n > 3);
    boolean allMatch = numbers.stream().allMatch(n -> n > 0);
    boolean noneMatch = numbers.stream().noneMatch(n -> n < 0);
    

    In the above code, anyMatchthe method determines whether there are elements greater than 3 in the set, allMatchthe method determines whether all elements in the set are greater than 0, and noneMatchthe method determines whether there are no elements less than 0 in the set.

  5. Reduce: Use reducemethods to perform reduction operations on elements in a collection, such as summing, maximizing, etc. For example:
    List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
    int sum = numbers.stream().reduce(0, (a, b) -> a + b);
    Optional<Integer> max = numbers.stream().reduce(Integer::max);
    

    In the above code, reducethe method sums the elements in the collection, the first parameter is the initial value, and the second parameter is the reduction operation.

    These are just some common operations provided by the Stream API, and there are other rich operation methods that can be used according to actual needs. It should be noted that Stream operations are lazily evaluated and will only be executed when the operation is terminated (such as collect, forEachetc.).

Guess you like

Origin blog.csdn.net/Feixiangdechenyu/article/details/131728180