Two ways to obtain Java Stream stream

The acquisition stream

java.util.stream. Stream <T> is the most common Java 8 stream interface the newly added. (This is not a function interface.)

Gets a stream of very simple, there are several common ways:

  • All Collection collections are available through the acquisition stream stream default method;

  • Stream interface static method of the array in a stream can be acquired.

 

According to acquire Flow Collection

First, java.util.Collection interfaces adding the default method stream to obtain the stream, so that all classes can be realized acquisition stream.

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

public class Demo03Stream {
    public static void main(String[] args) {
        
        List<String> list = new ArrayList<>();
        // ...
        Stream<String> stream1 = list.stream();
        
        Set<String> set = new HashSet<>();
        // ...
        Stream<String> stream2 = set.stream();
        
        Vector<String> vector = new Vector<>();
        // ...
        Stream<String> stream3 = vector.stream();
    }
}

The default method of adding an interface Collection - stream method, its source code is as follows:

default Stream<E> stream() {
    return StreamSupport.stream(spliterator(), false);
}

 

According to acquire Flow Map

Interface java.util.Map not Collection sub-interface, data structures and which does not conform KV single feature stream elements, so obtaining a corresponding flow required fraction key, value, or the like where entry:

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;

public class Demo04Stream {
    public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();
        // ...
        Set<String> key = map.keySet();
        Set<String> value = (Set<String>) map.values();
        Set<Map.Entry<String, String>> entries = map.entrySet();
        
        Stream<String> keyStream = key.stream();
        Stream<String> valueStream = value.stream();
        Stream<Map.Entry<String, String>> entryStream = entries.stream();
    }
}

Map interface rather special, it saved data is Key - Value, so be divided into Key, Value and Key-Value circumstances, Key-Value entry case that is the case. Each collection are stored in the Set, and Set interfaces inherited from the Collection interface, so there is an indirect method of using the default stream Collection interface here, to get the flow.

 

According to acquire an array of stream

If the map is not a collection or use of an array but, due to the array object can not add the default method, the Stream interface provides a static method  of, very simple to use:

import java.util.stream.Stream;

public class Demo05GetStream {
    public static void main(String[] args) {
        String[] array = { "Java", "C", "Python", "Hadoop", "Spark" };
        Stream<String> stream = Stream.of(array);
    }
}
Note: of parametric methods is actually a variable parameter, so the support arrays.

 

          

Guess you like

Origin www.cnblogs.com/liyihua/p/12289648.html