• 优雅是的使用Lambda .map函数


    1. 代码展示
    package com.jd.pda.gateway.spi.impl.service.stock;
    
    
    import com.alibaba.fastjson.JSON;
    import lombok.Data;
    
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.*;
    import java.util.stream.Collectors;
    
    public class Test {
        public static void main(String[] args) {
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //封装赋值
            LocationDto dto01 = new LocationDto();
            dto01.setUpdateBy("蝎子莱莱");
            dto01.setUpdateDate(dateFormat.format(new Date()));
            dto01.setVersion(4);
    
            LocationDto dto02 = new LocationDto();
            dto02.setUpdateBy("蜘蛛侦探");
            dto02.setUpdateDate(dateFormat.format(new Date()));
            dto02.setVersion(5);
    
            //放入至集合
            List<LocationDto> list = new ArrayList<>();
            Collections.addAll(list, dto01, dto02);
    
            //装载至Map集合, 机器人版本为Key, JSON串为Value
            Map<Integer, LocationVo> map = new HashMap<>();
    
            //调用方法转换
            convert2LocationVo(list).forEach(each -> map.put(each.getVersion(), each));
            //遍历
            map.forEach((key, value) -> System.out.println("key:" + key + "; value" + JSON.toJSON(value)));
            // TODO: key:4; value{"updateDate":"2021-06-25 15:39:25","updateBy":"蝎子莱莱","version":4}
            // TODO: key:5; value{"updateDate":"2021-06-25 15:39:25","updateBy":"蜘蛛侦探","version":5}
        }
    
        private static List<LocationVo> convert2LocationVo(List<LocationDto> locationDtoList) {
            if (null == locationDtoList) {
                return null;
            }
            return locationDtoList.stream().map(item -> {
                LocationVo locationVo = new LocationVo();
                locationVo.setUpdateBy(item.getUpdateBy());
                locationVo.setUpdateDate(item.getUpdateDate());
                locationVo.setVersion(item.getVersion());
                return locationVo;
            }).collect(Collectors.toList());
        }
    }
    
    
    @Data
    class LocationDto {
        private String UpdateBy;
        private String UpdateDate;
        private Integer Version;
    }
    
    @Data
    class LocationVo {
        private String UpdateBy;
        private String UpdateDate;
        private Integer Version;
    }
    
    
  • 相关阅读:
    jni java与c++交互返回三维数组jobjectArray
    Android开发之EditText 详解(addTextChangedListener监听用户输入状态)
    Android JNI和NDK学习(09)--JNI实例二 传递类对象
    JavaDoc的生成规则---ShinePans
    Android 电话自己主动接听和挂断具体解释
    传统线程技术(一)
    Linux内核启动及根文件系统载入过程
    eclipse、MyEclipse实现批量改动文件编码
    菜鸟开发—应具备的搜索技巧
    Java之GUI编程(一)
  • 原文地址:https://www.cnblogs.com/Twittery/p/14931329.html
Copyright © 2020-2023  润新知