Java對List使用stream流進行操作


1:java8之前List分組:

假設有個student類,有id、name、score屬性,list集合中存放所有學生信息,現在要根據學生姓名進行分組。

public Map<String, List<Student>> groupList(List<Student> students) {
    Map<String, List<Student>> map = new Hash<>();
    for (Student student : students) {
        List<Student> tmpList = map.get(student.getName());
        if (tmpList == null) {
            tmpList = new ArrayList<>();
            tmpList.add(student);
            map.put(student.getName(), tmpList);
        } else {
            tmpList.add(student);
        }
    }
    return map;
}

2:其他的一些stream流轉換

Java8之后常規分組

public Map<String, List<Student>> groupList(List<Student> students) {
    Map<String, List<Student>> map = students.stream().collect(Collectors.groupingBy(Student::getName));
    return map;
}

 

1:第一種: 取list中某2個字段作為Map的K,V

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

2:第二種:將id和實體Bean做為K,V

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

或者這樣寫:

public Map<Long, Account> getIdAccountMap(List<Account> accounts) {
    return accounts.stream().collect(Collectors.toMap(Account::getId, Function.identity()));
}
account -> account是一個返回本身的lambda表達式,后面的使用Function接口中的一個默認方法代替,使整個方法更簡潔優雅。

3:使用某個具體的Map類來保存,如保存時使用LinkedHashMap

public Map<String, Account> getNameAccountMap(List<Account> accounts) {
    return accounts.stream().collect(Collectors.toMap(Account::getUsername, Function.identity(), (key1, key2) -> key2, LinkedHashMap::new));
}

4:List<Object>轉List<String,Map<String, String>>

 1 public Map<String,List<MCode>> getCodeListMap(){
 2         if(CollectionUtils.isEmpty(codeListMap)){
 3                 List<MCode> codeList = this.getCodeList();
 4                 Set<String> keySet = codeList.stream().map(code -> code.getCodeKbn()).collect(Collectors.toSet());
 5                 Iterator<String> it = keySet.iterator();
 6                 while(it.hasNext()) {
 7                         String key = it.next();
 8                         codeListMap.put(key, codeList.stream().filter(code -> code.getCodeKbn().equals(key)).collect(Collectors.toList()));
 9                 }
10         }
11         return codeListMap;
12 }

5:轉成value為Set的Map

1 Map<String, Set<Long>> onlineTradeNoMap = tradeOrderGoodsRelateOnlineList.stream()
2                 .filter(tradeOrderGoodsRelateOnline -> tradeOrderGoodsRelateOnline.getSourceTradeNo() != null)
3                 .collect(Collectors.groupingBy(TradeOrderGoodsRelateOnline::getSourceTradeNo,
4                         Collectors.mapping(TradeOrderGoodsRelateOnline::getTradeId, Collectors.toSet())));

6:獲取list對象 list屬性 並進行去重

List<String> collect = users.stream().map(e -> e.getUserName()).distinct().collect(Collectors.toList());

7:單獨獲取List對象某個屬性操作

List<String> collect = users.stream().map(e -> e.getUserName()).collect(Collectors.toList());

8:單獨去重操作

List<String> distinctElements = list.stream().distinct().collect(Collectors.toList());

 9: 在stream流轉Map的過程中做數據類型轉換

例如 Student 對象的id為 integer類型, 但我想輸出Map<Long, String> 類型的數據

 

Map<Long, String> map = list..stream().collect(Collectors.toMap(student -> Long.valueOf(student.getId()), Student::getName));
 

 

各個地方整理來的,侵刪

 

.stream().collect(Collectors.toMap(tradePromGroupingDto -> Long.valueOf(tradePromGroupingDto.getId()), Tra


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM