【发布时间】:2018-05-30 13:12:24
【问题描述】:
我有两个带有 JSONObject 的 ArrayList,我需要比较两者并从中找到不同的项目,到目前为止,这是我的代码,由于某些原因输出我收到的内容不正确。
public static void main(String args[]){
JSONObject obj1= new JSONObject();
obj1.put("id", "1DDX");
obj1.put("crx", "some random string");
JSONObject obj3= new JSONObject();
obj3.put("id", "2DDX");
obj3.put("BMX", "some random data");
JSONObject obj2= new JSONObject();
obj2.put("id", "1DDX");
obj2.put("crx", "some more random string");
List<JSONObject> list1= new ArrayList<JSONObject>();
list1.add(obj1);
list1.add(obj3);
List<JSONObject> list2= new ArrayList<JSONObject>();
list2.add(obj2);
list2.add(obj2);
List<JSONObject> listcom=list2.stream().filter(json-> list1.contains(json.get("id"))).collect(Collectors.toList());
System.out.println(listcom);
The output for equal and not equal comparison
[]
The output for
List<JSONObject> listcom=list2.stream().filter(!json-> list1.contains(json.get("id"))).collect(Collectors.toList());
[{"crx":"some more random string","id":"1DDX"}, {"crx":"some more random string","id":"1DDX"}]
The output what I am looking for is
{"BMX":"some random data","id":"2DDX"}
【问题讨论】:
-
那么这里的问题是什么?
-
我的意思是输出不正确,我在比较两者后从list2中寻找不同的JSONObject