Java 8 如何快速的将List<T>转换成List<Map<String,Object>>
实际开发过程中,经常会遇到需要将List<T>转换List<Map<String,Object>>的情况,那么你们都是用什么方法实现的呢?
下面是我开发过程中使用的方法,还望大佬看后轻喷。
List<Map<String,Object>> monitorVoMapList = Optional.ofNullable(monitorVoLists).orElseGet(ArrayList::new) .stream().map(monitorVO -> { Map<String, Object> monitorVoMap = BeanCopyUtil.beanToMap(monitorVO); List<Map<String, Object>> sensorDataList = sensorService .listMaps(new LambdaQueryWrapper<SensorDetail>() .select(SensorDetail::getDataInfo, SensorDetail::getSensorType) .eq(SensorDetail::getStationSn, monitorVO.getStationSn())); sensorDataList.forEach(sensorData -> { JSONObject data = new JSONObject(); JSONObject json = JSON.parseObject(sensorData.get("data_info") + ""); json.keySet().forEach(key -> data.put(key, json.getJSONObject(key).getString("value"))); monitorVoMap.put(sensorData.get("sensor_type") + "", data); }); return monitorVoMap; }).collect(Collectors.toList())
public static <T> Map<String, Object> beanToMap(T bean) { Map<String, Object> map = Maps.newHashMap(); if (bean != null) { BeanMap beanMap = BeanMap.create(bean); for (Object key : beanMap.keySet()) { map.put(key + "", beanMap.get(key)); } } return map; }
上面的方法没有数据结构看起来可能比较吃力,下面就将数据贴出来
monitorVoLists数据格式
[ { "stationSn": "0223990045", "stationName": "0223990045", "deptId": "0158fabc60ed48a8ac8de8f6021e722f", "deptName": "武汉分公司", "stationType": "闸坝", "status": 0, "location": { "address": "广东省广州市白云区白云山曹家坝", "lng": 113.28562286878172, "lat": 23.173928972719555 } }, { "stationSn": "0223990044", "stationName": "0223990044", "deptId": "0158fabc60ed48a8ac8de8f6021e722f", "deptName": "武汉分公司", "stationType": "闸坝2", "status": 1, "location": { "address": "广东省广州市白云区白云山曹家坝2", "lng": 123.28562286878172, "lat": 24.173928972719555 } } ]
sensorDataLists数据格式
[ { "data_info": { "03H_C": { "time": "2022-08-23 10:55:00", "value": "" }, "27H_Q": { "time": "2022-08-23 10:55:00", "value": "" }, "39H_Z": { "time": "2022-08-23 10:55:00", "value": "" }, "30H_QA": { "time": "2022-08-23 10:55:00", "value": "16302.000" }, "37H_VJ": { "time": "2022-08-23 10:55:00", "value": "" }, "FF08H_NAQ1": { "time": "2022-08-23 10:55:00", "value": "0.000" }, "FF10H_PAQ1": { "time": "2022-08-23 10:55:00", "value": "16302.000" } }, "sensor_type": "H1688V2X" }, { "data_info": { "39H_Z": { "time": "2022-08-17 15:49:00", "value": "1.021" } }, "sensor_type": "H1600" } ]
最终处理后返回数据格式
[ { "H1600": { "39H_Z": "1.021" }, "deptName": "武汉分公司", "H1688V2X": { "39H_Z": "", "03H_C": "", "30H_QA": "16302.000", "FF10H_PAQ1": "16302.000", "FF08H_NAQ1": "0.000", "27H_Q": "", "37H_VJ": "" }, "stationType": "闸坝", "deptId": "0158fabc60ed48a8ac8de8f6021e722f", "location": { "address": "广东省广州市白云区白云山曹振宇家", "lng": 113.28562286878172, "lat": 23.173928972719555 }, "stationName": "0223990045", "stationSn": "0223990045", "status": 0 }, { "stationSn": "0223990044", "stationName": "0223990044", "deptId": "0158fabc60ed48a8ac8de8f6021e722f", "deptName": "武汉分公司", "stationType": "闸坝2", "status": 1, "location": { "address": "广东省广州市白云区白云山曹振宇家2", "lng": 123.28562286878172, "lat": 24.173928972719555 } } ]