java8 new features --stream notes

stream objects

  1. Stream

  2. IntStream

  3. LongStream

  4. DoubleStream

create

Commonly used in three ways:

  • Use the object list:
    • list.stream () - create a serial stream for the collection.
    • list.parallelStream () - Create a set of parallel streams.
  • Arrays: Arrays.stream (T [] array) - array to create a stream (create IntStream, LongStream, DoubleStrem).
  • Stream: Stream.of (T ... values) - creates a stream for a group of the same type of data.

demo:

 /**
     * 集合接口有两个方法来生成流:
     * 按照流的类型可分为串行流和并行流
     * stream() − 为集合创建串行流。
     * parallelStream() − 为集合创建并行流。
     */
    private static Stream<String> createStreamFromCollection() {
        List<String> list = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
        return list.stream();
    }
    /**
     *  使用Stream.of()创建流
     * @return
     */
    private static Stream<String> createStreamFromValues() {
        return Stream.of("hello", "alex", "wangwenjun", "world", "stream");
    }
    /**
     * 使用Arrays.stream()创建流
     * 可以生成IntStream,LongStream,DoubleStream
     * @return
     */
    private static Stream<String> createStreamFromArrays() {
        String[] strings = {"hello", "alex", "wangwenjun", "world", "stream"};
        return Arrays.stream(strings);
    }

operating

filter: filter, to filter out non-qualifying data.

map: convert the stream type Returns a stream that contains the results of a given function is applied to the flow element.

For example, a Stream 类型的流组装后成为Stream 的流

limit: paging.

collect: The stream collected in an array.

  List<Dish> menu = Arrays.asList(
                new Dish("pork", false, 800, "肉类"),
                new Dish("beef", false, 700, "肉类"),
                new Dish("chicken", false, 400, "肉类"),
                new Dish("french fries", true, 530, "鱼肉类"),
                new Dish("rice", true, 350, "鱼肉类"),
                new Dish("season fruit", true, 120, "鱼肉类"),
                new Dish("pizza", true, 550, "鱼肉类"),
                new Dish("prawns", false, 300, "其他"),
                new Dish("salmon", false, 450, "其他"));


        //创建菜单流
        List<Map<String,Integer>> result = menu.stream().filter(d -> {
            // 过滤出卡路里大于300的值
            System.out.println("filtering->" + d.getName());
            return d.getCalories() > 300;
        })
                .map(d -> {
                    Map<String, Integer> map = new HashMap<>();
                    map.put(d.getName(),d.getCalories());
                    return map;
                })
                .limit(3)
                .collect(toList());

        System.out.println(result);

Other operations

  List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 6, 7, 7, 1);
        List<Integer> result = list.stream().filter(i -> i % 2 == 0).collect(toList());
        System.out.println("i -> i % 2 的值  :  "+result);
        result = list.stream().distinct().collect(toList());
        System.out.println("去重   :   "+result);
        result = list.stream().skip(2).collect(toList());//跳过前两个数
        System.out.println("跳过   : "+result);
        result = list.stream().limit(4).collect(toList());//取前4个值
        System.out.println("分页   : "+result);
        System.out.println("循环方式一  : ");
        list.forEach(i -> System.out.println(i));
        System.out.println("循环方式二  : ");
        list.forEach((Integer i) -> System.out.println(i));
        System.out.println("循环方式三  : ");
        list.forEach(System.out::println);

        for (int i : list) {
            System.out.println(i);
        }

**** If the code word is not easy to have to help you Give me a concern ****

**** love life love technology QQ group: 894 109 590 ****

Guess you like

Origin www.cnblogs.com/freeoldman/p/11995868.html