Java 8:根据某些字段过滤2个不同的JSON数组

时间:2022-05-19 18:28:48

This may be silly but I couldn't figure it out, I have 2 different schema JSON arrays.

这可能很愚蠢,但我无法弄清楚,我有2个不同的架构JSON数组。

JSON array A

JSON数组A.

[
  {
     "name": "Robin",
     "uid": 1234   
  },  
  {
     "name": "Tom",
     "uid": 8768   
  },   
  {
     "name": "Eddy",
     "uid": 4534   
  }
]

JSON array B

JSON数组B.

[
  {
     "group": "Reign",
     "admin": 8768   
  },  
  {
     "group": "Hounds",
     "admin": 1234   
  },   
  {
     "group": "Dukes",
     "admin": 5996   
  }
]

Essentially the values of uid in JSON array A and admin field in JSON array B are same. The lists are very large so iteration is very expensive.

本质上,JSON数组A中的uid和JSON数组B中的admin字段的值是相同的。列表非常大,因此迭代非常昂贵。

My task is to get the elements of Array A matches with elements of Array B according to these 2 fields (uid & admin). Also need to collect excess elements of Array A and excess elements of Array B.

我的任务是根据这两个字段(uid和admin)使数组A的元素与数组B的元素匹配。还需要收集数组A的多余元素和数组B的多余元素。

What I have done so far

到目前为止我做了什么

List<JsonObject> listOutput = jsonDataAList.stream()
                .filter(e -> jsonDataBList.stream()
                        .map(JsonDataA::getUid)
                        .anyMatch(admin -> admin.equals(e.getAdmin())))
                .collect(Collectors.toList());

Down voters please find time for any explanation

请选民下来,请抽空解释

Thank in advance.

预先感谢。

2 个解决方案

#1


1  

Assuming that uid is unique I would convert both Jsons to Map<Integer, String> and then use next answer

假设uid是唯一的,我会将两个Jsons转换为Map ,然后使用下一个答案 ,string>

You can use Guava's Maps.difference(Map<K, V> left, Map<K, V> right) method. It returns a MapDifference object, which has methods for getting all four kinds of map entries:

您可以使用Guava的Maps.difference(Map left,Map right)方法。它返回一个MapDifference对象,该对象具有获取所有四种映射条目的方法: ,v> ,v>

  • equally present in left and right map
  • 同样出现在左右地图中

  • only in left map
  • 只在左图中

  • only in right map
  • 只在右图中

  • key present in both maps, but with different values
  • 密钥存在于两个映射中,但具有不同的值

#2


0  

Solved by myself

由我自己解决

private List<JsonObject> getExcessElements(List<JsonObject> primaryList, List<JsonObject> secondaryList, String primaryKey, String secondaryKey) {
List<JsonObject> excessInPrimary = primaryList.stream()
.filter(e -> (secondaryList.stream()
.filter(d -> d.get(secondaryKey).equals(e.get(primaryKey)))
.count()) < 1)
.collect(Collectors.toList());
return excessInPrimary;
}

#1


1  

Assuming that uid is unique I would convert both Jsons to Map<Integer, String> and then use next answer

假设uid是唯一的,我会将两个Jsons转换为Map ,然后使用下一个答案 ,string>

You can use Guava's Maps.difference(Map<K, V> left, Map<K, V> right) method. It returns a MapDifference object, which has methods for getting all four kinds of map entries:

您可以使用Guava的Maps.difference(Map left,Map right)方法。它返回一个MapDifference对象,该对象具有获取所有四种映射条目的方法: ,v> ,v>

  • equally present in left and right map
  • 同样出现在左右地图中

  • only in left map
  • 只在左图中

  • only in right map
  • 只在右图中

  • key present in both maps, but with different values
  • 密钥存在于两个映射中,但具有不同的值

#2


0  

Solved by myself

由我自己解决

private List<JsonObject> getExcessElements(List<JsonObject> primaryList, List<JsonObject> secondaryList, String primaryKey, String secondaryKey) {
List<JsonObject> excessInPrimary = primaryList.stream()
.filter(e -> (secondaryList.stream()
.filter(d -> d.get(secondaryKey).equals(e.get(primaryKey)))
.count()) < 1)
.collect(Collectors.toList());
return excessInPrimary;
}