Some insights Java8 stream flow

Fractions intermediate operation without intermediate termination operation to terminate operation of the operation is not performed
Collectors.joining ( "/")
flatMap () draw all streams of a stream labeled
Stream <List> stream = Stream.of ( Arrays.asList ( . 1), Arrays.asList (2,3), Arrays.asList (4,5,6));
. stream.flatMap (list-> list.stream ()) Map (item-> Item * Item) .forEach ( system.out :: println);
the set of data of interest is stored with the data itself,
the stream about compute the data
stream can not be reused

Intermediate operations return stream object
terminating operation does not return value, the return value may not, returns a single value may also be other types of

If there is current short circuit operation will not find the data in line with other data to determine
flow is equivalent to a container all operations into them, and then the data one by one into the operation, but only after all the first data operation is completed, additional data in order to to operate

@Test
    public void test1(){
        List<String> list = Arrays.asList("hello","world","hello world");
        list.stream().mapToInt(item->{
            int length =item.length();
            System.out.println(item);
            return length;
        }).filter(item->item==5).findFirst().ifPresent(System.out::println);

        List<String> list1 = Arrays.asList("hello world","hello welcome","welcome world");
        List<String> list2 = list1.stream().map(item->item.split(" ")).flatMap(Arrays::stream).distinct().collect(Collectors.toList());
        list2.forEach(System.out::println);
    }

Print out data

hello//找到符合条件的hello 就不会去找后面的数据
5
---------------------
hello
world
welcome
Published 80 original articles · won praise 140 · views 640 000 +

Guess you like

Origin blog.csdn.net/linjpg/article/details/104228402