Two ways to get Stream stream
-
Two ways to get Stream stream
-
Demo01GetStream
package cn.xiaoge.day21.demo02; /* java.util.stream.Stream<T>Java 1.8 is the most commonly used new stream interface Getting a flow is very simple. There are several common ways: All Collection collections can obtain streams by stream default method; default Stream<E> stream() Returns a sequence Stream and this collection as its source. Stream The static method of interface can get the stream corresponding to the array static <T> Stream<T> of(T... values) Returns the ordered flow whose elements are the specified values. Parameter is a variable parameter, then we can pass an array */ import java.util.*; import java.util.stream.Stream; public class Demo01GetStream { public static void main(String[] args) { // Convert set to Stream List<String> list = new ArrayList<>(); Stream<String> stream1 = list.stream(); Set<String> set = new HashSet<>(); Stream<String> stream2 = set.stream(); Map<String, String> map = new HashMap<>(); // Get key, save to Set set Set<String> keySet = map.keySet(); Stream<String> stream3 = keySet.stream(); // Get the value and save it in a Collection Collection<String> values = map.values(); Stream<String> stream4 = values.stream(); Set<Map.Entry<String, String>> entries = map.entrySet(); Stream<Map.Entry<String, String>> stream5 = entries.stream(); // Convert array to Stream stream Stream<Integer> stream6 = Stream.of(1, 2, 3, 4, 5); // Variable parameters can pass arrays Integer[] arr = {1, 2, 3, 4, 5}; Stream<Integer> stream7 = Stream.of(arr); String[] arr1 = {"1", "2", "3", "4", "5"}; Stream<String> stream8 = Stream.of(arr1); } }
-
-
Common methods in Stream stream
-
Demo02Stream_forEach
package cn.xiaoge.day21.demo02; /* Stream Common methods in flow void forEach(Consumer<? super T> action); This method accepts a Consumer interface function, which will process each flow element Consumer The interface is a consumption type functional interface, which can transfer Lambda expressions and consumption data Simple account: forEach Method to traverse data in the stream Is a termination method. After traversal, other methods in the Stream cannot be called */ import java.util.stream.Stream; public class Demo02Stream_forEach { public static void main(String[] args) { // Get a Stream Stream<String> stream = Stream.of("Zhang San", "Li Si", "Wang Wu", "Zhao Liu", "Pseudo-ginseng"); // Use the method forEach in Stream stream to continue traversing the data in Stream stream /* stream.forEach((String name) -> { System.out.println(name); }); */ // To optimize Lambda expressions, you must annotate the above to use the following one, because forEach is the end method stream.forEach(name-> System.out.println(name)); } } // Operation result //Zhang San //Li Si //Wang Wu //Zhao Liu //Pseudo-ginseng
-
-
Common methods in Stream stream
-
Demo03Stream_filter
package cn.xiaoge.day21.demo02; /* Stream Common methods in flow void forEach(Consumer<? super T> action); filter The parameter Predicate of the method is a functional interface, so you can pass Lambda expressions to filter the data Predicate Abstract method in: boolean test(T t); */ import java.util.stream.Stream; public class Demo03Stream_filter { public static void main(String[] args) { // Create a Stream stream Stream<String> stream = Stream.of("Zhang Wuji", "Zhou Zhi Luo", "Zhao Min", "Zhang Qiang", "Zhang Sanfeng"); // Filter the elements in the Stream as long as the person of the line sheet /*Stream<String> stream2 = stream.filter((String name)->{ return name.startsWith("Zhang ""; });*/ // Optimize Lambda expressions Stream<String> stream2 = stream.filter(name->name.startsWith("Zhang")); // Traversal stream2 flow stream2.forEach(name-> System.out.println(name)); /* Stream Flow belongs to pipeline flow and can only be consumed (used) once After the first Stream calls the method, the data will flow to the next Stream When the first Stream has been used, it will be closed So the first Stream can no longer call methods */ // Error reporting of traversal stream // stream.forEach(name-> System.out.println(name)); // java.lang.IllegalStateException } } // Operation result //Zhang Wuji //Zhang Qiang //Zhang Sanfeng
-
-
Common methods in Stream stream
-
Demo04Stream_map
package cn.xiaoge.day21.demo02; /* If you need to map elements in a flow to another flow, you can use the map method. <R> Stream<R> map(Function<? super T, ? extends R> mapper); This interface requires a Function type interface parameter, which can convert the T type data in the current flow to another R type flow. Function Abstract method in: R apply(T t); */ import java.util.function.Function; import java.util.stream.Stream; public class Demo04Stream_map { public static void main(String[] args) { // Get a Stream of Stream type Stream<String> stream = Stream.of("1", "2", "3", "4", "5"); // Use the map method to convert (map) an Integer of string type to an Integer of Integer type Stream<Integer> stream1 = stream.map(number -> Integer.parseInt(number)); // Traverse stream1 stream stream1.forEach(number-> System.out.println(number.getClass() + " " + number)); } } // Operation result class java.lang.Integer 1 class java.lang.Integer 2 class java.lang.Integer 3 class java.lang.Integer 4 class java.lang.Integer 5
-
-
Common methods in Stream stream
-
Demo05Stream_count
package cn.xiaoge.day21.demo02; /* Stream Common method in Stream count: used to count the number of elements in Stream stream long count(); count Method is an end method, and the return value is an integer of type long So we can't continue to call other methods in the Stream */ import java.util.ArrayList; public class Demo05Stream_count { public static void main(String[] args) { // Get a Stream ArrayList<Integer> list = new ArrayList<>(); // Additive elements list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); long count = list.stream().count(); System.out.println(count); } } // Operation result 5
-
-
Common methods in Stream stream
-
Demo06Stream_limit
package cn.xiaoge.day21.demo02; /* Stream Common methods in stream ﹐ limit: used to intercept elements in stream limit Only the first n can be used Stream<T> limit(long maxSize); The parameter is a long type. If the current length of the set is greater than the parameter, it will be intercepted; otherwise, no operation will be performed limit Method is a delay method. It just intercepts the elements in the Stream and returns a new Stream, so you can continue to call other methods in the Stream stream */ import java.util.stream.Stream; public class Demo06Stream_limit { public static void main(String[] args) { // Get a Stream String[] arr = {"Beaming with Joy", "Beauty sheep", "Languid", "Grey Wolf", "Red Wolf"}; Stream<String> stream = Stream.of(arr); // limit(4) intercepts the first four elements, returns the new flow, and then traverses the new flow for each stream.limit(4).forEach(name-> System.out.println(name)); } } // Operation result //Beaming with Joy //Beauty sheep //Languid //Grey Wolf
-
-
Commonly used method in Stream stream
-
Demo07Stream_skip
package cn.xiaoge.day21.demo02; /* Stream Commonly used method in stream ﹣ skip; used to skip elements If you want to skip the first few elements, you can use the skip method to get a new stream after interception; Stream<T> skip(long n); If the current length of the stream is greater than N, the first n will be skipped; otherwise, an empty stream with a length of 0 will be obtained */ import java.util.stream.Stream; public class Demo07Stream_skip { public static void main(String[] args) { Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5); // Skip 3 elements with skip // Stream < integer > stream2 = stream.skip (3); / / here 3 are 3 elements, not Subscripts // stream2.forEach(number-> System.out.println(number)); // 4 5 stream.skip(6).forEach(number-> System.out.println(number)); // Air flow } } // Operation result //It's empty because it's all5Element
-
-
Common methods in Stream stream
-
Demo08Stream_concat
package cn.xiaoge.day21.demo02; /* Stream Common methods in streams ﹐ concat: used to group streams together If you want to merge two streams into one, you can use the static method concat of the Stream interface static <T> Stream<T> concat(Stream<? extends T>a, Stream<? extends T> b) */ import java.util.stream.Stream; public class Demo08Stream_concat { public static void main(String[] args) { // Create a stream Stream<String> stream = Stream.of("Zhang Wuji", "Zhou Zhi Luo", "Zhao Min", "Zhang Qiang", "Zhang Sanfeng"); // Create a stream2 stream Stream<String> stream2 = Stream.of("Zhang San", "Li Si", "Wang Wu", "Zhao Liu", "Pseudo-ginseng"); // concat combines two streams into one Stream<String> stream3 = Stream.concat(stream, stream2); stream3.forEach(name-> System.out.println(name)); } } // Operation result //Zhang Wuji //Zhou Zhi Luo //Zhao Min //Zhang Qiang //Zhang Sanfeng //Zhang San //Li Si //Wang Wu //Zhao Liu //Pseudo-ginseng
-
-
The processing of collection elements -- the traditional way & stream way
-
Person
package cn.xiaoge.day21.demo03; 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 + '\'' + '}'; } }
-
Demo01StreamTest
package cn.xiaoge.day21.demo03; /* Exercise: set element handling (traditional way) Now there are two ArrayList collection storage teams with multiple member names, which require the following steps in turn using the traditional for loop (or enhanced for loop): 1. The first team only needs the names of members whose names are three words; they are stored in a new set. 2. After the first team is filtered, only the first three people are needed; they are stored in a new collection. 3. The second team only needs the name of the member whose surname is Zhang; it is stored in a new collection. 4. Do not use the first two people after the second team screening; store in a new collection. 5. Merge two teams into one team; store in a new collection. 6. Creates a Person object based on a name; stores it in a new collection. 7. Print the Person object information for the entire team. */ import java.util.ArrayList; public class Demo01StreamTest { public static void main(String[] args) { // Now there are two ArrayList collection storage teams with multiple member names, which require the following steps in turn using the traditional for loop (or enhanced for loop): ArrayList<String> one = new ArrayList<>(); one.add("Di Ali Gerba"); one.add("Song Yuan Qiao"); one.add("Su Xing He"); one.add("Shi Tian Tian"); one.add("Stone jade"); one.add("Lao Tzu"); one.add("Chuang-tzu"); one.add("Master Hongqi"); ArrayList<String> two = new ArrayList<>(); two.add("Guli Nazha"); two.add("Zhang Wuji"); two.add("Zhao Liying"); two.add("Zhang Sanfeng"); two.add("Nicholas Zhao Si"); two.add("Zhang Tian AI"); two.add("Zhang Er dog"); // 1. The first team only needs the member name with 3 words; it is stored in a new set. ArrayList<String> three = new ArrayList<>(); for (String s : one) { if(s.length() == 3){ three.add(s); } } for (String s : two) { if(s.length() == 3){ three.add(s); } } // 2. After the first team is filtered, only the first three people are needed; they are stored in a new collection. ArrayList<String> four = new ArrayList<>(); for (int i = 0; i < three.size(); i++) { if (i < 3) { four.add(three.get(i)); } } // 3. The second team only needs the name of the member whose surname is Zhang; it is stored in a new set. ArrayList<String> five = new ArrayList<>(); for (String s : three) { if(s.startsWith("Zhang")){ five.add(s); } } // 4. Do not use the first two people after the second team screening; store in a new collection. ArrayList<String> six = new ArrayList<>(); for (int i = 0; i < five.size(); i++) { if(i < 2){ continue; } six.add(five.get(i)); } // 5. Merge two teams into one team; store in a new set. ArrayList<String> seven = new ArrayList<>(); for (String s : four) { seven.add(s); } for (String s : six) { seven.add(s); } // 6. Create a Person object based on the name; store in a new collection. ArrayList<Person> eight = new ArrayList<>(); for (String s : seven) { Person p = new Person(s); eight.add(p); } // 7. Print the Person object information of the whole team. for (Person person : eight) { System.out.println(person); } } } // Operation result Person{name='Song Yuan Qiao'} Person{name='Su Xing He'} Person{name='Shi Tian Tian'} Person{name='Zhang Tian AI'} Person{name='Zhang Er dog'}
-
Demo02StreamTest
package cn.xiaoge.day21.demo03; /* Exercise: set element handling (traditional way) Now there are two ArrayList collection storage teams with multiple member names, which require the following steps in turn using the traditional for loop (or enhanced for loop): 1. The first team only needs the names of members whose names are three words; they are stored in a new set. 2. After the first team is filtered, only the first three people are needed; they are stored in a new collection. 3. The second team only needs the name of the member whose surname is Zhang; it is stored in a new collection. 4. Do not use the first two people after the second team screening; store in a new collection. 5. Merge two teams into one team; store in a new collection. 6. Creates a Person object based on a name; stores it in a new collection. 7. Print the Person object information for the entire team. */ import java.util.ArrayList; import java.util.stream.Stream; public class Demo02StreamTest { public static void main(String[] args) { // Now there are two ArrayList collection storage teams with multiple member names, which require the following steps in turn using the traditional for loop (or enhanced for loop): ArrayList<String> one = new ArrayList<>(); one.add("Di Ali Gerba"); one.add("Song Yuan Qiao"); one.add("Su Xing He"); one.add("Shi Tian Tian"); one.add("Stone jade"); one.add("Lao Tzu"); one.add("Chuang-tzu"); one.add("Master Hongqi"); ArrayList<String> two = new ArrayList<>(); two.add("Guli Nazha"); two.add("Zhang Wuji"); two.add("Zhao Liying"); two.add("Zhang Sanfeng"); two.add("Nicholas Zhao Si"); two.add("Zhang Tian AI"); two.add("Zhang Er dog"); // 1. The first team only needs the member name with 3 words; it is stored in a new set. // 2. After the first team is filtered, only the first three people are needed; they are stored in a new collection. Stream<String> stream1 = Stream.concat(one.stream().filter(name -> name.length() == 3), two.stream().filter(name -> name.length() == 3)).limit(3); // 3. The second team only needs the name of the member whose surname is Zhang; it is stored in a new set. // 4. Do not use the first two people after the second team screening; store in a new collection. Stream<String> stream2 = Stream.concat(one.stream().filter(name->name.startsWith("Zhang")), two.stream().filter(name->name.startsWith("Zhang"))).skip(2); // 5. Merge two teams into one team; store in a new set. Stream<String> stream3 = Stream.concat(stream1, stream2); // 6. Create a Person object based on the name; store in a new collection. // 7. Print the Person object information of the whole team. stream3.forEach(name-> System.out.println(new Person(name))); } } // Operation result Person{name='Song Yuan Qiao'} Person{name='Su Xing He'} Person{name='Shi Tian Tian'} Person{name='Zhang Tian AI'} Person{name='Zhang Er dog'}
-
278 original articles published, 89 praised, 20000 visitors+