# Lambda ## list -> map #### account list -> account map. map<id,account> ```java= public Map<Long, Account> getIdAccountMap(List<Account> accounts) { return accounts.stream().collect(Collectors.toMap(Account::getId, account -> account)); } public Map<Long, Account> getIdAccountMap(List<Account> accounts) { return accounts.stream().collect(Collectors.toMap(Account::getId, Function.identity())); } ``` 因為list有順序,但是map沒有,若要有順序,則可轉成 LinkedHashMap ```java= Map<Long, RestorationScheme> restorationSchemeMap = queryRestorationScheme() .stream() .sorted(Comparator.comparing(RestorationScheme::getId, Comparator.naturalOrder())) .collect(Collectors.toMap(RestorationScheme::getId, Function.identity(), (e1, e2) -> e1, LinkedHashMap::new)); ``` 計算list中重複的string次數 ```java= Map<String, Long> countMap = strings.stream() .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); ``` Pair<K,V> 雙重key值 找value ```java= Map<Pair<Integer, Integer>, ActTemp> actTempMapByCompanyAndProductCode = actTempService.selectByEntity(searchDTO) .stream() .collect(Collectors.toMap(actTemp -> new Pair(actTemp.getCompanyCode(), actTemp.getProductCode()), Function.identity())); ``` reference: https://www.cnblogs.com/xujanus/p/6133865.html