Java8中l(wèi)ist轉(zhuǎn)map方法總結(jié)

背景
在最近的工作開發(fā)之中,慢慢習(xí)慣了很多Java8中的Stream的用法,很方便而且也可以并行的去執(zhí)行這個流,這邊去寫一下昨天遇到的一個list轉(zhuǎn)map的場景。
list轉(zhuǎn)map在Java8中stream的應(yīng)用
常用方式
1.利用Collectors.toMap方法進行轉(zhuǎn)換

public Map<Long, String> getIdNameMap(List<Account> accounts) {
return accounts.stream().collect(Collectors.toMap(Account::getId, Account::getUsername));
}

其中第一個參數(shù)就是可以,第二個參數(shù)就是value的值。

2.收集對象實體本身

  • 在開發(fā)過程中我們也需要有時候?qū)ψ约旱膌ist中的實體按照其中的一個字段進行分組(比如 id ->List),這時候要設(shè)置map的value值是實體本身。

public Map<Long, Account> getIdAccountMap(List<Account> accounts) {
return accounts.stream().collect(Collectors.toMap(Account::getId, account -> account));
}

account -> account是一個返回本身的lambda表達式,其實還可以使用Function接口中的一個默認方法 Function.identity(),這個方法返回自身對象,更加簡潔

重復(fù)key的情況。
在list轉(zhuǎn)為map時,作為key的值有可能重復(fù),這時候流的處理會拋出個異常: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));
}

用groupingBy 或者 partitioningBy進行分組
根據(jù)一個字段或者屬性分組也可以直接用groupingBy方法,很方便。
Map<Integer, List<Person>> personGroups = Stream.generate(new PersonSupplier()).
limit(100).
collect(Collectors.groupingBy(Person::getAge));
Iterator it = personGroups.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Integer, List<Person>> persons = (Map.Entry) it.next();
System.out.println("Age " + persons.getKey() + " = " + persons.getValue().size());
}

partitioningBy可以理解為特殊的groupingBy,key值為true和false,當然此時方法中的參數(shù)為一個判斷語句(用于判斷的函數(shù)式接口)
Map<Boolean, List<Person>> children = Stream.generate(new PersonSupplier()).
limit(100).
collect(Collectors.partitioningBy(p -> p.getAge() < 18));
System.out.println("Children number: " + children.get(true).size());
System.out.println("Adult number: " + children.get(false).size());

關(guān)于stream使用的好文推薦:
這里去看了ibm的一篇關(guān)于stream的文章,get到了不少stream還沒遇到過的用法。老鐵們可以去學(xué)習(xí)一下。[https://www.ibm.com/developerworks/cn/java/j-lo-java8streamapi/ ]

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容