@@ -76,48 +76,44 @@ else if (node1 != null && node1.isArray() && node2 != null && node2.isArray()) {
76
76
List <JsonNode > list2 = new ArrayList <>();
77
77
node1 .forEach (list1 ::add );
78
78
node2 .forEach (list2 ::add );
79
+ list1 .sort (Comparator .comparing (JsonNode ::toString ));
80
+ list2 .sort (Comparator .comparing (JsonNode ::toString ));
79
81
int i = 0 ;
80
82
81
- // Identify removed items (exist in list1 but not in list2)
82
- for (JsonNode node : list1 ) {
83
- if (!containsNode (list2 , node )) {
84
- String newPath = path + "[" + i + "]" ;
85
- JsonNode childNode = compareJsonNodes (node , null , newPath , nodeKey + "[" + i + "]" );
86
- children .add (childNode );
87
- i ++;
88
- }
89
- }
90
-
91
- // Identify added items (exist in list2 but not in list1)
92
- for (JsonNode node : list2 ) {
93
- if (!containsNode (list1 , node )) {
94
- String newPath = path + "[" + i + "]" ;
95
- JsonNode childNode = compareJsonNodes (null , node , newPath , nodeKey + "[" + i + "]" );
96
- children .add (childNode );
97
- i ++;
98
- }
99
- }
83
+ List <JsonNode > list1rem = new ArrayList <>();
84
+ List <JsonNode > list2rem = new ArrayList <>();
100
85
101
86
// Identify changed items (same structure but different values)
102
87
for (JsonNode nodeNo1 : list1 ) {
103
88
for (JsonNode nodeNo2 : list2 ) {
104
- if (isSimilar ( nodeNo1 , nodeNo2 ) && ! nodeNo1 . equals ( nodeNo2 )) {
89
+ if (nodeNo1 . equals ( nodeNo2 ) || isSimilar ( nodeNo1 , nodeNo2 )){
105
90
String newPath = path + "[" + i + "]" ;
106
91
JsonNode childNode = compareJsonNodes (nodeNo1 , nodeNo2 , newPath , nodeKey + "[" + i + "]" );
107
92
children .add (childNode );
108
93
i ++;
94
+ // Remove the matched node from list2 and list1 to avoid duplicate comparisons
95
+ list1rem .add (nodeNo1 );
96
+ list2rem .add (nodeNo2 );
109
97
}
110
98
}
111
99
}
112
- // Identify unchanged items (same structure and same values)
113
- for (JsonNode nodeNo1 : list1 ) {
114
- for (JsonNode nodeNo2 : list2 ) {
115
- if (nodeNo1 .equals (nodeNo2 )) {
116
- String newPath = path + "[" + i + "]" ;
117
- JsonNode childNode = compareJsonNodes (nodeNo1 , nodeNo2 , newPath , nodeKey + "[" + i + "]" );
118
- children .add (childNode );
119
- i ++;
120
- }
100
+
101
+ // Remove matched nodes from the original lists
102
+ list1 .removeAll (list1rem );
103
+ list2 .removeAll (list2rem );
104
+
105
+ for (JsonNode node : list1 ) {
106
+ // Identify removed items (exist in list1 but not in list2)
107
+ if (!containsNode (list2 , node )) {
108
+ String newPath = path + "[" + i + "]" ;
109
+ JsonNode childNode = compareJsonNodes (node , null , newPath , nodeKey + "[" + i + "]" );
110
+ children .add (childNode );
111
+ i ++;
112
+ } else {// Identify added items (exist in list2 but not in list1)
113
+ String newPath = path + "[" + i + "]" ;
114
+ JsonNode childNode = compareJsonNodes (null , node , newPath , nodeKey + "[" + i + "]" );
115
+ children .add (childNode );
116
+ i ++;
121
117
}
122
118
}
123
119
}
0 commit comments