我如何在 Java 中比较两个 JSONArray?

我如何比较两个 JSONArray,如果这两个中的任何值匹配,函数必须返回true. 例如:


array1=={1,2,3,4,5,6} 

array2=={9,10,11,1,12}

当比较这两个数组时,结果是true因为两个数组之间至少有一个共同的元素。数组中的位置并不重要。


我试图通过一种原始的方式来比较它(遍历每个数组并比较值)。


private boolean compareArrays(JSONArray deviation, JSONArray deviationIds) throws JSONException {

for (int i = 0; i < deviation.length(); i++) {

for (int j = 0; j < deviationIds.length(); j++)

if(deviation.getString(i).equals(deviationIds.getString(j)))

    return true;

}

return false;

}


这段代码有效,但我想知道是否有办法让我更专业地做到这一点。也许,通过使用 JAVA Stream API 来实现。如果您需要任何其他信息,请发表评论。谢谢!!


繁星淼淼
浏览 273回答 3
3回答

HUH函数

如果要使用 JAVA Stream API,对象必须是 Iterable。正如@Michal 提到的,您可以将其更改为 Collection。从@Michal 的回答中,我们可以简化它,就像:public class JsonArrayIntersectTest {&nbsp; &nbsp; private boolean anyMatch(JSONArray first, JSONArray second) throws JSONException {&nbsp; &nbsp; &nbsp; &nbsp; Set<Object> firstAsSet = convertJSONArrayToSet(first);&nbsp; &nbsp; &nbsp; &nbsp; Set<Object> secondIdsAsSet = convertJSONArrayToSet(second);&nbsp; &nbsp; &nbsp; &nbsp; // use stream API anyMatch&nbsp; &nbsp; &nbsp; &nbsp; return firstAsSet.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .anyMatch(secondIdsAsSet::contains);&nbsp; &nbsp; }&nbsp; &nbsp; private Set<Object> convertJSONArrayToSet(JSONArray input) throws JSONException {&nbsp; &nbsp; &nbsp; &nbsp; Set<Object> retVal = new HashSet<>();&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < input.length(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; retVal.add(input.get(i));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return retVal;&nbsp; &nbsp; }&nbsp; &nbsp; @Test&nbsp; &nbsp; public void testCompareArrays() throws JSONException {&nbsp; &nbsp; &nbsp; &nbsp; JSONArray deviation = new JSONArray(ImmutableSet.of("1", "2", "3"));&nbsp; &nbsp; &nbsp; &nbsp; JSONArray deviationIds = new JSONArray(ImmutableSet.of("3", "4", "5"));&nbsp; &nbsp; &nbsp; &nbsp; Assert.assertTrue(anyMatch(deviation, deviationIds));&nbsp; &nbsp; &nbsp; &nbsp; deviation = new JSONArray(ImmutableSet.of("1", "2", "3"));&nbsp; &nbsp; &nbsp; &nbsp; deviationIds = new JSONArray(ImmutableSet.of("4", "5", "6"));&nbsp; &nbsp; &nbsp; &nbsp; Assert.assertFalse(anyMatch(deviation, deviationIds));&nbsp; &nbsp; }}

慕尼黑的夜晚无繁华

任何匹配返回 true 的要求都可以重新表述为非空交集 return true。将JSONArrayinto转换Collection为很容易做到这一点,例如像这样public class JsonArrayIntersectTest {private boolean anyMatch(JSONArray first, JSONArray second) throws JSONException {&nbsp; &nbsp; Set<Object> firstAsSet = convertJSONArrayToSet(first);&nbsp; &nbsp; Set<Object> secondIdsAsSet = convertJSONArrayToSet(second);&nbsp; &nbsp; //Set<Object> intersection = Sets.intersection(firstAsSet, secondIdsAsSet);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// use this if you have Guava&nbsp; &nbsp; Set<Object> intersection = firstAsSet.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .distinct()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(secondIdsAsSet::contains)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toSet());&nbsp; &nbsp; return !intersection.isEmpty();}Set<Object> convertJSONArrayToSet(JSONArray input) throws JSONException {&nbsp; &nbsp; Set<Object> retVal = new HashSet<>();&nbsp; &nbsp; for (int i = 0; i < input.length(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; retVal.add(input.get(i));&nbsp; &nbsp; }&nbsp; &nbsp; return retVal;}@Testpublic void testCompareArrays() throws JSONException {&nbsp; &nbsp; JSONArray deviation = new JSONArray(ImmutableSet.of("1", "2", "3"));&nbsp; &nbsp; JSONArray deviationIds = new JSONArray(ImmutableSet.of("3", "4", "5"));&nbsp; &nbsp; Assert.assertTrue(anyMatch(deviation, deviationIds));&nbsp; &nbsp; deviation = new JSONArray(ImmutableSet.of("1", "2", "3"));&nbsp; &nbsp; deviationIds = new JSONArray(ImmutableSet.of("4", "5", "6"));&nbsp; &nbsp; Assert.assertFalse(anyMatch(deviation, deviationIds));&nbsp; &nbsp; }}但是,与直接使用 API 的初始版本相比,代码要多得多JSONArray。它还会产生比直接 API 版本更多的迭代JSONArray,这可能是也可能不是问题。然而,总的来说,我认为在传输类型上实现领域逻辑并不是一个好的做法,例如JSONArray并且总是会转换成领域模型。另请注意,生产就绪代码还会null对参数进行检查。

明月笑刀无情

您可以使用 XStream 来处理 JSON 映射或者根据您检索字符串的逻辑,您可以比较为 JSONCompareResult result = JSONCompare.compareJSON(json1, json2, JSONCompareMode.STRICT);  System.out.println(result.toString());
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java