单列集合
List,Set
直接 集合对象.stream();
List<Author> author = getAuthor();
author.stream()
双列集合
Map
先转换为单列集合,再创建流
Map<String, Integer> map = new HashMap<>();
map.put("蜡笔小新",19);
map.put("黑子",17);
map.put("日向翔阳",16);
Stream<Map.Entry<String, Integer>> stream3 = map.entrySet().stream();
entrySet()将Map转换为单列集合
数组
Arrays.stream(数组)或或者Stream.of(数组)
Integer[] arr = {1,2,3,4,5};
int[] arr1 = {1,2,3,4,5};
Stream<Integer> stream = Arrays.stream(arr);
Stream<Integer> stream2 = Stream.of(arr);
但是,如果使用Stream.of(数组)创建stream流时,
元素为基本类型时,记得使用具体Stream子类创建,如:
IntStream stream1 = Arrays.stream(arr1);
IntStream arr11 = IntStream.of(arr1);
否则的话会出现:
Stream<IntStream> arr111 = Stream.of(arr11);
流中是一个数组,而不是数组中的元素,具体原因不详