0

I have two ArrayList with JSONObject in it, I need to compare both and find distinct items from it, so far here is my code some reason output what I receive is incorrect.

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"}
2
  • so what's the question here?
    – Ousmane D.
    Dec 16, 2017 at 15:51
  • I meant to say is that the output is not correct, I am looking for distinct JSONObject from list2 after comparing both Dec 16, 2017 at 15:55

1 Answer 1

0

If you are looking to get the distinct JSONObjects from both lists using the java streaming API, you can concatenate the streams from both lists into a single stream, then use the distinct method on it. Try this:

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);


Stream<JSONObject> jsonStream = Stream.concat(list1.stream(), list2.stream);

List<JSONObject> listcom= jsonStream.distinct().collect(Collectors.toList());

System.out.println(listcom);

Edit:

If you are trying to retrieve one value from list1 if it corresponds to a value in list2 you have to map to that value. Try this:

List<JSONObject> listcom=list2.stream().filter(json-> list1.contains(json.get("id"))).map(json-> list1.get(json.get("id"))).distinct().collect(Collectors.toList()); 
3
  • Dean, for distinct that works, however what I am looking for is,only an items that is not in list2, basically I need to compare only the id part not the content part. : such that {"BMX":"some random data","id":"2DDX"} Dec 16, 2017 at 16:16
  • in that case you have to map to the result in list one:
    – Dean
    Dec 16, 2017 at 16:18
  • First of all when I filter list2 with list1.contain(json.get("id") it returns a empty array[]. Also the second part of map is returning a compiler error Type mismatch: cannot convert from Stream<Object> to <unknown> Dec 16, 2017 at 17:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.