Introduction to Common APIs of Steam Streaming in Java

Stream common API

Common APIs for Stream (intermediate operation methods)

name illustrate
filter(Predicate<? super T> predicate) Used to filter the data in the stream.
limit(long maxSize) Get the first few elements
skip(long n) skip the first few elements
distinct() Remove duplicate elements from a stream. Dependencies (hashCode and equals methods)
concat(Stream a, Stream b) Merge two streams a and b into one stream

filter method: used to filter the data in the stream

public static void main(String[] args) {
    
    
    List<String> list = new ArrayList<>();
    Collections.addAll(list, "张无忌", "赵敏", "周芷若", "张三丰", "张三");

    // 对Stream流中的数据进行过滤, 过滤后会返回一个新的Stream流
    Stream<String> st = list.stream().filter(s -> s.startsWith("张"));
    // 对过滤后的Stream流进行遍历输出
    st.forEach(s -> System.out.println(s));
}

The intermediate method is also called a non-terminal method. After the call is completed, a new Stream is returned and can be used continuously, which supports chain programming.

public static void main(String[] args) {
    
    
    List<String> list = new ArrayList<>();
    Collections.addAll(list, "张无忌", "赵敏", "周芷若", "张三丰", "张三");

    // 对Stream流中的数据进行过滤
    list.stream().filter(s -> s.startsWith("张")).forEach(s -> System.out.println(s)); // 张无忌 张三丰 张三
}

limit: Get the first few elements

public static void main(String[] args) {
    
    
    List<String> list = new ArrayList<>();
    Collections.addAll(list, "张无忌", "赵敏", "周芷若", "张三丰", "张三");
  
    // 获取前两个元素
    list.stream().limit(2).forEach(s -> System.out.println(s)); //张无忌 赵敏
}

skip: skip the first few elements

public static void main(String[] args) {
    
    
    List<String> list = new ArrayList<>();
    Collections.addAll(list, "张无忌", "赵敏", "周芷若", "张三丰", "张三");

    // 跳过前三个元素
    list.stream().skip(3).forEach(s -> System.out.println(s)); // 张三丰 张三
}

distinct: remove duplicate elements

public static void main(String[] args) {
    
    
    List<String> list = new ArrayList<>();
    Collections.addAll(list, "张无忌", "张无忌", "赵敏", "周芷若", "周芷若", "张三丰", "张三");

    list.stream().distinct().forEach(s -> System.out.println(s)); // 张无忌 赵敏 周芷若 张三丰 张三
}

concat: merge a, b into one stream

public static void main(String[] args) {
    
    
    List<String> list1 = new ArrayList<>();
    Collections.addAll(list1, "aaa", "bbb", "aaa");
    List<String> list2 = new ArrayList<>();
    Collections.addAll(list2, "ccc", "ddd", "ccc");

    Stream<String> a = list1.stream();
    Stream<String> b = list2.stream();
    Stream<String> c = Stream.concat(a, b);
    c.forEach(s -> System.out.println(s)); // aaa bbb aaa ccc ddd ccc
}

Extended API: map processing method

public static void main(String[] args) {
    
    
    List<String> list = new ArrayList<>();
    Collections.addAll(list, "张无忌", "张无忌", "赵敏", "周芷若", "周芷若", "张三丰", "张三");

    list.stream().map(s -> "金庸小说-" + s).forEach(s -> System.out.println(s));
  	// 打印结果:
    // 金庸小说-张无忌
    // 金庸小说-张无忌
    // 金庸小说-赵敏
    // 金庸小说-周芷若
    // 金庸小说-周芷若
    // 金庸小说-张三丰
    // 金庸小说-张三
}

Note :

Data in collections and arrays cannot be directly modified in the Stream stream, which means that operating the Stream stream will not affect the original array or collection.

Common terminal operation methods of Stream stream :

name illustrate
forEach(Consumer action) performs a traversal operation on each element of this stream
long count() returns the number of elements in this stream

Note : After the operation method is terminated, the stream cannot be used after the call is completed, because the Stream will not be returned.

Guess you like

Origin blog.csdn.net/m0_71485750/article/details/127493355