lambda表达式

本文介绍 Java Stream API 的多种实用技巧,包括如何将 List 转换为 Map、List,如何进行排序、过滤、去重操作,以及如何收集数据到特定的数据结构中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

stream方法API
list转map
  • list泛型对象的任意一属性进行分组,构成Map<属性,Object>
    Map<String,List<User>> map= userList.stream().collect(Collectors.groupingBy(User::getName));
    
  • list泛型对象的任意两个属性构成Map<属性1, 属性2>
    public Map<Long, String> getIdNameMap(List<Account> accounts) {
        return accounts.stream().collect(Collectors.toMap(Account::getId, 			Account::getUsername));
    }
    
  • list泛型对象的任意一属性为key,泛型对象为value构成Map<属性,Object>
    // 方式一:account -> account是一个返回自己本身的lambda表达式
    public Map<Long, Account> getIdAccountMap(List<Account> accounts) {
        return accounts.stream().collect(Collectors.toMap(Account::getId,account -> account));
        
    // 方式二:其实还可以使用Function接口中的一个默认方法代替,使整个方法更简洁优雅
    public Map<Long, Account> getIdAccountMap(List<Account> accounts) {
        return accounts.stream().collect(Collectors.toMap(Account::getId, Function.identity()));
    }
        
    // 方式是三:name重复的,方式二就会抛出异常(java.lang.IllegalStateException: Duplicate key),可使用重载方法传入一个合并相同key的函数来解决key冲突问题:
    public Map<String, Account> getNameAccountMap(List<Account> accounts) {
        return accounts.stream().collect(Collectors.toMap(Account::getUsername, Function.identity(), (key1, key2) -> key2));
    }
    
  • 指定返回Map的具体实现数据结构
    // 指定一个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));
    }
    
list转list
  • list泛型对象转为另一个对象的list
    List<SettlementInfoPO> settlementInfoPOS = timeoutData.stream().map(item -> {
        SettlementInfoPO settlementInfoPO = item.convertPo();
        return settlementInfoPO;
    }).collect(Collectors.toList());
    
  • list泛型对象的某个属性构成另一个list
    roomList.stream().map(Room::getAvgPrice).collect(Collectors.toList());
    
  • list泛型对象的某属性排序后构成另一个list
    // 升序
    roomList.stream().sorted(Comparator.comparing(Room::getAvgPrice)).collect(Collectors.toList());
    
    // 降序
    roomList.stream().sorted(Comparator.comparing(Room::getAvgPrice).reversed()).collect(Collectors.toList());
    
    list.sort((a, b) -> a.name.compareTo(b.name));
    
  • 去重,需要重写对象的equals和hashcode方法

    roomList.stream().distinct().collect(Collectors.toList());
    
  • list泛型对象的某些属性过滤后构成另一个list
    // 且
    roomList.stream().filter(benefit -> benefit.getId() == 1 && benefit.getAge() == 20).collect(Collectors.toList());
    
    // 或
    roomList.stream().filter(benefit -> benefit.getId() == 1 || benefit.getId() == 20).collect(Collectors.toList());
    
  • 返回list第一个元素
    // 返回第一个元素,没有返回null
    roomList.stream().findFirst().orElse(null);
    
list转String
  • 将list中的元素拼接成一个字符串,中间用T隔开
    roomList.stream().collect(Collectors.joining("T"));
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值