Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
623 views
in Technique[技术] by (71.8m points)

Java Compare two Objects containing deeply nested lists, find deleted list items based on attribute on list A and update List B

Given two instances A and B of the same class T with the following sample structure:

Class T {
  Object C;
  Object D;
  List<E> listA;
  List<F> listB;
 ..... 
 }
 
 Where CLass E, Class F have also nested lists containing Objects etc. 
  • All Objects contained in a list have an attribute BigDecimal seqNum, that denotes the sequenceNumber of the item in the list.

I want to develop a method that compares the two instances A and B, as follows:

Compare all deeply nested lists of the two instances, find deleted list items from instance A and add them to the corresponding list in instance B with a negative seqNum

Example

*Instance A*     *Instance B*      *Updated Instance B*

    List E          List E           List E 
       1                1                1
       2                3                -2
       3                5                3
       4                                 -4
       5                                 5

    Where 1,2,3,4,5 the seqNums of the objects contained in the list.

What would be an optimal way of performing such a comparison / update in a generic way for all nested lists?

I have thought of transforming the initial objects to JSON Objects and do the comparison/update in that way, but I was wondering whether a more elegant solution exists.

UPDATE Here is my solution using JsonObjects:

public T intersectLists(T obj1, T obj2, Class<T> type){
    String obj1InString = new Gson().toJson(obj1);
    String obj2InString = new Gson().toJson(obj2);
    JsonParser parser = new JsonParser();
    JsonElement j1 = parser.parse(obj1InString);
    JsonElement j2 = parser.parse(obj2InString);
    compare(j1,j2);
    GsonBuilder gson = new GsonBuilder();
    return  gson.create().fromJson(j2, type);
}

private void compare(JsonElement j1, JsonElement j2) {

    if (j1.equals(j2)) return;

    if (j1.isJsonArray() && j2.isJsonArray()) {
        JsonArray arrJ = (JsonArray) j1;
        JsonArray arrO = (JsonArray) j2;

        int size = Math.min(arrJ.size(), arrO.size());
        for (int i = 0; i < size; i++) {
            compare(arrJ.get(i), arrO.get(i));
        }
        if (arrJ.size() > arrO.size()) {
            JsonArray deletedElements = new JsonArray();
            for (int i = 0; i < arrJ.size(); i++) {
                JsonObject jsonObject = arrJ.get(i).getAsJsonObject();
                String sequenceNumeric = jsonObject.get("sequenceNumeric").getAsString();
                int j=0;
                boolean elementFound = false;
                while( j < arrO.size()) {
                    JsonObject amendmentJsonObject = arrO.get(j).getAsJsonObject();
                    String amendSequenceNumeric = amendmentJsonObject.get("sequenceNumeric").getAsString();
                    if(amendSequenceNumeric.equals(sequenceNumeric)){
                        elementFound = true;
                        break;
                    }
                    j++;
                }
                if(!elementFound){
                    jsonObject.remove("sequenceNumeric");
                    jsonObject.addProperty("sequenceNumeric", -Integer.parseInt(sequenceNumeric));
                    deletedElements.add(jsonObject);
                }
            }
            arrO.addAll(deletedElements);
        }
    } else if (j1.isJsonObject() && j2.isJsonObject()) {
        JsonObject objJ = (JsonObject) j1;
        JsonObject objO = (JsonObject) j2;

        for (Map.Entry<String, JsonElement> entry : objJ.entrySet()) {
            String key = entry.getKey();
            JsonElement value = entry.getValue();
            if (objO.has(key)) {
                compare(value, objO.get(key));
            }
        }
    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...