对各字符串数据(" ZhongGuo "," Meiguo "," deguo "," faguo "," eluosi "," yidu "}完成:分四种方式创建 stream 流对象,并打印输出各元素: a )集合对象调用 stream ()静态方法 b ) Stream 接口的 ofO 静态方法获取基本类型包装类数组 c ) Arrays 工具类的 streamO 静态方法 d ) Stream 接口的 ofO 静态方法对字符串源数据直接创建
时间: 2023-07-19 09:07:29 浏览: 327
a) 集合对象调用 stream() 静态方法:
```java
List<String> list = Arrays.asList("ZhongGuo", "Meiguo", "deguo", "faguo", "eluosi", "yidu");
Stream<String> stream1 = list.stream();
stream1.forEach(System.out::println);
```
输出:
```
ZhongGuo
Meiguo
deguo
faguo
eluosi
yidu
```
b) Stream 接口的 ofO 静态方法获取基本类型包装类数组:
```java
Stream<String> stream2 = Stream.of("ZhongGuo", "Meiguo", "deguo", "faguo", "eluosi", "yidu");
stream2.forEach(System.out::println);
```
输出:
```
ZhongGuo
Meiguo
deguo
faguo
eluosi
yidu
```
c) Arrays 工具类的 streamO 静态方法:
```java
String[] arr = {"ZhongGuo", "Meiguo", "deguo", "faguo", "eluosi", "yidu"};
Stream<String> stream3 = Arrays.stream(arr);
stream3.forEach(System.out::println);
```
输出:
```
ZhongGuo
Meiguo
deguo
faguo
eluosi
yidu
```
d) Stream 接口的 ofO 静态方法对字符串源数据直接创建:
```java
Stream<String> stream4 = Stream.of("ZhongGuo", "Meiguo", "deguo", "faguo", "eluosi", "yidu");
stream4.forEach(System.out::println);
```
输出:
```
ZhongGuo
Meiguo
deguo
faguo
eluosi
yidu
```
阅读全文