提交 19a92fe9 编写于 作者: M Mars Liu

exerices

上级 42862151
......@@ -2,6 +2,5 @@
"node_id": "java-0f1fc49b2e1c41368403ca2239810920",
"keywords": [],
"children": [],
"export": [],
"title": "收集器简介"
"export": ["to_list.json"]
}
\ No newline at end of file
{
"type": "code_options",
"author": "刘鑫",
"source": "to_list.md",
"notebook_enable": false
}
\ No newline at end of file
# Stream To List
下面的选项尝试从一个整数 Stream 对象中构造 List。那么不正确的是:
## 答案
```java
return new ArrayList<>(stream);
```
## 选项
### 推荐做法
```java
return stream.collect(Collectors.toList());
```
### 朴素做法,不推荐但可用
```java
List<Integer> result = new ArrayList<>();
stream.foreach(x -> {
result.add(x);
});
return result;
```
### 方法引用
```java
List<Integer> result = new ArrayList<>();
stream.foreach(result::add);
```
{
"type": "code_options",
"author": "刘鑫",
"source": "to_set.md",
"notebook_enable": false
}
\ No newline at end of file
# Stream To Set
下面的选项尝试从一个整数 Stream 对象中构造 List。那么不正确的是:
## 答案
```java
return new HashSet<>(stream);
```
## 选项
### 推荐做法
```java
return stream.collect(Collectors.toSet());
```
### 朴素做法,不推荐但可用
```java
List<Integer> result = new ArrayList<>();
stream.foreach(x -> {
result.add(x);
});
return result;
```
### 方法引用
```java
Set<Integer> result = new HashSet<>();
stream.foreach(result::add);
```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册