New features of java8 -- stream notes

Keywords: Programming

stream object

  1. Stream<T>

  2. IntStream

  3. LongStream

  4. DoubleStream

Establish

There are three common ways:

  • Use list object:

    • list.stream() − creates a serial stream for the collection.
    • list.parallelStream() - creates a parallel stream for the collection.
  • Arrays: arrays.stream (t [] array) - - creates a stream for an array (IntStream, LongStream, doublestream can be created).

  • Stream: stream.of (t... Values) − creates a stream for a set of data of the same type.

demo:

 /**
     * The collection interface has two methods for generating streams:
     * According to the type of stream, it can be divided into serial stream and parallel stream
     * stream() − Create a serial stream for the collection.
     * parallelStream() − Create a parallel flow for the collection.
     */
    private static Stream<String> createStreamFromCollection() {
        List<String> list = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
        return list.stream();
    }
    /**
     *  Create a stream using Stream.of()
     * @return
     */
    private static Stream<String> createStreamFromValues() {
        return Stream.of("hello", "alex", "wangwenjun", "world", "stream");
    }
    /**
     * Create a stream using Arrays.stream()
     * IntStream,LongStream,DoubleStream can be generated
     * @return
     */
    private static Stream<String> createStreamFromArrays() {
        String[] strings = {"hello", "alex", "wangwenjun", "world", "stream"};
        return Arrays.stream(strings);
    }

operation

Filter: filter out unqualified data.

map: converts the flow type and returns a flow containing the result of applying the given function to the flow element.

For example, a stream of type stream < Map > is assembled into a stream of type stream < string >

limit: paging.

collect: integrate stream collection into an array.

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


        //Create menu flow
        List<Map<String,Integer>> result = menu.stream().filter(d -> {
            // Filter out values with more than 300 calories
            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 Value  :  "+result);
        result = list.stream().distinct().collect(toList());
        System.out.println("Duplicate removal   :   "+result);
        result = list.stream().skip(2).collect(toList());//Skip the first two numbers
        System.out.println("skip   : "+result);
        result = list.stream().limit(4).collect(toList());//Take the first four values
        System.out.println("paging   : "+result);
        System.out.println("Cycle mode I  : ");
        list.forEach(i -> System.out.println(i));
        System.out.println("Circulation mode II  : ");
        list.forEach((Integer i) -> System.out.println(i));
        System.out.println("Circulation mode 3  : ");
        list.forEach(System.out::println);

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

****Code is not easy. If it helps you, please pay attention****

****Love technology love life QQ group: 894109590****

Posted by ccx004 on Sun, 08 Dec 2019 13:21:53 -0800