目录
笔者在实际生产中,遇到很多 需要操作list ,构成 固定的数据格式
复杂一
我需要
"list": [
{
"sonlist": [
{
"sontype": "3-3",
"url": "11111"
}
],
"type": "3"
}
]
但是我的数据库 里面存放的是
CREATE TABLE `t_apply_image` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID ',
`user_id` bigint(20) NOT NULL COMMENT '用户ID ',
`apply_id` bigint(20) NOT NULL COMMENT '申请流水号 ',
`image_type` varchar(60) NOT NULL COMMENT '图片类型 31-1,31-2 32-1
`image_url` varchar(100) NOT NULL COMMENT '图片路径',
`oss_image_url` varchar(255) DEFAULT NULL COMMENT '图片url',
PRIMARY KEY (`id`),
KEY `t_apply_image_index` (`apply_id`,`image_type`)
) ;
处理
public Map<String, Object> getImageList(UserRequest common, UserInfoCache userInfo) {
Map<String, String> tempMap = new HashMap<String, String>();
tempMap.put("id", common.getApplyId());
List<TApplyImage> list=getListBySqlId( tempMap, new TApplyImage());
Map<String ,List<Map<String,String>>> map=new HashMap<>();
for(TApplyImage item:list){
String type=item.getImageType();
String [] strings=type.split("-");
if(map.containsKey(strings[0])){
List<Map<String,String>> currmetlist=map.get(strings[0]);
Map<String,String> itemmap=new HashMap<>();
itemmap.put("sontype",item.getImageType());
itemmap.put("url",item.getOssImageUrl());
currmetlist.add(itemmap);
}else{
List<Map<String,String>> currmetlist=new ArrayList<>();
Map<String,String> itemmap=new HashMap<>();
itemmap.put("sontype",item.getImageType());
itemmap.put("url",item.getOssImageUrl());
currmetlist.add(itemmap);
map.put(strings[0],currmetlist);
}
}
Iterator< Map.Entry< String, List<Map<String,String>>>> it = map.entrySet().iterator();
List<Map<String,Object>> resultlist=new ArrayList<>();
while (it.hasNext()) {
Map<String,Object> resumap=new HashMap<>();
Map.Entry<String, List<Map<String,String>>> entry = it.next();
resumap.put("type",entry.getKey());
resumap.put("sonlist",entry.getValue());
resultlist.add(resumap);
}
resultMap.put("list",resultlist);
return resultMap;
}
同样是这个存放,但是数据格式要求
{
"fileList": [
{
"urlList": [
{ "url": "https://jufa.jpeg;https://jufan-i8A4E769.jpeg",
"uniqueIds": "15318"}
],
"fileType": "33"
}
]
}
处理
private List<MiddleImageVo> getFileListSupplement(List<TApplyImage> imgLst) {
List<TApplyImage> tApplyImages=imgLst.parallelStream().filter(d->{
return !Strings.isNullOrEmpty(d.getOssImageUrl())&&!Strings.isNullOrEmpty(d.getImageType());
}).sorted(Comparator.comparing(TApplyImage::getImageType)).collect(Collectors.toList());
List<MiddleImageVo> resultList=new ArrayList<>();
for (TApplyImage item : tApplyImages) {
String riskType = tNotifyService.convertImgType(item.getImageType());
if(resultList.isEmpty()){
//初始化进入时候
MiddleImageVo vo=ModelConvertUtil.builderNewVo(riskType,item);
resultList.add(vo);
}else{
//判断之前有没有 这个type
List<MiddleImageVo> centrList=resultList.parallelStream().filter(d->{
return d.getFileType().equals(riskType);
}).collect(Collectors.toList());
if(centrList.isEmpty()){
// 没有则新加入
MiddleImageVo vo=ModelConvertUtil.builderNewVo(riskType,item);
resultList.add(vo);
}else{
//如果有那么就直接 在后面做叠加
MiddleImageVo middleImageVo= centrList.get(0);
MinImageVo oldminImageVo= middleImageVo.getUrlList().get(0);
String starturl=oldminImageVo.getUrl(); //拿出以前的url
MiddleImageVo newVo= centrList.get(0);
MinImageVo newminImageVo= newVo.getUrlList().get(0);
String endurl=starturl+";"+item.getOssImageUrl(); //拼接出现在的url
newminImageVo.setUrl(endurl);
resultList.remove(middleImageVo);
resultList.add(newVo);
}
}
}
return resultList;
}