java stream() 简单示例

List stringList = new ArrayList<>();
        stringList.add("name1");
        stringList.add("name2");
        stringList.add("name3");
        stringList.add("name4");
        stringList.add("name5");
        stringList.add("name6");

        long count = stringList.stream().count();
        //等于stringList.size()
        System.out.println(count);
        //输出:6


        List filterList1 = stringList.stream().filter(data-> Objects.equals(data, "name1")).collect(Collectors.toList());
        System.out.println(filterList1);
        //输出:["name1"]

        List collect = stringList.stream().limit(3).filter("name1"::equals).collect(Collectors.toList());
        System.out.println(collect);
        //输出:["name1"]


        /*map重组数据*/
        List collect1 = stringList.stream().limit(1).map(data -> {
            return "QA_" + data;
        }).collect(Collectors.toList());
        System.out.println(collect1);
        //输出:["QA_name1"]


        /*跳过前面5个*/
        List collect2 = stringList.stream().skip(5).collect(Collectors.toList());
        System.out.println(collect2);
        //输出:["name6"]

你可能感兴趣的:(java,jvm,servlet)