提交 d040f971 编写于 作者: M Mars Liu

optional

上级 3647836a
...@@ -2,5 +2,5 @@ ...@@ -2,5 +2,5 @@
"node_id": "java-05498c81f6be42a2a44ddb4de0178420", "node_id": "java-05498c81f6be42a2a44ddb4de0178420",
"keywords": [], "keywords": [],
"children": [], "children": [],
"export": [] "export": ["partition_by.json"]
} }
\ No newline at end of file
{
"type": "code_options",
"author": "刘鑫",
"source": "partition_by.md",
"notebook_enable": false,
"exercise_id": "0fdf50ac2303468090d036e7fc0ee726"
}
\ No newline at end of file
# 二分
现有一个数据流 `Stream<Integer> stream`, 下列代码尝试把它按正负号分为两部分(不拘于某种结果类型,能够区分两类数据即可),其中有错的是?
## 答案
```java
Stream<Integer> upstream=stream.filter(x->x>=0);
Stream<Integer> downstream=stream.filter(x->x< 0);
```
## 选项
### 常规方法
```java
Collectors.partitionBy(x->x>=0,stream);
```
### 常规方法
```java
Collector<Integer, ?, Map<Boolean, List<Integer>>>collector=Collectors.partitioningBy(x->x>0);
stream.collect(collector);
```
### 朴素方法
```java
List<Integer> upstream=new ArrayList<>();
List<Integer> downstream=new ArrayList<>();
stream.foreach(x->{
if(x>=0){
upstream.add(x);
}else{
downstream.add(x);
}
})
```
\ No newline at end of file
{
"node_id": "java-36a3b51a36624ee8930794d64da6b22a",
"keywords": [],
"children": [],
"export": ["optional.json"]
}
\ No newline at end of file
...@@ -2,5 +2,6 @@ ...@@ -2,5 +2,6 @@
"type": "code_options", "type": "code_options",
"author": "刘鑫", "author": "刘鑫",
"source": "form.md", "source": "form.md",
"notebook_enable": false "notebook_enable": false,
"export": ["optional.json"]
} }
\ No newline at end of file
# Optional 应用
现有一个 `String content` 字符串,它有可能是 NULL ,也有可能是字符串,那么下列将其转为 Optional 的操作,哪个是错的?
## 答案
```java
var opt = Optional.of(content);
```
## 选项
### 常规方法
```java
var opt = Optional.ofNullable(content);
```
### 平凡方法,不推荐,但可用
```java
Optional<String> opt;
if(content == null){
opt = Optional.empty();
} else {
opt = Optional.of(content);
}
```
### C
以上全都可用,没有错误代码
{
"node_id": "java-6a0ad22b08cd4520b453904d832936ea",
"keywords": [],
"children": [],
"export": ["optional.json"]
}
\ No newline at end of file
{
"type": "code_options",
"author": "刘鑫",
"source": "form.md",
"notebook_enable": false,
"export": ["optional.json"]
}
\ No newline at end of file
...@@ -2,6 +2,5 @@ ...@@ -2,6 +2,5 @@
"node_id": "java-c3a2ef586dd7449788102b631419992f", "node_id": "java-c3a2ef586dd7449788102b631419992f",
"keywords": [], "keywords": [],
"children": [], "children": [],
"export": ["optional.json"], "export": []
"title": "用Optional取代null"
} }
\ No newline at end of file
...@@ -2756,7 +2756,9 @@ ...@@ -2756,7 +2756,9 @@
{ {
"类型转换前先做检查": { "类型转换前先做检查": {
"node_id": "java-48593d705752415e95c42b09d36bc2a4", "node_id": "java-48593d705752415e95c42b09d36bc2a4",
"keywords": [], "keywords": [
"instanceof"
],
"children": [ "children": [
{ {
"使用类字面常量": { "使用类字面常量": {
...@@ -2802,7 +2804,9 @@ ...@@ -2802,7 +2804,9 @@
{ {
"反射:运行时类信息": { "反射:运行时类信息": {
"node_id": "java-42b26ca8a2384dcfb02fca6e159ace67", "node_id": "java-42b26ca8a2384dcfb02fca6e159ace67",
"keywords": [], "keywords": [
"annotation"
],
"children": [ "children": [
{ {
"类方法抽取器": { "类方法抽取器": {
...@@ -4009,9 +4013,6 @@ ...@@ -4009,9 +4013,6 @@
"keywords": [], "keywords": [],
"children": [] "children": []
} }
}
]
}
}, },
{ {
"分区": { "分区": {
...@@ -4019,14 +4020,32 @@ ...@@ -4019,14 +4020,32 @@
"keywords": [], "keywords": [],
"children": [] "children": []
} }
}
]
}
}, },
{ {
"Optional": { "Optional": {
"node_id": "java-c3a2ef586dd7449788102b631419992f", "node_id": "java-c3a2ef586dd7449788102b631419992f",
"keywords": [], "keywords": [],
"children": [
{
"Optional与Null": {
"node_id": "java-36a3b51a36624ee8930794d64da6b22a",
"keywords": [],
"children": [] "children": []
} }
}, },
{
"一致性": {
"node_id": "java-6a0ad22b08cd4520b453904d832936ea",
"keywords": [],
"children": []
}
}
]
}
},
{ {
"Java模块系统": { "Java模块系统": {
"node_id": "java-81c079af472746fc838268974b43dc55", "node_id": "java-81c079af472746fc838268974b43dc55",
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册