1.Collection体系的集合可以使用默认方法stream()生成流
default Stream< E > stream()
代码如下:
package StreamTest;import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Stream;public class StreamDemo01 {public static void main(String[] args){List<String> list = new ArrayList<String>();Stream<String> listStream = list.stream();Set<String> set = new HashSet<String>();Stream<String> setStream = set.stream();}
}
2.Map体系的集合间接的生成流
代码如下:
package StreamTest;import java.util.*;
import java.util.stream.Stream;public class StreamDemo01 {public static void main(String[] args){Map<String,Integer> map = new HashMap<String,Integer>();Stream<String> keyStream = map.keySet().stream();Stream<Integer> valueStream = map.values().stream();Stream<Map.Entry<String, Integer>> entryStream = map.entrySet().stream();}
}
3.数组可以通过Stream接口的静态方法of(T… values)生成流
代码如下:
package StreamTest;import java.util.*;
import java.util.stream.Stream;public class StreamDemo01 {public static void main(String[] args){String[] strArray = {"hello","java","world"};Stream<String> strArrayStream = Stream.of(strArray);Stream<String> strArrayStream02 = Stream.of("hello", "java", "world");Stream<Integer> intStream = Stream.of(10,20,30);}
}