Method 1 of creating Stream: through collection
The Collection interface in Java 8 is extended to provide two methods to obtain streams:
-
Public default stream < E > stream(): returns a sequential stream
-
Public default stream < E > parallelstream(): returns a parallel stream
Method 2 of creating Stream: through array
The static method stream() of Arrays in Java 8 can obtain the array stream:
-
Public static < T > stream < T > stream (t [] array): returns a stream
Overloaded form, which can handle arrays of corresponding basic types:
-
Public static intstream (int [] array): returns an integer data stream
-
public static LongStream stream(long[] array): returns a long integer data stream
-
Public static double stream (double [] array): returns a floating-point data stream
Method 3 of creating a Stream: through of()
You can call the static method of() of the Stream class to create a Stream by displaying the value. It can receive any number of parameters.
-
Public static < T > stream < T > of (t... Values): returns a sequential stream
Create Stream mode 4: create infinite Stream
-
Public static < T > stream < T > iterate (final t seed, final unaryoperator < T > F): returns an infinite stream
-
Public static < T > stream < T > generate (supplier < T > s): returns an infinite stream
Intermediate operation of stream
Multiple intermediate operations can be connected to form a pipeline. Unless the termination operation is triggered on the pipeline, the intermediate operation will not perform any processing! When the operation is terminated, it is processed all at once, which is called "lazy evaluation".
public class MiddleTest { @Test public void test05(){ String [] strArr = {"hello","hello","kitty","Bob"}; Arrays.stream(strArr) .flatMap(new Function<String, Stream<?>>() { @Override public Stream<?> apply(String s) { System.out.print(s + " "); System.out.println("---------------"); return Stream.of(s.split("")).distinct(); } }).forEach(System.out::println); } @Test public void test04(){ Stream.of("hello","world") .map(String::toUpperCase) .forEach(System.out::println); } @Test public void test03(){ Stream.of(7,6,1,2,3,4,5,1,2,3,7,8,9) .distinct()//duplicate removal .sorted() //Default sort from small to large // .sorted((Integer::compare) .sorted(Comparator.reverseOrder())//flashback .limit(3)//Limit length .skip(2)//Skip specified quantity .forEach(System.out::println); } @Test public void test02(){ ArrayList<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); list.add(6); list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); list.add(6); Stream<Integer> integerStream = list.stream().filter((e) -> e % 2 == 0); System.out.println("list = " + list); /*//1:Create stream Stream<Integer> stream = list.stream(); //2 Intermediate operation Stream<Integer> stream1 = stream.distinct(); //3: Terminate operation stream1.forEach(System.out::println);*/ } @Test public void test01(){ //1: Create stream Stream<Integer> stream = Stream.of(1,2,3,4,5,6,7); //2: Intermediate operation /*stream.filter(new Predicate<Integer>() { @Override public boolean test(Integer integer) { return integer % 2 == 0; } }).forEach(System.out::println);*/ Stream<Integer> stream1 = stream.filter(integer -> true); //3: Terminate operation stream1.forEach(System.out::println); }
End operation of stream
The terminal operation generates results from the pipeline of the stream. The result can be any value that is not a stream, such as List, Integer, or even void. The stream cannot be used again after it has been terminated.
public class EndTest { @Test public void test04() { List<Integer> list = Stream.of(1, 3, 5, 7, 9, 2, 4) .filter(k -> k % 2 != 0) .collect(Collectors.toList());//Save odd numbers to array System.out.println("list = " + list); } @Test public void test03() { Stream.of(1, 3, 5, 6, 9, 2, 4) .filter(k -> k % 2 == 0) .forEach(System.out::println); Optional<Integer> max = Stream.of(1, 3, 5, 6, 9, 2, 4) .filter(k -> k % 2 == 0) .max(new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return o2 - o1; } }); // .max(Integer::compareTo); System.out.println("max = " + max); } @Test public void test02(){ Optional<Integer> first = Stream.of(1, 3, 7, 9, 2, 4) .sorted() .peek(System.out::println) .findFirst(); System.out.println("first = " + first); /* Stream<Integer> peek = Stream.of(1, 3, 7, 9, 2, 4) .sorted() .peek(System.out::println);*/ // peek.forEach(System.out::println); } @Test public void test01(){ /*boolean b = Stream.of(1, 3, 5, 7, 9, 2, 4) .filter(k -> k % 2 != 0) //filter .allMatch(c -> c % 2 != 0)//Match all ;*/ // System.out.println("b = " + b); Stream.of(1,3,5,7,9,2,4) .filter(k -> k % 2 == 0) .forEach(System.out::println); } }
practice
Now there are two ArrayList collections to store the names of multiple members in the team. The traditional for loop (or enhanced for loop) is required to perform the following steps in turn:
-
The first team only needs the name of the member whose name is three words; Store in a new collection.
-
After the first team screening, only the first three people; Store in a new collection.
-
The second team only needs the names of members surnamed Zhang; Store in a new collection.
-
After the second team is screened, do not use the first two people; Store in a new collection.
-
Merge the two teams into one team; Store in a new collection.
-
Create a Person object based on the name; Store in a new collection.
-
Print the Person object information of the whole team.
public class Person { private String name; public Person(){} public Person(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + '}'; } }
public class PersonTest { public static void main(String[] args) { //First team ArrayList<String> one = new ArrayList<>(); one.add("Delireba"); one.add("Song Yuanqiao"); one.add("Su Xinghe"); one.add("Stone breaks the sky"); one.add("Shi Zhongyu"); one.add("Laozi"); one.add("Zhuangzi"); one.add("master hongqi"); //Second team ArrayList<String> two = new ArrayList<>(); two.add("Gulinaza"); two.add("zhang wuji"); two.add("Zhao Liying"); two.add("Zhang Sanfeng"); two.add("Nicholas Zhao Si"); two.add("Zhang Tianai"); two.add("Zhang Ergou"); //The first team only needs the name of the member whose name is three words; Store in a new collection. List<String> list1 = one.stream() .filter(name -> name.length() == 3) .collect(Collectors.toList()); System.out.println("list1 = " + list1); //After the first team screening, only the first three people; Store in a new collection. List<String> list2 = one.stream() .limit(3) .collect(Collectors.toList()); System.out.println("list2 = " + list2); //The second team only needs the names of members surnamed Zhang; Store in a new collection. List<String> list3 = two.stream() .filter(str -> str.charAt(0) == 'Zhang') .collect(Collectors.toList()); System.out.println("list3 = " + list3); //After the second team is screened, do not use the first two people; Store in a new collection. List<String> list4 = two.stream() .skip(2) .collect(Collectors.toList()); System.out.println("list4 = " + list4); //Merge the two teams into one team; Store in a new collection. Stream<String> stream1 = one.stream(); Stream<String> stream2 = two.stream(); //Stream<String> concatStream = Stream.concat(stream1, stream2); /*List<String> list5 = concatStream.collect(Collectors.toList()); System.out.println("list5 = " + list5);*/ //Create a Person object based on the name; Store in a new collection. /*concatStream.map(new Function<String, Person>() { @Override public Person apply(String s) { return new Person(s); } });*/ //The terminated stream cannot be operated again Stream.concat(stream1, stream2).map(Person::new).forEach(System.out::println); } }