本代码想要达到的效果
测试完整代码如下(copy直接运行):
public class TestConvert {标签:Java,HashMap,nodeMap,代码,name,行列,item,new,String From: https://www.cnblogs.com/Williamwen/p/18542594
public static void main(String[] args) {
ArrayList<Attribute> sourceList = new ArrayList<>();
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
sourceList.add(new Attribute("name"+i, "value"+j, "code"+i, "data"+j));
}
}
ArrayList<HashMap<String, String>> maps = new ArrayList<>();
HashMap<String, Map<String,String>> temp = new HashMap<>();
sourceList.forEach(item->{
String name = item.getName();
String code = item.getCode();
String value = item.getValue();
String data = item.getData();
if(!temp.containsKey(name)){
HashMap<String, String> nodeMap = new HashMap<>();
nodeMap.put("cellName",name);
nodeMap.put("cellCode",code);
nodeMap.put(data,value);
temp.put(name,nodeMap);
maps.add(nodeMap);
}else {
HashMap<String, String> map = (HashMap<String, String>) temp.get(name);
map.put(data,value);
}
});
System.out.println(maps);
}
}
@Data
@AllArgsConstructor
@NoArgsConstructor
class Attribute{
private String name;
private String value;
private String code;
private String data;
}