Stream API

Stream简介编程

  • 为何要使用Stream数据结构

  • 实例数据源并发

  • Filterapp

  • Map函数式编程

  • FlatMap函数

  • Reduce性能

  • Collect测试

  • Optionalui

  • 并发spa

  • 调试


Stream简介

一、Java 8引入了全新的Stream API。这里的Stream和I/O流不一样,它更像具备Iterable的集合类,但行为和集合类又有所不一样。

二、stream是对集合对象功能的加强,它专一于对集合对象进行各类很是便利、高效的聚合操做,或者大批量数据操做。

三、只要给出须要对其包含的元素执行什么操做,好比 “过滤掉长度大于 10 的字符串”、“获取每一个字符串的首字母”等,Stream 会隐式地在内部进行遍历,作出相应的数据转换。

为何要使用Stream

一、函数式编程带来的好处尤其明显。这种代码更多地表达了业务逻辑的意图,而不是它的实现机制。易读的代码也易于维护、更可靠、更不容易出错。

二、高端

实例数据源

public class Data {
    private static List<PersonModel> list = null;

    static {
        PersonModel wu = new PersonModel("wu qi"18"男");
        PersonModel zhang = new PersonModel("zhang san"19"男");
        PersonModel wang = new PersonModel("wang si"20"女");
        PersonModel zhao = new PersonModel("zhao wu"20"男");
        PersonModel chen = new PersonModel("chen liu"21"男");
        list = Arrays.asList(wu, zhang, wang, zhao, chen);
    }

    public static List<PersonModel> getData() {
        return list;
    }
}

Filter

一、遍历数据并检查其中的元素时使用。

二、filter接受一个函数做为参数,该函数用Lambda表达式表示。

    /**      * 过滤全部的男性      */
    public static void fiterSex(){
        List<PersonModel> data = Data.getData();

        //old
        List<PersonModel> temp=new ArrayList<>();
        for (PersonModel person:data) {
            if ("男".equals(person.getSex())){
                temp.add(person);
            }
        }
        System.out.println(temp);
        //new
        List<PersonModel> collect = data
                .stream()
                .filter(person -> "男".equals(person.getSex()))
                .collect(toList());
        System.out.println(collect);
    }

    /**      * 过滤全部的男性 而且小于20岁      */
    public static void fiterSexAndAge(){
        List<PersonModel> data = Data.getData();

        //old
        List<PersonModel> temp=new ArrayList<>();
        for (PersonModel person:data) {
            if ("男".equals(person.getSex())&&person.getAge()<20){
                temp.add(person);
            }
        }

        //new 1
        List<PersonModel> collect = data
                .stream()
                .filter(person -> {
                    if ("男".equals(person.getSex())&&person.getAge()<20){
                        return true;
                    }
                    return false;
                })
                .collect(toList());
        //new 2
        List<PersonModel> collect1 = data
                .stream()
                .filter(person -> ("男".equals(person.getSex())&&person.getAge()<20))
                .collect(toList());

    }

Map

一、map生成的是个一对一映射,for的做用

二、比较经常使用

三、并且很简单

   /**      * 取出全部的用户名字      */
    public static void getUserNameList(){
        List<PersonModel> data = Data.getData();

        //old
        List<String> list=new ArrayList<>();
        for (PersonModel persion:data) {
            list.add(persion.getName());
        }
        System.out.println(list);

        //new 1
        List<String> collect = data.stream().map(person -> person.getName()).collect(toList());
        System.out.println(collect);

        //new 2
        List<String> collect1 = data.stream().map(PersonModel::getName).collect(toList());
        System.out.println(collect1);

        //new 3
        List<String> collect2 = data.stream().map(person -> {
            System.out.println(person.getName());
            return person.getName();
        }).collect(toList());
    }

FlatMap

一、顾名思义,跟map差很少,更深层次的操做

二、但仍是有区别的

三、map和flat返回值不一样

四、Map 每一个输入元素,都按照规则转换成为另一个元素。
还有一些场景,是一对多映射关系的,这时须要 flatMap。

五、Map一对一

六、Flatmap一对多

七、map和flatMap的方法声明是不同的

(1) Stream map(Function mapper);

(2) Stream flatMap(Function> mapper);

(3) map和flatMap的区别:我我的认为,flatMap的能够处理更深层次的数据,入参为多个list,结果能够返回为一个list,而map是一对一的,入参是多个list,结果返回必须是多个list。通俗的说,若是入参都是对象,那么flatMap能够操做对象里面的对象,而map只能操做第一层。

public static void flatMapString() {
        List<PersonModel> data = Data.getData();
        //返回类型不同
        List<String> collect = data.stream()
                .flatMap(person -> Arrays.stream(person.getName().split(" "))).collect(toList());

        List<Stream<String>> collect1 = data.stream()
                .map(person -> Arrays.stream(person.getName().split(" "))).collect(toList());

        //用map实现
        List<String> collect2 = data.stream()
                .map(person -> person.getName().split(" "))
                .flatMap(Arrays::stream).collect(toList());
        //另外一种方式
        List<String> collect3 = data.stream()
                .map(person -> person.getName().split(" "))
                .flatMap(str -> Arrays.asList(str).stream()).collect(toList());
    }

Reduce

一、感受相似递归

二、数字(字符串)累加

三、我的没咋用过

 public static void reduceTest(){
        //累加,初始化值是 10
        Integer reduce = Stream.of(1234)
                .reduce(10(count, item) ->{
            System.out.println("count:"+count);
            System.out.println("item:"+item);
            return count + item;
        } );
        System.out.println(reduce);

        Integer reduce1 = Stream.of(1234)
                .reduce(0(x, y) -> x + y);
        System.out.println(reduce1);

        String reduce2 = Stream.of("1""2""3")
                .reduce("0"(x, y) -> (x + "," + y));
        System.out.println(reduce2);
    }

Collect

一、collect在流中生成列表,map,等经常使用的数据结构

二、toList()

三、toSet()

四、toMap()

五、自定义

 /**      * toList      */
    public static void toListTest(){
        List<PersonModel> data = Data.getData();
        List<String> collect = data.stream()
                .map(PersonModel::getName)
                .collect(Collectors.toList());
    }

    /**      * toSet      */
    public static void toSetTest(){
        List<PersonModel> data = Data.getData();
        Set<String> collect = data.stream()
                .map(PersonModel::getName)
                .collect(Collectors.toSet());
    }

    /**      * toMap      */
    public static void toMapTest(){
        List<PersonModel> data = Data.getData();
        Map<String, Integer> collect = data.stream()
                .collect(
                        Collectors.toMap(PersonModel::getName, PersonModel::getAge)
                );

        data.stream()
                .collect(Collectors.toMap(per->per.getName(), value->{
            return value+"1";
        }));
    }

    /**      * 指定类型      */
    public static void toTreeSetTest(){
        List<PersonModel> data = Data.getData();
        TreeSet<PersonModel> collect = data.stream()
                .collect(Collectors.toCollection(TreeSet::new));
        System.out.println(collect);
    }

    /**      * 分组      */
    public static void toGroupTest(){
        List<PersonModel> data = Data.getData();
        Map<Boolean, List<PersonModel>> collect = data.stream()
                .collect(Collectors.groupingBy(per -> "男".equals(per.getSex())));
        System.out.println(collect);
    }

    /**      * 分隔      */
    public static void toJoiningTest(){
        List<PersonModel> data = Data.getData();
        String collect = data.stream()
                .map(personModel -> personModel.getName())
                .collect(Collectors.joining(",""{""}"));
        System.out.println(collect);
    }

    /**      * 自定义      */
    public static void reduce(){
        List<String> collect = Stream.of("1""2""3").collect(
                Collectors.reducing(new ArrayList<String>(), x -> Arrays.asList(x), (y, z) -> {
                    y.addAll(z);
                    return y;
                }));
        System.out.println(collect);
    }

Optional

一、Optional 是为核心类库新设计的一个数据类型,用来替换 null 值。

二、人们对原有的 null 值有不少抱怨,甚至连发明这一律念的Tony Hoare也是如此,他曾说这是本身的一个“价值连城的错误”

三、用处很广,不光在lambda中,哪都能用

四、Optional.of(T),T为非空,不然初始化报错

五、Optional.ofNullable(T),T为任意,能够为空

六、isPresent(),至关于 !=null

七、ifPresent(T), T能够是一段lambda表达式 ,或者其余代码,非空则执行

public static void main(String[] args) {


        PersonModel personModel=new PersonModel();

        //对象为空则打出 -
        Optional<Object> o = Optional.of(personModel);
        System.out.println(o.isPresent()?o.get():"-");

        //名称为空则打出 -
        Optional<String> name = Optional.ofNullable(personModel.getName());
        System.out.println(name.isPresent()?name.get():"-");

        //若是不为空,则打出xxx
        Optional.ofNullable("test").ifPresent(na->{
            System.out.println(na+"ifPresent");
        });

        //若是空,则返回指定字符串
        System.out.println(Optional.ofNullable(null).orElse("-"));
        System.out.println(Optional.ofNullable("1").orElse("-"));

        //若是空,则返回 指定方法,或者代码
        System.out.println(Optional.ofNullable(null).orElseGet(()->{
            return "hahah";
        }));
        System.out.println(Optional.ofNullable("1").orElseGet(()->{
            return "hahah";
        }));

        //若是空,则能够抛出异常
        System.out.println(Optional.ofNullable("1").orElseThrow(()->{
            throw new RuntimeException("ss");
        }));


//        Objects.requireNonNull(null,"is null");


        //利用 Optional 进行多级判断
        EarthModel earthModel1 = new EarthModel();
        //old
        if (earthModel1!=null){
            if (earthModel1.getTea()!=null){
                //...
            }
        }
        //new
        Optional.ofNullable(earthModel1)
                .map(EarthModel::getTea)
                .map(TeaModel::getType)
                .isPresent();


//        Optional<EarthModel> earthModel = Optional.ofNullable(new EarthModel());
//        Optional<List<PersonModel>> personModels = earthModel.map(EarthModel::getPersonModels);
//        Optional<Stream<String>> stringStream = personModels.map(per -> per.stream().map(PersonModel::getName));


        //判断对象中的list
        Optional.ofNullable(new EarthModel())
                .map(EarthModel::getPersonModels)
                .map(pers->pers
                        .stream()
                        .map(PersonModel::getName)
                        .collect(toList()))
                .ifPresent(per-> System.out.println(per));


        List<PersonModel> models=Data.getData();
        Optional.ofNullable(models)
                .map(per -> per
                        .stream()
                        .map(PersonModel::getName)
                        .collect(toList()))
                .ifPresent(per-> System.out.println(per));

    }

并发

一、stream替换成parallelStream或 parallel

二、输入流的大小并非决定并行化是否会带来速度提高的惟一因素,性能还会受到编写代码的方式和核的数量的影响

三、影响性能的五要素是:数据大小、源数据结构、值是否装箱、可用的CPU核数量,以及处理每一个元素所花的时间

 //根据数字的大小,有不一样的结果
    private static int size=10000000;
    public static void main(String[] args) {
        System.out.println("-----------List-----------");
        testList();
        System.out.println("-----------Set-----------");
        testSet();
    }

    /**      * 测试list      */
    public static void testList(){
        List<Integer> list = new ArrayList<>(size);
        for (Integer i = 0; i < size; i++) {
            list.add(new Integer(i));
        }

        List<Integer> temp1 = new ArrayList<>(size);
        //老的
        long start=System.currentTimeMillis();
        for (Integer i: list) {
            temp1.add(i);
        }
        System.out.println(+System.currentTimeMillis()-start);

        //同步
        long start1=System.currentTimeMillis();
        list.stream().collect(Collectors.toList());
        System.out.println(System.currentTimeMillis()-start1);

        //并发
        long start2=System.currentTimeMillis();
        list.parallelStream().collect(Collectors.toList());
        System.out.println(System.currentTimeMillis()-start2);
    }

    /**      * 测试set      */
    public static void testSet(){
        List<Integer> list = new ArrayList<>(size);
        for (Integer i = 0; i < size; i++) {
            list.add(new Integer(i));
        }

        Set<Integer> temp1 = new HashSet<>(size);
        //老的
        long start=System.currentTimeMillis();
        for (Integer i: list) {
            temp1.add(i);
        }
        System.out.println(+System.currentTimeMillis()-start);

        //同步
        long start1=System.currentTimeMillis();
        list.stream().collect(Collectors.toSet());
        System.out.println(System.currentTimeMillis()-start1);

        //并发
        long start2=System.currentTimeMillis();
        list.parallelStream().collect(Collectors.toSet());
        System.out.println(System.currentTimeMillis()-start2);
    }

调试

一、list.map.fiter.map.xx 为链式调用,最终调用collect(xx)返回结果

二、分惰性求值和及早求值

三、判断一个操做是惰性求值仍是及早求值很简单:只需看它的返回值。若是返回值是 Stream,那么是惰性求值;若是返回值是另外一个值或为空,那么就是及早求值。使用这些操做的理想方式就是造成一个惰性求值的链,最后用一个及早求值的操做返回想要的结果。

四、经过peek能够查看每一个值,同时能继续操做流

相关文章
相关标签/搜索