天天看點

java1.8中stream的使用

一起盡在代碼中,大家可以參考代碼了解stream的各種操作

先把一些正常的中間條件以及結束條件羅列一下

Intermediate(中間操作):

map (mapToInt, flatMap 等)、 filter、 distinct、 sorted、 peek、 limit、 skip、 parallel、 sequential、 unordered

Terminal(結束操作):

forEach、 forEachOrdered、 toArray、 reduce、 collect、 min、 max、 count、 anyMatch、 allMatch、 noneMatch、 findFirst、 findAny、 iterator

Short-circuiting(快速比對):

anyMatch、 allMatch、 noneMatch、 findFirst、 findAny、 limit

package org.gfu.base.stream;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * jdk1.8 stream application
 *
 * https://stackify.com/streams-guide-java-8/
 * @author [email protected] | [email protected] | 有問題可以郵箱或者github聯系我
 * @date 2019/9/22 12:48
 */
public class StreamMain {

    private static final int HASH_MAP_INIT_SIZE = 16;

    public static void main(String[] args) {
        Map map = new HashMap(HASH_MAP_INIT_SIZE);
        listStreamHandler03(StreamUnit.getStreamUnit());
        mapStreamHandler(map);

    }

    /**
     * stream basic operation
     *
     * @param list
     */
    private static void listStreamHandler(List<StreamUnit> list) {
        List<String> result = list
                .stream()
                .filter(i -> i.name.equals("zhangsan"))
                .map(i -> {
                    StreamUnit su = new StreamUnit();
                    su.name = i.name;
                    su.des = i.des;
                    return su;
                })
                .flatMap(i -> {
                    String[] ss = i.name.split("");
                    return Stream.of(ss);
                })
                .skip(2)
                .limit(5)
//                .peek()
//                .collect(Collectors.toSet())
                .collect(Collectors.toList());

        System.out.println(result);
    }

    /**
     * flatMap and reduce
     *
     * @param list
     */
    private static void listStreamHandler01(List<StreamUnit> list) {
        Optional<String> result = list
                .stream()
                .flatMap(i -> Stream.of(i.name.split("")))
                .reduce(String::concat);
        System.out.println(result);
    }

    /**
     * reduce params:{startValue, result , item}
     *
     * @param list
     */
    private static void listStreamHandler02(List<StreamUnit> list) {
        String result = list
                .stream()
                .flatMap(i -> Stream.of(i.name.split("")))
                .skip(4)
                .reduce("fadsf", String::concat);
        System.out.println(result);
    }

    /**
     * reduce params:{object, result , item}
     *
     * @param list
     */
    private static void listStreamHandler03(List<StreamUnit> list) {
        List<StreamUnit> initList = new ArrayList<>(1);
        List<StreamUnit> result = list.stream()
                .reduce(initList, (acc, i) -> {
                    System.out.println(acc);
                    StreamUnit su = new StreamUnit();
                    su.name = i.toString();
                    acc.add(su);
                    return acc;
                }, (u, i) -> u);
        
        System.out.println(result);
    }

    private static void mapStreamHandler(Map map) {
        map.entrySet().stream();
    }
}
``           

複制