tags: java Development language rear end
package com.stream;
/**
* @author wxl
*/
/*
Demand: Create and traverse the collection according to the requirements below
Create a collection, store multiple string elements
"Zhang Sanfeng", "Zhang Wuji", "Zhang Cuishan", "Wang Ermazi", "Zhang Liang", "Xie Guangkun"
Store all the elements in the set "Zhang" to a new collection
Store the elements of the length of the "Zhang" set at the beginning of the "Zhang" to a new collection
The collection obtained in the previous step
*/
import java.util.Collections;
import java.util.stream.Collectors;
import java.util.List;
import java.util.ArrayList;
public class MyStream1 {
public static void main(String []args) {
// collection batch add elements
ArrayList<String> list = new ArrayList();
Collections.addAll(list, "Zhang Sanfeng","Zhang Wuji","Zhang Cuishan","Wang Ermazi","Zhang Liang","Xie Guangkun");
// Traversing the list and adding the elements to the beginning to List1.
ArrayList<String> list1 = new ArrayList<>();
for (String s : list) {
if(s.startsWith("open")) {
list1.add(s);
}
}
// Traversing the list1 collection, adding elements with a length of 3 to List2.
ArrayList<String> list2 = new ArrayList<>();
for (String s : list1) {
if (s.length() == 3) {
list2.add(s);
}
}
for (String s : list2) {
System.out.println(s);
}
// Apocastle: It is very convenient to handle with flowing
List<String> list3 = list.stream().filter(s->s.startsWith("open")).collect(Collectors.toList());
System.out.println(list3);
List<String> list4 = list.stream().filter(s->s.startsWith("open")).filter(s->s.length()==3).collect(Collectors.toList());
System.out.println(list4);
}
}
result:
Task :MyStream1.main()
Zhang Sanfeng
Zhang Wuji
Zhang Cuishan
[Zhang Sanfeng, Zhang Wuji, Zhang Cuishan, Zhang Liang]
[Zhang Sanfeng, Zhang Wuji, Zhang Cuishan]
It seems that the operation is convenient and concise. In fact, the collection uses iterative and accumulators to achieve it, and the stream is declared to handle the data set (expressed by querying statements instead of temporarily writing one implementation). In addition,, in addition,, in addition,, in addition, Streaming can be processed in parallel without writing any multi -threaded code! If you have a impression first, you can use it.

learning target Chapter 1 First Java 1.1 Java language overview 1.1.1 Introduction to Java The Java language is a Senior programming language in the United States Sun, in 1995. The so-called programmi...
1. Do not use the compiler, press Window + R directly to use the compiler. 2. The name of the outermost layer in Java needs to be consistent with the file name. 3. The keyword is completely lowercase,...
Java novice entry 01 This article gives the newly entry-ended programmer, don't look at it, for the work, or learn the computer's programming knowledge, whether it is the C language, or Java, or C #, ...
Java entry 01 About common shortcuts and some dos commands Common shortcut Ctrl + C: Replication Ctrl + V: Paste Ctrl + A: All choice Ctrl + x: Cut Ctrl + z: revoked Ctrl + S: Save Ctrl + L: Lock scre...
getting Started...
Welcome to pay attention to the public account [Python Development Reality] to get more content! Tool-Pandas The Pandas library provides high -performance, easy -to -use data structure and data analys...
Lambda expression Lambada expression conditions: Functional interface can only have an abstract method, but there are two special cases, except for the method of default methods and Object classes. La...
Java IO package architecture diagram: Streaming part- the main part of IO; 2. Non-flowing part- mainly contains some auxiliary stream part of the class, such as: File class, Ra...
The relationship between various input streams and output streams of java is a bit complicated. It takes a lot of time for beginners like me to sort out them, so I sorted out my understanding, hoping ...
First, Java version JavaSe: Java Platform Standard Edition Javaee: Java Platform Enterprise Edition Javame: Java platform micro version Second, Java Features Third, Java history Java history Fourth, r...