最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網 會員登陸 & 注冊

java8 list 轉Map

2022-05-13 11:08 作者:wulizhao1  | 我要投稿

1 public Map<Long, Account> getIdAccountMap(List<Account> accounts) {

? ? return accounts.stream().collect(Collectors.toMap(Account::getId, account -> account));

}

2 重復key的情況

在list轉為map時,作為key的值有可能重復,這時候流的處理會拋出個異常:Java.lang.IllegalStateException:Duplicate key。

這時候就要在toMap方法中指定當key沖突時key的選擇。(這里是選擇第二個key覆蓋第一個key):

public Map<String, Account> getNameAccountMap(List<Account> accounts) {

? ? return accounts.stream().collect(Collectors.toMap(Account::getUsername, Function.identity(), (key1, key2) -> key2));

}? ??

3分組

Map<String, List<CompanyInfo>> companyByMonth = companyInfos.stream()
? ? ? ?.collect(Collectors.groupingBy(o -> o.getCreatedTime().toInstant().atZone(ZoneId.systemDefault())
? ? ? ? ? ? ? ?.toLocalDateTime().getYear() + separate +
? ? ? ? ? ? ? ?o.getCreatedTime().toInstant().atZone(ZoneId.systemDefault())
? ? ? ? ? ? ? ? ? ? ? ?.toLocalDateTime().getMonthValue()));


java8 list 轉Map的評論 (共 條)

分享到微博請遵守國家法律
宿松县| 丰顺县| 张家口市| 行唐县| 乐东| 宁化县| 乡宁县| 中方县| 治县。| 云和县| 米林县| 尼勒克县| 托克托县| 开封市| 黄山市| 鹰潭市| 铁岭市| 宽甸| 芮城县| 延长县| 乃东县| 卢氏县| 嘉定区| 乌什县| 循化| 荔波县| 海安县| 诸暨市| 永新县| 苏尼特右旗| 钟祥市| 鹰潭市| 临清市| 商河县| 怀远县| 依安县| 莆田市| 芜湖县| 昌图县| 汽车| 金沙县|