Java Stream Quick Entry 01

tags: java  Development language  rear end

1. Introduction (look at the case first):

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]

2. Explanation:

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.

3. Stream flow operation process (three types):

  • Get the stream stream method
    Create an assembly line and put the data on the assembly line to prepare for operation. It is like a collection method that can directly call the stream (). This is the method of obtaining the stream.
  • Intermediate method
    The operation on the assembly line is like Filter filtering, and the elements needed through conditions. This is the intermediate method
  • Ending method
    A Stream stream can only have one ending method, the last operation on the streamline. When using the flow, this process is all operational flow. In the end, you have to turn the flow into a collection or print it or calculate. These methods are the ending method of the stream, such as Collection (Collectors.tolist ()).

4. Summary: Section 02 Introduction to stream stream acquisition method

Intelligent Recommendation

01-Java entry learning

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...

Java entry syntax 01

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

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

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...

Java entry to the ground 01

getting Started...

More Recommendation

pandas tutorial 01 --- Quick entry Series

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...

Java8 new feature lambda expression + stream stream! ! ! Quick entry ~

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 Stream 01 - General

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...

Character stream, byte stream and media stream, filter stream of java entry

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 ...

01-Java foundation - JAVA entry

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...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top