1. 程式人生 > >Java8中list轉map方法總結

Java8中list轉map方法總結

背景

  • 在最近的工作開發之中,慢慢習慣了很多Java8中的Stream的用法,很方便而且也可以並行的去執行這個流,這邊去寫一下昨天遇到的一個list轉map的場景。

list轉map在Java8中stream的應用

常用方式

1.利用Collectors.toMap方法進行轉換

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

其中第一個引數就是可以,第二個引數就是value的值。

2.收集物件實體本身
- 在開發過程中我們也需要有時候對自己的list中的實體按照其中的一個欄位進行分組(比如 id ->List),這時候要設定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(),這個方法返回自身物件,更加簡潔

  1. 重複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)); }
  1. 用groupingBy 或者 partitioningBy進行分組
    • 根據一個欄位或者屬性分組也可以直接用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,當然此時方法中的引數為一個判斷語句(用於判斷的函式式介面)
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());

關於stream使用的好文推薦: