Stream流常见方法

package com.ding.stream;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.stream.Stream;

public class MyStream2 {

    public static void main(String[] args) {
        //method1();
        //method2();
        int [] arr = {1, 2, 3, 4, 5};
        Arrays.stream(arr).forEach(s -> System.out.println(s));

        Stream.of(1,2,3,4,5).forEach(s -> System.out.println(s));

    }

    private static void method2() {
        HashMap<String, Integer> hm = new HashMap<>();
        hm.put("zhangsan",23);
        hm.put("lisi",24);
        hm.put("wangwu",25);
        hm.put("zhaoliu",26);
        hm.put("qianqi",27);
        //双列集合不能直接获取Stream流
        hm.keySet().stream().forEach(s -> System.out.println(s));

        hm.entrySet().stream().forEach(stringIntegerEntry -> System.out.println(stringIntegerEntry));
    }

    private static void method1() {
        ArrayList<String> list = new ArrayList<>();
        list.add("aaa");
        list.add("bbb");
        list.add("ccc");
        Stream<String> stream = list.stream();
        stream.forEach( s -> System.out.println(s));

        list.stream().forEach(s -> System.out.println(s));
    }


}


package com.ding.stream;


import java.util.ArrayList;
import java.util.function.Predicate;

public class MyStream3 {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("张三丰");
        list.add("张无忌");
        list.add("张翠山");
        list.add("王二麻子");
        list.add("张良");
        list.add("谢广坤");

        list.stream().filter(
                new Predicate<String>() {
                    @Override
                    public boolean test(String s) {

                        boolean starts = s.startsWith("张");
                        return starts;
                    }
                }
        ).forEach(s -> System.out.println(s));


        list.stream().filter(
                (String s) -> {
                    boolean starts = s.startsWith("张");
                    return starts;
                }
        ).forEach(s -> System.out.println(s));


        list.stream().filter( s -> s.startsWith("张")).forEach( s -> {
            System.out.println(s);
        });
    }



}

上一篇:sql动态拼接<if>标签嵌套<foreach>判空、if失效等问题


下一篇:JavaScript数组的常用方法总结:遍历,复制,反转,排序,添加,删除(前端常见面试题必考必问