The most concise thing (though perhaps not the most efficient) would
be to dump their constituent JsonElements into ordinary Java Sets.
Then you can use the normal Set API (e.g. contains, equals) to get the
information you want.
CompareArrays.java
====================
import java.util.*;
import com.google.gson.*;
public class CompareArray {
static Set<JsonElement> setOfElements(JsonArray arr) {
Set<JsonElement> set = new HashSet<JsonElement>();
for (JsonElement j: arr) {
set.add(j);
}
return set;
}
public static void main(String[] args) {
JsonParser parser = new JsonParser();
Set<JsonElement> arr1elems =
setOfElements(parser.parse(args[0]).getAsJsonArray());
Set<JsonElement> arr2elems =
setOfElements(parser.parse(args[1]).getAsJsonArray());
System.out.println("Arrays match? " + arr1elems.equals(arr2elems));
}
}
====================
$ java -cp .:gson-2.2.2.jar CompareArray '[{"key1":"value1"},
{"key2":"value2"}, {"key3":"value3"}]' '[{"key3":"value3"},
{"key1":"value1"}, {"key2":"value2"}]'
Arrays match? true