1、stream流的三种获取方法:
-
单列集合 可以使用Collection接口中的默认方法stream()生成流。
ArrayList<String> arrayList=new ArrayList<>(); arrayList.add("aaa"); arrayList.add("bbb"); arrayList.add("ccc"); arrayList.stream().forEach(s->System.out.println(s));
- 双列集合 间接生成流:可以先通过keySet或entrySet获取Set集合,再获取stream流。
HashMap<String, Integer> hmHashMap = new HashMap<>(); hmHashMap.put("zhangsan",23); hmHashMap.put("lisi",24); hmHashMap.put("wangwu",25); hmHashMap.put("zhaoliu",26); hmHashMap.put("qianqi",27); #流遍历取出每个key hmHashMap.keySet().stream().forEach(s->System.out.println(s)); #流遍历取出每个entry对象 hmHashMap.entrySet().forEach(s->System.out.println(s));
- 数组 Arrays 中的静态方法stream生成流。
int[] arr= {1,2,3,4,5,6}; Arrays.stream(arr).forEach(s->System.out.println(s));
- 同种数据类型的多个数据 1,2,3,4,5,6; "aaa","bbb","vvv","ddd","",""....... 使用Stream.of(T......values)生成流。
Stream.of(1,2,3,4,5,6,7).forEach(s->System.out.println(s));