Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
CSDN 技术社区
skill_tree_java
提交
22391b5f
S
skill_tree_java
项目概览
CSDN 技术社区
/
skill_tree_java
通知
43
Star
8
Fork
4
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
4
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
skill_tree_java
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
4
Issue
4
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
22391b5f
编写于
3年前
作者:
M
Mars Liu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add to map
上级
fc7afb4e
master
无相关合并请求
变更
5
展开全部
隐藏空白更改
内联
并排
Showing
5 changed file
with
80 addition
and
1610 deletion
+80
-1610
data/3.Java高阶/12.Apache Common/config.json
data/3.Java高阶/12.Apache Common/config.json
+5
-0
data/3.Java高阶/4.收集器/config.json
data/3.Java高阶/4.收集器/config.json
+6
-1
data/3.Java高阶/4.收集器/to_map.json
data/3.Java高阶/4.收集器/to_map.json
+7
-0
data/3.Java高阶/4.收集器/to_map.md
data/3.Java高阶/4.收集器/to_map.md
+60
-0
data/tree.json
data/tree.json
+2
-1609
未找到文件。
data/3.Java高阶/12.Apache Common/config.json
0 → 100644
浏览文件 @
22391b5f
{
"node_id"
:
"java-e5d307b8ce2049648b4afd9fecdb48b0"
,
"keywords"
:
[]
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
data/3.Java高阶/4.收集器/config.json
浏览文件 @
22391b5f
...
...
@@ -2,5 +2,9 @@
"node_id"
:
"java-0f1fc49b2e1c41368403ca2239810920"
,
"keywords"
:
[],
"children"
:
[],
"export"
:
[
"to_list.json"
]
"export"
:
[
"to_list.json"
,
"to_set.json"
,
"to_map.json"
]
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
data/3.Java高阶/4.收集器/to_map.json
0 → 100644
浏览文件 @
22391b5f
{
"type"
:
"code_options"
,
"author"
:
"刘鑫"
,
"source"
:
"to_map.md"
,
"notebook_enable"
:
false
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
data/3.Java高阶/4.收集器/to_map.md
0 → 100644
浏览文件 @
22391b5f
# Stream To Map
现有类型 Person 如下:
```
java
public
class
Person
{
private
final
int
id
;
private
final
String
name
;
public
Person
(
int
id
,
String
name
){
this
.
id
=
id
;
this
.
name
=
name
;
}
public
int
getId
(){
return
id
;
}
public
String
getName
(){
return
name
;
}
}
```
下面的程序将
`Stream<Person> people`
中的数据整理到一个 Map
<Integer
,
String
>
字典中,键和值分别对应 Person 类型的 id 和name。其中有错的是:
## 答案
```
java
Map
<
Integer
,
String
>
dict
=
new
Map
();
people
.
collect
(
p
->{
dict
.
put
(
p
.
getId
(),
p
.
getName
());
});
```
## 选项
### 常规收集器
```
java
Map
<
Integer
,
String
>
dict
=
people
().
collect
(
Collectors
.
toMap
(
Person:
:
getId
,
Person:
:
getName
));
```
### 朴素的过程化方法
```
java
Map
<
Integer
,
String
>
dict
=
new
HashMap
<>();
people
.
forEach
(
p
->{
dict
.
put
(
p
.
getId
(),
p
.
getName
());
});
```
### 朴素的 for 循环,不推荐但是能用
```
java
Map
<
Integer
,
String
>
dict
=
new
HashMap
<>();
for
(
var
p:
people
.
toList
()){
dict
.
put
(
p
.
getId
(),
p
.
getName
());
}
```
This diff is collapsed.
Click to expand it.
data/tree.json
浏览文件 @
22391b5f
此差异已折叠。
点击以展开。
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录
反馈
建议
客服
返回
顶部