This is the 28th day of my participation in Gwen Challenge

The traditional way

// Put the eligible items into the new collection, possibly multiple times
List<String> list = new ArrayList<>();
List<String> list2 = new ArrayList<>();
list.add("Zhang");
list.add("Bill");
list.add("Lili Zhang");
for (String s : list) {
    if (s.startsWith("Zhang")&&s.length()==3){
        list2.add(s);
    }
}
System.out.println(list2);
Copy the code

Stream flow way

list.stream().filter(name -> name.startsWith("Zhang")).filter(name -> name.length() == 3).forEach(name -> System.out.println(name));
// Result Zhang Lili
Copy the code

When working with multiple elements, first put together a “model” step scheme for performance and convenience, and then execute it

Java.util.stream. stream is the most common new stream interface added to Java 8. Getting a stream is very simple. There are several common ways to get a stream:

  • All collections can get streams using the stream default
  • The lens method of the Stream interface gets the corresponding Stream of the array

1. Get stream 1)

2) Static

Stream

returns a sequential Stream whose elements are the specified values

// Convert the collection to a Stream
List<String> list = new ArrayList<>();
Stream<String> s1 = list.stream();

Set<String> set  = new HashSet<>();
Stream<String> s2 = set.stream();

Map<String,String> map = new HashMap<>();
// Get the key and store it in a Set
Set<String> keySet = map.keySet();
Stream<String> s3 =keySet.stream();
// Get the value and store it in a Collection
Collection<String> values = map.values();
Stream<String> s4 = values.stream();
// Get the key-value pairs
Set<Map.Entry<String ,String>> entries = map.entrySet();
Stream<Map.Entry<String,String>> s5 = entries.stream();

// Convert the array to a Stream
Stream<Integer> s6 =Stream.of(1.2.3.4.5);
// Variable argument pass array
String[] arr = {"a"."b"."c"};
Stream<String> s7 = Stream.of(arr);
Copy the code

Commonly used method

  • Delayed methods: The return value type is still the stream interface type, supporting chained calls
  • Finalizing methods: The return value type is no longer a Stream interface type, so chain operations like StringBuilder are no longer supported.

foreach


Stream<String> stream = Stream.of("Zhang"."Bill"."Fifty");
stream.forEach((String name) -> {
    System.out.println(name);
});
stream.forEach(name -> System.out.println(name));
Copy the code

filter


Stream<String> stream1 = stream.filter(name -> name.startsWith("Zhang"));
stream1.forEach(name -> System.out.println(name));
``


map

```java
Stream<String> stream = Stream.of("1"."2"."3");
Stream<Integer> integerStream = stream.map(s -> { returnInteger.parseInt(s); });Copy the code

Count Indicates the number of statistical elements. Limit Indicates the first number of statistical elements

Stream go back to list or set (collect)

List<Integer> collect = stream.map(s -> {
    return Integer.parseInt(s);
}).collect(Collectors.toList());
Copy the code