Five of the six new features of Java8 Stream type operation Stream class

Stream is a new member of the javaAPI, which allows you to process data collections in a declarative manner. It can also be transparently processed in parallel, you don't need to write any multi-threaded code.

Encyclopedia of specific methods and types of operations:

 

The most commonly used collection class flow acquisition and operation:

import java.util.*;
import java.util.stream.Stream;

public class StreamOperateTest {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("aaaa");
        list.add("bbbb");
        list.stream().forEach(System.out::println);

        Set<Integer> set = new HashSet<>();
        set.add(111);set.add(222);
        set.stream().forEach(System.out::println);

        Map<String, Integer> map = new HashMap();
        map.put("Allen",19);
        map.put("Bill",18);
        map.keySet().stream().forEach(System.out::println);
        map.values().stream().forEach(System.out::println);

        String[] array = {"array1", "array2", "array3"};
        Stream.of(array).forEach(System.out::println);
    }
}

Results of the:

aaaa
bbbb
222
111
Bill
Allen
18
19
array1
array2
array3

Process finished with exit code 0

 

The filter method is defined to receive a Predicate functional interface, so you only need to implement its abstract method test.

Stream<T> filter(Predicate<? super T> predicate);
boolean test(T t);

The foreach method is defined to receive a Consumer functional interface, so here only the abstract method accetp is implemented.

void forEach(Consumer<? super T> action);
void accept(T t);

import java.util.ArrayList;
import java.util.List;

public class StreamFilterForeachTest {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("aaaa");
        list.add("bbbb");
        list.add("aabb");
        list.add("aabbcc");
        //用非stream流得方式进行遍历
        for (String str:list){
            if (str.contains("ab")&&str.length()==4){
                System.out.println("got "+ str);
            }
        }
        //用stream流进行遍历
        list.stream()
                .filter(str->str.contains("ab"))
                .filter(str->str.length()==4)
//                .forEach(str-> System.out.println("got"+str));
                .forEach(System.out::println);
    }
}

Results of the:

got aabb
aabb

Process finished with exit code 0

The map method is defined to receive a Function functional interface, so here only the abstract method apply is implemented .

<R> Stream<R> map(Function<? super T, ? extends R> mapper)
R apply(T t);

So the role of map is to convert one type of stream into another type of stream

For example, a stream is full of int, use map to convert them to String

Examples are as follows:

eam.of("111","222","333").map(Integer::parseInt).forEach(x-> System.out.println("map - "+ (++x)));

operation result:

map - 112
map - 223
map - 334

The function of limit is to take the first n elements in the stream and generate the next stream, which can be understood as the head in the linux command

example:

Stream.of("111","222","333").limit(2).forEach(x-> System.out.println("limit - "+ x));

Results of the:

limit - 111
limit - 222

The function of skip is to take the last size-n elements of steam and generate the next stream

Stream.of("111","222","333").skip(1).forEach(x->System.out.println("skip - "+ x));

Results of the:

skip - 222
skip - 333

concat merges the elements in the two streams and generates a new stream

Stream.concat(Stream.of("111","222"),Stream.of("333","444")).forEach(x->System.out.println("concat - "+ x));

Results of the:

concat - 111
concat - 222
concat - 333
concat - 444

collect collection operation, convert the stream to collection

List <String> list = Stream.of(111,222,333).map(x->x+"-").collect(Collectors.toList());

The reduce operation is to sum the elements in the stream and take the maximum and minimum values. . .

int a = Stream.of(111,222,333).reduce(0,(x,y)->x+y);
System.out.println(a);

Results of the:

666

parallel parallel processing, stream supports parallel processing to improve execution efficiency

LongStream.range(0,50).forEach(System.out::print);
Long sumVal= LongStream.range(0,100000000L).parallel().reduce(0,Long::sum);
System.out.println("sumVal" + sumVal);

Stream.of(1,2,3,4,5,6,7,8,9).parallel().forEach(System.out::print);
Stream.of(1,2,3,4,5,6,7,8,9).sequential().forEach(System.out::print);

Execution effect:

012345678910111213141516171819202122232425262728293031323334353637383940414243444546474849sumVal4999999950000000
658973412123456789
Process finished with exit code 0

Guess you like

Origin blog.csdn.net/pengweismile/article/details/109700531