美文网首页
Lambda的使用

Lambda的使用

作者: lbcBoy | 来源:发表于2018-05-31 10:27 被阅读25次

    常用Lambda处理

    • List<UserDto> to Map<Long,UserDto>
    • Map<Long,UserDto> to List<UserDto>
    • Map<Long,UserDto> to List<Long>
    • List<UserDto> to List<Long>
    • List排序
    • filter
    • map修改dto的值
    • foreach
    • sort
    • list跟据某个字段去重
    • list to map(key&value多个字段,防止duplicate key)

    范例

    public class MyLambdaTest {
        public static void main(String[] args) {
    
            List<UserDto> userDtoList = new ArrayList<>();
            for (int i = 0; i < 10; i++) {
                UserDto userDto = new UserDto();
                userDto.setId((long) i);
                userDto.setUserName("name" + i);
                userDto.setAge(10 + i);
                userDtoList.add(userDto);
            }
    
            //List<UserDto>  to  Map<Long,UserDto>
            Map<Long, UserDto> userDtoMap = userDtoList.stream().collect(Collectors.toMap(UserDto::getId, Function.identity()));
    
            //Map<Long,UserDto>  to  List<UserDto>
            List<UserDto> map2List = new ArrayList<UserDto>(userDtoMap.values());
    
            //Map<Long,UserDto>  to  List<Long>
            List<Long> map2keys = new ArrayList<Long>(userDtoMap.keySet());
    
            //List<UserDto>  to  List<Long>
            List<Long> userIds = userDtoList.stream().map(UserDto::getId).collect(Collectors.toList());
    
            //list排序
            userDtoList.sort(Comparator.comparing(UserDto::getUserName)); //按名字排序
            userDtoList.sort((p1, p2) -> {
                return p1.getUserName().toLowerCase().compareTo(p2.getUserName().toLowerCase());
            });
    
            userDtoList.sort(Comparator.comparing(p -> p.getUserName().toLowerCase()));//lambda表达式
    
            //filter
            List<UserDto> filterResult = userDtoList.stream().filter(p -> StringUtils.equals(p.getUserName(), "name3")).collect(Collectors.toList());
    
            //map的使用
            //map函数的主要功能是对List中的每个元素进行处理并返回,可以返回其它的数据类型
            //1.修改dto的值;
            List<UserDto> userDtos = userDtoList.stream().map(userDto -> {
                userDto.setAge(100);
                return userDto;
            }).collect(Collectors.toList());
    
            //foreach
            userDtoList.stream().forEach(userDto -> {
                userDto.setAge(90);
            });
        }
    }
        //排序
        Comparator<WalletDto> comparator = (WalletDto d1, WalletDto d2) -> d1.getSortFlag().compareTo(d2.getSortFlag());
        Collections.sort(walletDtoList, comparator);
    
      //list去重
      unique = symbolContrasts.stream().collect(
                            Collectors.collectingAndThen(Collectors.toCollection(
                                    () -> new TreeSet<>(Comparator.comparing(o -> o.getExchange()))), ArrayList::new)
                    );
    
      //list to map(key&value多个字段,防止duplicate key)
    resMap = symbolContrasts.stream()
                            .distinct().collect(Collectors.toMap(k -> k.getPCoinType() + "_" + k.getPMoneyType(),
                                    k -> k.getPCoinType() + "_" + k.getPMoneyType(), (k, v) -> v));
    

    //list to map(key&value多个字段)
    Map<String, PositionDto> resMap = positionDtos.stream()
    .collect(Collectors.toMap(k -> k.getExchange() + "_" + k.getFbgSymbol(), pos->pos));

    相关文章

      网友评论

          本文标题:Lambda的使用

          本文链接:https://www.haomeiwen.com/subject/frqajftx.html