-
-
-
- stream流打印单列集合:
//单列集合获取
ArrayList<String> list=new ArrayList<>();
Collections.addAll(list,"a","a","a");
//stream流
Stream<String> stream = list.stream();
//用终结方法打印所有数据
stream.forEach(new Consumer<String>() {
@Override
public void accept(String s) {
System.out.println(s);
}
});
//stream流的链式编程
list.stream().forEach(s -> System.out.println(s)); - 双列集合使用stream流:
//双列集合获取
HashMap<String,Integer> hashMap=new HashMap<>();
hashMap.put("bbb",111);
hashMap.put("ccc",111);
hashMap.put("ddd",111);
//获取stream流的第一种方式
hashMap.keySet().stream().forEach(s -> System.out.println(s));
//获取stream流的第二种方式
hashMap.entrySet().stream().forEach(stringIntegerEntry -> System.out.println(stringIntegerEntry)); - 数组使用stream流
int[] number={1,2,3,4,5};
Arrays.stream(number).forEach(s-> System.out.println(s)); - 一堆零散数据使用stream流:
Stream.of(1,2,3,4,5).forEach(s-> System.out.println(s));
- Stream接口细节: