What is the flow

Streams do not store elements, and elements can be extracted and manipulated from pipes without iterating over elements in the collection. These pipes are usually grouped together to form a series of pipes that operate by convection

Create a flow

1. Create flows from collections
ArrayList<Integer> list = new ArrayList<>();
Stream<Integer> stream1 = list.stream();

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

Map<String,String> map = new HashMap<>();
Stream<Map.Entry<String, String>> stream3 = map.entrySet().stream();
Copy the code
2. Create a sequence flow
Range = intstream.range (0, 100);Copy the code
3. Create an iteration flow
// The first argument of the iterate() method is an initial value, which is passed as a second argument to the iterate() method. The return value of the iterate() method is then passed as an argument. Iterate (0,e -> ++e).limit(10).foreach (system.out ::println);Copy the code
The build method creates the flow
Builder<String> Builder = stream.builder (); builder.add("a").add("b").accept("c"); Builder.build ().foreach (system.out ::println);Copy the code
Create a stream using an array
Int [] arr = {1, 2, 3, 4, 5}; Stream (arr); // Arrays.stream(arr);Copy the code
6, other
Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13); Stream5 = pattern.compile ("[.,?]+").splitasStream (" String to input "); // Create a Stream object with the regular expression Stream<String> stream5 = pattern.compile ("[.,?]+").splitasstream (" String to input "); ` ` `Copy the code

In the middle of operation

Peek () unmodified view element
long count = IntStream.range(0, 100).
        peek(System.out::println).
        sum();
Copy the code
2 sorted (), distinct ()
Random random = new Random(); Intstream.range (1,100).map (random::nextInt).boxed (). // Elements are automatically boxed with sorted((x,y) -> y.compareTo(x)). // Sort distinct() peek(System.out::println).collect(Collectors.toCollection(ArrayList::new));Copy the code
3. Filter (Predicate) Filters elements
ArrayList<Object> collect = intstream.range (0, 1000). ArrayList<Object> collect = intstream.range (0, 1000). i < e; i++){ if ((e % i) == 0){ return false; } } if(e ==0 || e == 1) return false; return true; }).collect(ArrayList::new, ArrayList::add,ArrayList::addAll); System.out.println(collect);Copy the code
4. Map () maps an element to another element, returning the new element to the stream
Stream<Integer> integerStream = IntStream. range(0, MapToObj (e -> uuid.randomuuid ().toString())). // Map the UUID String to an int and return it to the stream map(String::length);Copy the code
5. FlatMap () is similar to map(), but returns a stream that summarizes the elements in the stream
Stream<String> Stream = intstream.range (0, 100).mapToobj (e -> uuid.randomuuid ().toString()). FlatMap (e -> arrays.stream (e.split("-"))));Copy the code

The final operation

ToArray ()
String[] strings = stream.of ("a", "b", "c"). // Generate an array of the corresponding type. The IntFunction function interface method takes an int as the number of elements toArray(String[]::new); Object[] objects = stream.of ("a", "b", "c").Copy the code
2. Loop forEach(Consumer)
IntStream. Range (0100). The forEach (System. Out: : println);Copy the code
3, save to collect()
Stream.iterate(0,e -> ++e).limit(100).collect(Collectors.toCollection(ArrayList::new));
Copy the code
4. Combine Reduce (BinaryOperator)

Take two arguments and return a result as the first argument in the next iteration

// Count elements Optional<Integer> reduce = stream.iterate (0, e -> ++e).limit(100).reduce(Integer::sum); // Find the largest element Optional<Integer> reduce1 = stream.iterate (0, e -> ++e).limit(100).reduce((x, y) -> x <= y? y : x);Copy the code
5, match,

All elements in the allMatch(Predicate) stream return true and true and false otherwise

An element of anyMatch(Predicate) that meets the conditions returns true,

NoneMatch (Predicate) Returns true when all elements do not meet the return condition

Return true Boolean b = intstream.range (11, 21). AllMatch (e -> e > 10); Boolean b1 = intstream.range (11, 21).anymatch (e -> e > 17); Boolean b2 = intstream.range (11, 21). NoneMatch (e -> e > 21);Copy the code
6, find

FindFirst () returns the first in the element stream

The findAny() non-parallel stream selects the first element in the stream

OptionalInt first = IntStream.range(11, 21).findFirst();
OptionalInt any = IntStream.range(11, 21).findAny();
Copy the code
7. Maximum, minimum, count
Stream<Date> dateStream = Stream.of(new Date(2020, 10, 1), new Date(2020, 10, 2), new Date(2020, 10, 3));
long count = dateStream.count();
System.out.println(count);

Stream<Date> dateStream1 = Stream.of(new Date(2020, 10, 1), new Date(2020, 10, 2), new Date(2020, 10, 3));
Optional<Date> max = dateStream1.max(Date::compareTo);
System.out.println(max);

Stream<Date> dateStream2 = Stream.of(new Date(2020, 10, 1), new Date(2020, 10, 2), new Date(2020, 10, 3));
Optional<Date> min = dateStream2.min((x,y) -> x.compareTo(y));
System.out.println(min);
Copy the code
8. Digital stream aggregation function
OptionalInt max = IntStream.range(0, 100).max();
OptionalDouble average = IntStream.range(0, 100).average();
OptionalInt min = IntStream.range(0, 100).min();
Copy the code