81,094
社区成员
发帖
与我相关
我的任务
分享
List<WarnConfig> conflist = warnConfigService.getByIccid();
Map<String , List<String>> listMap = new HashMap<>();
Map<String , String> imeiMap = new HashMap<>();
conflist.forEach(conf ->{
String owner = conf.getUsername(); //得到用户名
List<ForecastDevice> forecastList = forecastDeviceService.getByOwner(owner , null);
forecastList.forEach(fst -> {
imeiList.add(fst.getImei());
imeiMap.put(fst.getImei(),owner);
});
List<WeatherDevice> weaList = weatherDeviceService.getByOwner(owner, null);
weaList.forEach(wea ->{
imeiList.add(wea.getImei());
imeiMap.put(wea.getImei(),owner);
});
List<MoistureDevice> mosList = moistureDeviceService.getByOwner(owner, null);
mosList.forEach(mos ->{
imeiList.add(mos.getImei());
imeiMap.put(mos.getImei(),owner);
});
List<LampLastItemVo> lamplist = lampServiceClient.getByOwner(owner);//用户下的杀虫灯设备
lamplist.forEach(lamp ->{
imeiList.add(lamp.getSn());
imeiMap.put(lamp.getSn(),owner);
});
});
public static void appendMap(List<?> list, String owner, List<String> imeiList , Map<String, String> imeiMap) {
for (Object o : list) {
String k = null;
if (o instanceof XXX) { //根据你自己的类型修改XXX,YYY
k = ((XXX)o).getImei();
} else if (o instanceof YYY) {
k = ((YYY)o).getSn();
}
if (k != null) {
imeiList.add(k);
imeiMap.put(k, owner);
}
}
}
// 调用
List<String> imeiList = new ArrayList<>();
Map<String , String> imeiMap = new HashMap<>();
conflist.forEach(conf ->{
String owner = conf.getUsername(); //得到用户名
List<ForecastDevice> forecastList = forecastDeviceService.getByOwner(owner , null);
appendMap(forecastList, owner, imeiList, imeiMap);
List<WeatherDevice> weaList = weatherDeviceService.getByOwner(owner, null);
appendMap(weaList, owner, imeiList, imeiMap);
....
});
public class OperatorDemo {
public static void main(String[] args) {
List<WarnConfig> conflist = new ArrayList<>();
Map<String, List<String>> listMap = new HashMap<>();
Map<String, String> imeiMap = new HashMap<>();
ArrayList<String> imeiList = new ArrayList<>();
conflist.forEach(conf -> {
String owner = conf.getUsername(); //得到用户名
List<ForecastDevice> forecastList = new ArrayList<>();
List<WeatherDevice> weaList = new ArrayList<>();
List<MoistureDevice> mosList = new ArrayList<>();
List<LampLastItemVo> lamplist = new ArrayList<>();//用户下的杀虫灯设备
operation(forecastList, imeiList, imeiMap, ForecastDevice::getImei, owner);
operation(weaList, imeiList, imeiMap, WeatherDevice::getImei, owner);
operation(mosList, imeiList, imeiMap, MoistureDevice::getImei, owner);
operation(lamplist, imeiList, imeiMap, LampLastItemVo::getSn, owner);
});
}
public static <T> void operation(List<T> operationList, List<String> resultList, Map<String, String> resultMap, Function<T, String> mapper, String owner) {
List<String> list = operationList.stream().map(mapper).collect(Collectors.toList());
resultList.addAll(list);
Map<String, String> map = list.stream().collect(Collectors.toMap(Function.identity(), s -> owner));
resultMap.putAll(map);
}
}
***Device 实现统一 接口 ParentDevice
ParentDevice 中 : 统一接口: doSomething :每个 实现重现,里面放你需要的代码
public void add(List<ParentDevice> list){
list.forEach(********);
}
这样行嘛