0
  • I can able to fetch json response , added response in arraylist

  • When i try to compare 2 arraylist , arraylist2 value is not changing.

  • what i have done :

json parsing :

for (int i = 0; i < array.length(); i++) {
                    JSONObject obj = array.getJSONObject(i);
                    id = obj.getString("alert_id");


                    map.put("alert_id", id);

                    Log.d("map", map.toString());
                    arraylist1.add(map);

                       Log.e("asdasd", arraylist1.toString());
                }

- Comparision :

for (int i = 0; i < arraylist.size(); i++) {
                for (int j = 0; j < arraylist1.size(); j++) {
                    if (!(arraylist.get(i).get("id")).equals((arraylist1.get(j).get("id")))) {
                        //Checking whether the last value in the list..
                        if (j == (arraylist1.size() - 1)) {
                            unCommonList.add(arraylist.get(i).get("alert_id"));


                        } else {

                        }
                    }
                }
            }

-- Output :

92 79
91 79
86 79
85 79
84 79
81 79
80 79

-- Everytime arraylist2 remains same, arraylist1 is changing, but arraylist2 value is not

-- This is my arraylist2 :

[{abcd=100, id=79, createdate=2016-03-14 03:59:43, defge=2012310, 123123xyzw=alpha}, {abcd=11230, id=79, createdate=2016-03-14 03:59:43, defge=201231230, xyzw=alpasdha}, {abcd=10452430, id=79, createdate=2016-03-14 03:59:43, defge=12346, xyzw=alasdpha}, {abcd=1za00, id=79, createdate=2016-03-14 03:59:43, defge=212312300, xyzw=alpdsaha}, {abcd=112312300, id=79, createdate=2016-03-14 03:59:43, defge=21231200, xyzw=alpsqwha}, {abcd=123123, id=79, createdate=2016-03-14 03:59:43, defge=201231230, xyzw=alpqwreha}, {abcd=112312300, id=79, createdate=2016-03-14 03:59:43, defge=212312300, xyzw=alpyjha}, {abcd=101231230, id=79, createdate=2016-03-14 03:59:43, defge=200, xyzw=alpha}]
6
  • unCommonList.add(arraylist.get(j).get("alert_id")); Commented Mar 18, 2016 at 11:09
  • You forgot what you want compare ? asc /desc? Commented Mar 18, 2016 at 11:09
  • @Ajay Pandya , can u show demo ? plz Commented Mar 18, 2016 at 11:11
  • @Nirav Ranpara , 2nd arraylist value is not changing. Commented Mar 18, 2016 at 11:12
  • First i want to know what you want to achieve ? Commented Mar 18, 2016 at 11:12

2 Answers 2

1
for (int i = 0; i < arraylist.size(); i++) {
            for (int j = 0; j < arraylist1.size(); j++) {
                if (!(arraylist.get(i).get("id")).equals((arraylist1.get(j).get("id")))) {
                    //Checking whether the last value in the list..
                    if (j == (arraylist1.size() - 1)) {
                        unCommonList.add(arraylist.get(i).get("alert_id"));


                    } else {

                    }
                }else{
                           break;
                              }
            }
        }
Sign up to request clarification or add additional context in comments.

Comments

1

Ok so for getting unmatch element you can try this code

ArrayList<Integer> results = new ArrayList<>();


for (Person person2 : arrayList2) {

    boolean found = false;
    for (Person person1 : arrayList1) {
    if (person2.id == person1.id) {
        found = true;
    }
}
if (!found) {
    results.add(person2.id);
}
}

7 Comments

what if my arraylist contains other elements also ?
Same way found=true only if require elements found
Have you try it with my code? because found boolean is vital for adding element.
have u seen my arraylist Log, every id is same
and what about this person2.id ? what should i mention here ?
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.