提交 3647836a 编写于 作者: M Mars Liu

group by

上级 e510b2f9
...@@ -5,6 +5,5 @@ ...@@ -5,6 +5,5 @@
"export": [ "export": [
"map.json", "map.json",
"sum.json" "sum.json"
], ]
"title": "通过行为参数化传递代码"
} }
\ No newline at end of file
{
"type": "code_options",
"author": "刘鑫",
"source": "sum.md",
"notebook_enable": false,
"exercise_id": "e2c94b7714a349219704ee0b564d7086"
}
\ No newline at end of file
...@@ -2,5 +2,6 @@ ...@@ -2,5 +2,6 @@
"type": "code_options", "type": "code_options",
"author": "刘鑫", "author": "刘鑫",
"source": "handler.md", "source": "handler.md",
"notebook_enable": false "notebook_enable": false,
"exercise_id": "e5379f8e71654ea1a3d07fe86e58932f"
} }
\ No newline at end of file
...@@ -2,5 +2,6 @@ ...@@ -2,5 +2,6 @@
"type": "code_options", "type": "code_options",
"author": "刘鑫", "author": "刘鑫",
"source": "lambda.md", "source": "lambda.md",
"notebook_enable": false "notebook_enable": false,
"exercise_id": "8fef994ebb804b72a1e6cdd957456357"
} }
\ No newline at end of file
{
"type": "code_options",
"author": "刘鑫",
"source": "map_reduce.md",
"notebook_enable": true
}
\ No newline at end of file
# Map Reduce
现有一个csv文件,共一千万行数据,每一行数据都是按逗号分隔的浮点数, 其中第一行是表头,需要忽略。假设我们已经得到了这个文件 对应的文本行 stream 对象 `Stream<String> stream`
,现在希望对所有行所有列求和,已知现在我们手上有一个16核的机器,下列 哪一个计算方法最快?
## 答案
```java
stream.drop(1).parallel()
.flatMap(str->Arrays.stream(str.split(",")))
.map(Float::parseFloat)
.reduce(0f,Float::sum);
```
## 选项
### 串行
```java
stream.drop(1)
.flatMap(str->Arrays.stream(str.split(",")))
.map(Float::parseFloat)
.reduce(0f,Float::sum);
```
### 并发不安全
```java
Float result = 0;
stream.drop(1).parallel().forEach(line -> {
for(var cell: line.split(",")){
result += Float.parseFloat(cell);
}
});
```
### 没有并行,无法充分利用 cpu 资源
```java
Float result = 0;
stream.drop(1).forEach(line -> {
for(var cell: line.split(",")){
result+=Float.parseFloat(cell);
}
});
```
### 没有并行
```java
Float result = 0;
stream.drop(1).map(str->Arrays.stream(str.split(","))).forEach(line -> {
for(var cell: line){
result+=Float.parseFloat(cell);
}
});
```
...@@ -2,5 +2,6 @@ ...@@ -2,5 +2,6 @@
"type": "code_options", "type": "code_options",
"author": "刘鑫", "author": "刘鑫",
"source": "to_list.md", "source": "to_list.md",
"notebook_enable": false "notebook_enable": false,
"exercise_id": "f5f5d0ca291d429999408159a59249d5"
} }
\ No newline at end of file
...@@ -2,5 +2,6 @@ ...@@ -2,5 +2,6 @@
"type": "code_options", "type": "code_options",
"author": "刘鑫", "author": "刘鑫",
"source": "to_map.md", "source": "to_map.md",
"notebook_enable": false "notebook_enable": false,
"exercise_id": "0febee3d748c4a5dab801452fb67e7e8"
} }
\ No newline at end of file
...@@ -2,5 +2,6 @@ ...@@ -2,5 +2,6 @@
"type": "code_options", "type": "code_options",
"author": "刘鑫", "author": "刘鑫",
"source": "to_set.md", "source": "to_set.md",
"notebook_enable": false "notebook_enable": false,
"exercise_id": "b729d96108664a058281d797b2214c21"
} }
\ No newline at end of file
...@@ -2,6 +2,5 @@ ...@@ -2,6 +2,5 @@
"node_id": "java-05498c81f6be42a2a44ddb4de0178420", "node_id": "java-05498c81f6be42a2a44ddb4de0178420",
"keywords": [], "keywords": [],
"children": [], "children": [],
"export": [], "export": []
"title": "分区"
} }
\ No newline at end of file
{
"node_id": "java-401df00d53964647a4fa249007f30a8b",
"keywords": []
}
\ No newline at end of file
...@@ -2,6 +2,6 @@ ...@@ -2,6 +2,6 @@
"node_id": "java-c3a2ef586dd7449788102b631419992f", "node_id": "java-c3a2ef586dd7449788102b631419992f",
"keywords": [], "keywords": [],
"children": [], "children": [],
"export": [], "export": ["optional.json"],
"title": "用Optional取代null" "title": "用Optional取代null"
} }
\ No newline at end of file
{ {
"type": "code_options", "type": "code_options",
"author": "刘鑫", "author": "刘鑫",
"source": "sum.md", "source": "form.md",
"notebook_enable": false "notebook_enable": false
} }
\ No newline at end of file
# Optional 应用
现有一个 `Optional<String> opt` 对象,下列代码中,哪一个操作有可能因为opt里携带了空值而抛出异常?
## 答案
```java
var item = opt.get();
```
## 选项
### A
```java
return opt.flatMap(s->post(s));
```
### B
```java
return opt.map(s->s.split(","));
```
### C
```java
var item = opt.orElse("nothing");
```
### D
```java
if(opt.isPresent()){
return opt.get();
} else {
return findNext();
}
```
### E
```java
if(opt.isEmpty()){
return 0;
} else {
rank(opt.get())
}
```
{
"node_id": "java-7dbbac2293584659a17e7dee9d666c56",
"keywords": [],
"children": [],
"export": [],
"title": "分区的优势"
}
\ No newline at end of file
{
"node_id": "java-a6a177d99c7d46f48f50668aa124bcfe",
"keywords": [],
"children": [],
"export": [],
"title": "将数字按质数和非质数分区"
}
\ No newline at end of file
{
"node_id": "java-ff25a7a669714604a7605822962d4c63",
"keywords": [],
"children": [],
"export": [],
"title": "收集器接口"
}
\ No newline at end of file
{
"node_id": "java-b4b04051f4be46288baaf54f55298949",
"keywords": [],
"children": [
{
"采用防御式检查减少NullPointerException": {
"keywords": [],
"children": [],
"node_id": "java-710beac55f7c4ce58da66d9ebc319dd4",
"title": "采用防御式检查减少NullPointerException"
}
},
{
"其他语言中null的替代品": {
"keywords": [],
"children": [],
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"title": "其他语言中null的替代品"
}
}
],
"export": [],
"title": "如何为缺失的值建模"
}
\ No newline at end of file
{
"node_id": "java-d329521188284298b68e7d7d56c66557",
"keywords": [],
"children": [],
"export": [],
"title": "Optional类入门"
}
\ No newline at end of file
{
"node_id": "java-c73d3483b19e4d64a3c5bf802bb0f2c6",
"keywords": [],
"children": [
{
"创建Optional对象": {
"keywords": [],
"children": [],
"node_id": "java-9843c864f2b848129407ac2d6ecfaacd",
"title": "创建Optional对象"
}
},
{
"使用map从Optional对象中提取和转换值": {
"keywords": [],
"children": [],
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"title": "使用map从Optional对象中提取和转换值"
}
},
{
"使用flatMap链接Optional对象": {
"keywords": [],
"children": [],
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"title": "使用flatMap链接Optional对象"
}
},
{
"操纵由Optional对象构成的Stream": {
"keywords": [],
"children": [],
"node_id": "569d5e11c4fc5de7844053d9a733c5e8"
}
},
{
"默认行为及解引用Optional对象": {
"keywords": [],
"children": [],
"node_id": "569d5e11c4fc5de7844053d9a733c5e8"
}
},
{
"两个Optional对象的组合": {
"keywords": [],
"children": [],
"node_id": "569d5e11c4fc5de7844053d9a733c5e8"
}
},
{
"使用filter剔除特定的值": {
"keywords": [],
"children": [],
"node_id": "569d5e11c4fc5de7844053d9a733c5e8"
}
}
],
"export": [],
"title": "应用Optional的几种模式"
}
\ No newline at end of file
...@@ -2323,27 +2323,6 @@ ...@@ -2323,27 +2323,6 @@
"children": [] "children": []
} }
}, },
{
"Java新IO概述": {
"node_id": "java-f824cb48cef64bbcad371bb304b26d10",
"keywords": [],
"children": []
}
},
{
"使用Buffer": {
"node_id": "java-4bae5939bee44a31ba92906e490293b0",
"keywords": [],
"children": []
}
},
{
"使用Channel": {
"node_id": "java-9e2f3a44543c457e84d3d8a2e8321b65",
"keywords": [],
"children": []
}
},
{ {
"字符集和Charset": { "字符集和Charset": {
"node_id": "java-984b508fc9a54650aa9509eeb198f925", "node_id": "java-984b508fc9a54650aa9509eeb198f925",
...@@ -2361,7 +2340,10 @@ ...@@ -2361,7 +2340,10 @@
{ {
"Path接口": { "Path接口": {
"node_id": "java-386113b4f6274eb591e1f01b39f0c8ce", "node_id": "java-386113b4f6274eb591e1f01b39f0c8ce",
"keywords": [], "keywords": [
"Paths",
"Path"
],
"children": [] "children": []
} }
}, },
...@@ -2383,7 +2365,11 @@ ...@@ -2383,7 +2365,11 @@
{ {
"网络编程基础": { "网络编程基础": {
"node_id": "java-59e7173695814c90858bc221e8688329", "node_id": "java-59e7173695814c90858bc221e8688329",
"keywords": [], "keywords": [
"网络",
"通信",
"协议"
],
"children": [ "children": [
{ {
"网络通信协议": { "网络通信协议": {
...@@ -2677,7 +2663,14 @@ ...@@ -2677,7 +2663,14 @@
{ {
"Java 11标准化的HTTP Client": { "Java 11标准化的HTTP Client": {
"node_id": "java-dd60957a0a7f4155a68aea7c6807d504", "node_id": "java-dd60957a0a7f4155a68aea7c6807d504",
"keywords": [], "keywords": [
"websocket",
"get",
"post",
"delete",
"put",
"请求"
],
"children": [ "children": [
{ {
"发送同步GET请求": { "发送同步GET请求": {
...@@ -3972,186 +3965,47 @@ ...@@ -3972,186 +3965,47 @@
"keywords": [], "keywords": [],
"children": [ "children": [
{ {
"通过行为参数化传递代码": { "行为抽象和Lambda": {
"node_id": "java-fb711fb0dfbb40db974476ea2d9faf9d", "node_id": "java-401df00d53964647a4fa249007f30a8b",
"keywords": [], "keywords": [],
"children": []
}
},
{
"Lambda表达式": {
"node_id": "java-a3b60740abe74bb8a6c16dec381d31c4",
"keywords": [
"lambda",
"高阶函数",
"方法引用",
"SAM",
"函数式接口"
],
"children": [ "children": [
{ {
"Lambda概念": { "流和行为抽象": {
"node_id": "java-f19e427139fc4f1b881ce286e0b0e4f6", "node_id": "java-fb711fb0dfbb40db974476ea2d9faf9d",
"keywords": [], "keywords": [],
"children": [] "children": []
} }
}, },
{ {
"使用函数式接口": { "Lambda表达式": {
"node_id": "java-24d804bc97ba47d0ba5798a35eb7a4bd", "node_id": "java-a3b60740abe74bb8a6c16dec381d31c4",
"keywords": [], "keywords": [
"children": [ "lambda",
{ "高阶函数",
"Predicate": { "方法引用",
"keywords": [], "SAM",
"children": [], "函数式接口"
"node_id": "java-a48e98bbc9ee412e8712c787018e0534", ],
"title": "Predicate"
}
},
{
"Consumer": {
"keywords": [],
"children": [],
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"title": "Consumer"
}
},
{
"Function": {
"keywords": [],
"children": [],
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"title": "Function"
}
}
]
}
},
{
"类型检查、类型推断以及限制": {
"node_id": "java-5a14598266004ad1a6dc7375348b1620",
"keywords": [],
"children": [
{
"类型检查": {
"keywords": [],
"children": [],
"node_id": "java-df8304bb13e94bec824365bfdec8b1c2",
"title": "类型检查"
}
},
{
"同样的Lambda,不同的函数式接口": {
"keywords": [],
"children": [],
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"title": "同样的Lambda,不同的函数式接口"
}
},
{
"类型推断": {
"keywords": [],
"children": [],
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"title": "类型推断"
}
},
{
"使用局部变量": {
"keywords": [],
"children": [],
"node_id": "569d5e11c4fc5de7844053d9a733c5e8"
}
}
]
}
},
{
"方法引用": {
"node_id": "java-a5a5542819914bc38d0454af6cf943a0",
"keywords": [],
"children": [
{
"管中窥豹": {
"keywords": [],
"children": [],
"node_id": "java-01b7b64326db41ffbbdca11fb1f91ada",
"title": "管中窥豹"
}
},
{
"构造函数引用": {
"keywords": [],
"children": [],
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"title": "构造函数引用"
}
}
]
}
},
{
"高阶函数": {
"node_id": "java-a7c7ebe0698741e0a6adea161a5139a6",
"keywords": [],
"children": []
}
}
]
}
},
{
"流": {
"node_id": "java-9f8b0da343fd415c9b29380647b0f1f8",
"keywords": [],
"children": []
}
},
{
"收集器": {
"node_id": "java-0f1fc49b2e1c41368403ca2239810920",
"keywords": [],
"children": [
{
"收集器用作高级归约": {
"node_id": "java-a19efae63666441eb21c86dff3045c70",
"keywords": [],
"children": [] "children": []
} }
}, },
{ {
"预定义收集器": { "流": {
"node_id": "java-0c52f1a755884deeb5e73305e0e7b744", "node_id": "java-9f8b0da343fd415c9b29380647b0f1f8",
"keywords": [],
"children": []
}
}
]
}
},
{
"分组": {
"node_id": "java-bbff17faa9a04aebbe14c62caccc2309",
"keywords": [],
"children": [
{
"操作分组的元素": {
"node_id": "java-4cc01469b31d42ac80854c4024c1b15a",
"keywords": [], "keywords": [],
"children": [] "children": []
} }
}, },
{ {
"多级分组": { "收集器": {
"node_id": "java-873636226f0e44ad9269f2a13fe2adb9", "node_id": "java-0f1fc49b2e1c41368403ca2239810920",
"keywords": [], "keywords": [],
"children": [] "children": []
} }
}, },
{ {
"按子组收集数据": { "分组": {
"node_id": "java-1223956aa7ae47a9a2e517dc23c3f371", "node_id": "java-bbff17faa9a04aebbe14c62caccc2309",
"keywords": [], "keywords": [],
"children": [] "children": []
} }
...@@ -4163,128 +4017,14 @@ ...@@ -4163,128 +4017,14 @@
"分区": { "分区": {
"node_id": "java-05498c81f6be42a2a44ddb4de0178420", "node_id": "java-05498c81f6be42a2a44ddb4de0178420",
"keywords": [], "keywords": [],
"children": [
{
"分区的优势": {
"node_id": "java-7dbbac2293584659a17e7dee9d666c56",
"keywords": [],
"children": []
}
},
{
"将数字按质数和非质数分区": {
"node_id": "java-a6a177d99c7d46f48f50668aa124bcfe",
"keywords": [],
"children": []
}
}
]
}
},
{
"收集器接口": {
"node_id": "java-ff25a7a669714604a7605822962d4c63",
"keywords": [],
"children": [] "children": []
} }
}, },
{ {
"用Optional取代null": { "Optional": {
"node_id": "java-c3a2ef586dd7449788102b631419992f", "node_id": "java-c3a2ef586dd7449788102b631419992f",
"keywords": [], "keywords": [],
"children": [ "children": []
{
"如何为缺失的值建模": {
"node_id": "java-b4b04051f4be46288baaf54f55298949",
"keywords": [],
"children": [
{
"采用防御式检查减少NullPointerException": {
"keywords": [],
"children": [],
"node_id": "java-710beac55f7c4ce58da66d9ebc319dd4",
"title": "采用防御式检查减少NullPointerException"
}
},
{
"其他语言中null的替代品": {
"keywords": [],
"children": [],
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"title": "其他语言中null的替代品"
}
}
]
}
},
{
"Optional类入门": {
"node_id": "java-d329521188284298b68e7d7d56c66557",
"keywords": [],
"children": []
}
},
{
"应用Optional的几种模式": {
"node_id": "java-c73d3483b19e4d64a3c5bf802bb0f2c6",
"keywords": [],
"children": [
{
"创建Optional对象": {
"keywords": [],
"children": [],
"node_id": "java-9843c864f2b848129407ac2d6ecfaacd",
"title": "创建Optional对象"
}
},
{
"使用map从Optional对象中提取和转换值": {
"keywords": [],
"children": [],
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"title": "使用map从Optional对象中提取和转换值"
}
},
{
"使用flatMap链接Optional对象": {
"keywords": [],
"children": [],
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"title": "使用flatMap链接Optional对象"
}
},
{
"操纵由Optional对象构成的Stream": {
"keywords": [],
"children": [],
"node_id": "569d5e11c4fc5de7844053d9a733c5e8"
}
},
{
"默认行为及解引用Optional对象": {
"keywords": [],
"children": [],
"node_id": "569d5e11c4fc5de7844053d9a733c5e8"
}
},
{
"两个Optional对象的组合": {
"keywords": [],
"children": [],
"node_id": "569d5e11c4fc5de7844053d9a733c5e8"
}
},
{
"使用filter剔除特定的值": {
"keywords": [],
"children": [],
"node_id": "569d5e11c4fc5de7844053d9a733c5e8"
}
}
]
}
}
]
} }
}, },
{ {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册