trante
trante

Reputation: 34006

Comparing two JSONArray and finding differences

(This question needs for the equality)
But I have two JSONArray and I will check the strings that exists in these arrays and find the different elements.

Normally I can see the values of the arrays like this:

JSONArray array1 = getArray1();
JSONArray array2 = getArray2();

// array1 = 12,23,44,66
// array2 = 23,44,66,90

for (int i=0; i < array1.length(); i++) {
   String name=array1.getString(i);
}

I need to find elements that exists in array1 but doesn't exist in array2. Array elements are strings.

Upvotes: 2

Views: 1567

Answers (1)

Blackbelt
Blackbelt

Reputation: 157457

Create two different Set starting from the two array, then call

firstSet.removeAll(secondSet);

the result will be the difference between the two arrays.

the doc for removeAll says:

Removes from this set all of its elements that are contained in the specified collection

Upvotes: 4

Related Questions