需求:两个Json对比 目前有两个json 类型的数据,{"1":"A","2":"B","3":"C","4":"D"} 和 {"1":"X","2":"Y","3":"Z","5":"Q"}
* 对比出来的结构需要有{"1":{"old":"A","new":"X"},"2":{"old":"B","new":"Y"},"3":{"old":"C","new":"Z"},"4":{"old":"D","new":""},"5":{"old":"","new":"Q"}}
class BO{
/** 行号 **/
private String ROWNO;
/** 名称 **/
private String NAME;
public BO(){
}
public BO(String ROWNO, String NAME) {
this.ROWNO = ROWNO;
this.NAME = NAME;
}
public String getROWNO() {
return ROWNO;
}
public void setROWNO(String ROWNO) {
this.ROWNO = ROWNO;
}
public String getNAME() {
return NAME;
}
public void setNAME(String NAME) {
this.NAME = NAME;
}
}
public class Test {
public void test(){
List<BO> oldList = new ArrayList<>();
BO bo1 = new BO("1","A");
BO bo2 = new BO("2","B");
BO bo3 = new BO("3","C");
BO bo4 = new BO("4","D");
oldList.add(bo1);
oldList.add(bo2);
oldList.add(bo3);
oldList.add(bo4);
List<BO> newList = new ArrayList<>();
BO b1 = new BO("1","X");
BO b2 = new BO("2","Y");
BO b3 = new BO("3","Z");
BO b4 = new BO("5","Q");
newList.add(b1);
newList.add(b2);
newList.add(b3);
newList.add(b4);
List<JSONObject> sonList = new ArrayList();
JSONObject jsonobject = new JSONObject();
Map<String,BO> map = new HashMap<String,BO>();
for(BO b : oldList){
map.put(b.getROWNO(),b);
}
List<BO> intersection = new ArrayList<BO> ();
String ROWNO;
for(BO b: newList) {
ROWNO = b.getROWNO();
if (map.containsKey(ROWNO)){
intersection.add(map.get(ROWNO));
}
}
// 这个时候能确定的是intersection 中的数据可以删掉,剩下的就是比old 多 的
List<BO> copyNewList = new ArrayList<>();
copyNewList.addAll(newList);
List<BO> copyOldList = new ArrayList<>();
copyOldList.addAll(oldList);
// 删除元素
List<BO> removeList = new ArrayList<>();
copyNewList.stream().forEach(vo ->{
for (int j = 0; j < intersection.size(); j++) {
if(vo.getROWNO().equals(intersection.get(j).getROWNO())){
removeList.add(vo);
}
}
});
copyNewList.removeAll(removeList);
for(BO bo:copyNewList){
jsonobject = new JSONObject();
String newName = bo.getNAME();
jsonobject.put("rowNo",bo.getROWNO());
jsonobject.put("oldName","");
jsonobject.put("newName",newName);
sonList.add(jsonobject);
}
// 删除元素
List<BO> tempList = new ArrayList<>();
copyOldList.stream().forEach(vo ->{
for (int j = 0; j < intersection.size(); j++) {
if(vo.getROWNO().equals(intersection.get(j).getROWNO())){
tempList.add(vo);
}
}
});
copyOldList.removeAll(tempList);
System.out.println(copyOldList.size());
for(BO bo:copyOldList){
jsonobject = new JSONObject();
String oldName = bo.getNAME();
jsonobject.put("rowNo",bo.getROWNO());
jsonobject.put("oldName",oldName);
jsonobject.put("newName","");
sonList.add(jsonobject);
}
// 两边相同行号的对比
for (String key : map.keySet()) {
for (int i = 0; i < newList.size(); i++) {
if(key.equals(newList.get(i).getROWNO())){
jsonobject = new JSONObject();
String oldName = map.get(key).getNAME();
String newName = newList.get(i).getNAME();
jsonobject.put("rowNo",key);
jsonobject.put("oldName",oldName);
jsonobject.put("newName",newName);
sonList.add(jsonobject);
}
}
}
// 按照rowNo 排序
Comparator<JSONObject> comparing = comparing((JSONObject a) -> a.getString("rowNo"));
sonList.sort(comparing((JSONObject a) -> a.getString("rowNo")));
System.out.println(sonList);
}
public static void main(String[] args) {
Test t = new Test();
t.test();
}
}