国产精品天干天干,亚洲毛片在线,日韩gay小鲜肉啪啪18禁,女同Gay自慰喷水

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

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的評論 (共 條)

分享到微博請遵守國家法律
额敏县| 安康市| 鸡泽县| 汉寿县| 汤阴县| 密山市| 衡水市| 威信县| 新泰市| 固阳县| 宜宾县| 鸡西市| 景洪市| 桐庐县| 卢龙县| 瓦房店市| 佛坪县| 远安县| 临猗县| 澳门| 新兴县| 平塘县| 浦东新区| 宜宾市| 九龙县| 肃南| 漳浦县| 清远市| 玉门市| 甘南县| 上林县| 托克逊县| 丹东市| 台湾省| 汕头市| 莱州市| 盐池县| 长岭县| 扬州市| 杭州市| 荔浦县|