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