Error message here!

Hide Error message here!

忘记密码?

Error message here!

请输入正确邮箱

Hide Error message here!

密码丢失?请输入您的电子邮件地址。您将收到一个重设密码链接。

Error message here!

返回登录

Close

About using streamd

LonZyuan 2021-01-14 21:15:52 阅读数:2 评论数:0 点赞数:0 收藏数:0

introduction

Stream yes Java8 , which deals with key abstractions of collections , It specifies what you want to do with the collection , You can perform very complex lookups 、 Operations such as filtering and mapping data . Use Stream API Operates on the collection data , It's kind of like using SQL Executed database query . You can also use Stream API To execute operations in parallel . In short ,Stream API Provides an efficient and easy-to-use way to process data .

from :https://blog.csdn.net/y_k_y/article/details/84633001

characteristic :

        1 . It's not a data structure , It doesn't save data .

        2. The original data source will not be modified , It will save the data after operation to another object .( Reservations : After all peek Method can modify the elements in the stream )

        3. Lazy evaluation , Flow in the middle of the process , It's just a record of the operation , Not immediately executed , It takes until the termination operation is performed before the actual calculation is performed .

Create stream

// Get sequential flow 
Stream stream = new ArrayList().stream();
// Get parallel flow 
Stream parallelStream = new ArrayList().parallelStream();
// Array into stream 
Integer[] nums = {1,2,3};
Stream<Integer> streamArray = Arrays.stream(nums);

 

assignment

// of Method assignment directly 
Stream<Integer> stream1 = Stream.of(1,2,3,4,5);
stream1.forEach(System.out::println); // 1 2 3 4 5
// iterate Generating infinite ordered flows , Its main function is to abstract iterative logic 
Stream<Integer> stream2 = Stream.iterate(2, x -> x * 2).limit(5);
stream2.forEach(System.out::println); // 2 4 8 16 32
// generate Generating an infinite sequence of unordered flows , Each of these elements is generated by the provider . This applies to generating a constant flow , Random element flow, etc .
Stream<Integer> stream3 = Stream.generate(new Random()::nextInt).limit(2);
stream3.forEach(System.out::println); //15356208 -2042159

 

Screening and slicing

filter: Filter some elements in the stream limit(n): obtain n Elements skip(n): skip n Elements distinct: Through the elements in the stream hashCode() and equals() Remove duplicate elements

Integer[] nums = {1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9, 10, 10};
Stream<Integer> streamArray = Arrays.stream(nums);
Stream<Integer> result = streamArray
.distinct() // duplicate removal 1 2 3 4 5 6 7 8 9 10
.filter(i -> (i > 2)) // Filtered out value >2 The elements of 3 4 5 6 7 8 9 10
.skip(2) // Skip the former 2 Elements 5 6 7 8 9 10
.limit(4); // Before acquisition 4 Elements 5 6 7 8

 

mapping

map: Receive a function as an argument , This function is applied to each element , And map it to a new element

Integer[] nums = {1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9, 10, 10};
Stream<Integer> streamArray = Arrays.stream(nums);
List<String> strings = streamArray
.map(String::valueOf) // adopt String.valueOf() Method to convert each element to a string 
.collect(Collectors.toList()); // Transfer to List aggregate 

flatMap: Receive a function as an argument , Replace each value in the stream with another stream , Then connect all streams into one stream

Stream<String> streamArray2 = Stream.of("1.2.3", "4.5.6", "7.8.9");
List<String> result = streamArray2
.flatMap(s -> Arrays.stream(s.split("\\."))) // Replace each value in the stream with another stream , Then divide 
 .collect(Collectors.toList());
// 1 2 3 4 5 6 7 8 9

 

Sort

sorted(): Natural ordering , Elements in the stream need to implement Comparable Interface

List<String> list = Arrays.asList("a", "d", "c", "b", "x");
List<String> result = list.stream()
.sorted()
.collect(Collectors.toList());
// a b c d x

sorted(Comparator com): Custom sort , Customize Comparator collator

Person s1 = new Person("aa", 10);
Person s2 = new Person("bb", 20);
Person s3 = new Person("aa", 30);
Person s4 = new Person("dd", 40);
List<Person> personList = Arrays.asList(s1, s2, s3, s4);
// Custom sort : First by name , If the names are the same, they are in ascending order of age 
personList.stream().sorted(
      (o1, o2) -> {
           if (o1.getName().equals(o2.getName())) {
               return o1.getAge() - o2.getAge();
          } else {
               return o1.getName().compareTo(o2.getName());
          }
      }
).forEach(person -> System.out.println(person.getName()));

consumption

peek: As in map, You can get every element in the stream . but map It received a Function expression , There is a return value ; and peek Receive is Consumer expression , no return value

Person s1 = new Person("aa", 10);
Person s2 = new Person("bb", 20);
Person s3 = new Person("aa", 30);
Person s4 = new Person("dd", 40);
List<Person> personList = Arrays.asList(s1, s2, s3, s4);
​
personList.stream()
.peek(person -> person.setAge(100))
.forEach(person -> System.out.println(person.getName()+":"+person.getAge()));

 

matching

allMatch: Receive one Predicate function , When every element in the stream matches the assertion true, Otherwise return to false

List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5);
// Are all the elements in it satisfied >10 This condition 
boolean result = integers.stream().allMatch(i -> i > 10); // false

noneMatch: Receive one Predicate function , When every element in the stream does not match the assertion true, Otherwise return to false

List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
// Are all the elements in it No Satisfy >10 This condition 
boolean result = list.stream().noneMatch(i -> i > 10); // true

anyMatch: Receive one Predicate function , As long as there are elements in the stream that satisfy the assertion, returns true, Otherwise return to false

List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 11);
// Is there any element in it that satisfies >10 This condition 
boolean result = list.stream().anyMatch(i -> i > 10); // true

findFirst: Returns the first element in the stream

List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 11);
// Optional Object is often used to solve the null pointer problem 
Optional<Integer> optional = list.stream().findFirst();
System.out.println(optional.get()); // 1

count: Returns the total number of elements in the stream

List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 11);
Long result = list.stream().count();
System.out.println(result); // 6

max: Returns the maximum value of the element in the stream

List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 11);
Integer max = list.stream().max(Integer::compareTo).get(); // 11

min: Returns the minimum value of the element in the stream

List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 11);
Integer min = list.stream().min(Integer::compareTo).get(); // 1

 

 

Copyright statement
In this paper,the author:[LonZyuan],Reprint please bring the original link, thank you