1

I am trying to create a way to compare two arraylist buttonresourceIds & buttonresourceIds2 . I want to compare them to find out whether they share button elements in common. Then return the "number" of elements they share.

ArrayList<String>> buttonresourceIds = new ArrayList<String>>();
ArrayList<String>> buttonresourceIds2 = new ArrayList<String>>();

for(int i=0;i<buttonresourceIds.size();i++){

    for(int j=0;j<buttonresourceIds2.size();j++){
        if(buttonresourceIds.contains(buttonresourceIds2.get(j))){
            System.out.println("Exist : "+buttonresourceIds2.get(j));
        }else{
            System.out.println("Not Exist : "+buttonresourceIds2.get(j));
        }
    }
}

It gives me the result but the loops keeps repeating. Am I missing something?

James Z
  • 12,209
  • 10
  • 24
  • 44
syndy1989
  • 403
  • 10
  • 25
  • 1
    How about using [`List::retainAll`](http://docs.oracle.com/javase/8/docs/api/java/util/List.html#retainAll-java.util.Collection-)? – Flown Jul 17 '17 at 12:09
  • This code doesn't do anything. Your error is somewhere else. – Christopher Schneider Jul 17 '17 at 12:10
  • Since you are calling `buttonresourceIds.contains(buttonresourceIds2.get(j))` what's that outer loop with `i` for? That's probably your problem: you're checking the list 2 against list 1 _for every element in list 1_. Also note that your code doesn't compile as it is (e.g. `ArrayList>` is not valid) and the hashmap code you posted before looks strange as well. – Thomas Jul 17 '17 at 12:10
  • @Tom No, it's not a duplicate of that. _It gives me the result but the **loops keeps repeating.**_ – Christopher Schneider Jul 17 '17 at 12:10
  • @ChristopherSchneider And the point in fixing bad code instead of using the proper method in the first place is where? It obviously is a dupe (because that question tells OP what he/she wants to know). – Tom Jul 17 '17 at 12:12
  • I let you look to all answers, I've proposed a 2-line one ;) – azro Jul 17 '17 at 12:25

4 Answers4

2

You don't need two loops to do this, One loop is sufficient to fix it

ArrayList<String>> buttonresourceIds = new ArrayList<String>>();
ArrayList<String>> buttonresourceIds2 = new ArrayList<String>>();
int common=0;
for(int j=0;j<buttonresourceIds2.size();j++){
    if(buttonresourceIds.contains(buttonresourceIds2.get(j))){
        System.out.println("Exist : "+buttonresourceIds2.get(j));
        common++;
    }else{
        System.out.println("Not Exist : "+buttonresourceIds2.get(j));
    }
}
return common;
Joey Pinto
  • 1,735
  • 1
  • 18
  • 34
1

See Below Code :

ArrayList<String>> buttonresourceIds = new ArrayList<String>>();
ArrayList<String>> buttonresourceIds2 = new ArrayList<String>>();


for(int i=0;i<buttonresourceIds.size();i++){
if(buttonresourceIds2.contains(buttonresourceIds.get(i)))
{
     System.out.println("Exist : "+buttonresourceIds.get(i));
}
else{
      System.out.println("Not Exist : "+buttonresourceIds.get(i));
    }

 }

In Above code, only one Loop is required.

Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
1

First there is some mistakes into your code like :

ArrayList<String>> --> ArrayList<String>

As the other answers said : one loop is usefull BUT calling get() 2 times is cost more than 1, so you may use the foreach feature down there :

for (String button : buttonresourceIds2) {
     if (buttonresourceIds.contains(button)) {
         System.out.println("Exist : " + button);
         elementInCommon++;
     } else {
         System.out.println("Not Exist : " + button);
     }
}

After that you want to count the elements in common, so you mat add a variable which increment itself if contains


And an easier way in 2 lines would be :

buttonresourceIds.retainAll(buttonresourceIds2);
int elementInCommom = buttonresourceIds.size();
azro
  • 53,056
  • 7
  • 34
  • 70
-1
public static void main(String[] args) {
        ArrayList<String> buttonresourceIds = new ArrayList<String>();
        ArrayList<String> buttonresourceIds2 = new ArrayList<String>();
        buttonresourceIds.add("1");
        buttonresourceIds.add("2");
        buttonresourceIds.add("3");

        buttonresourceIds2.add("1");
        buttonresourceIds2.add("2");
        buttonresourceIds2.add("4");


            for(int i=0;i<buttonresourceIds.size();i++){

                for(int j=i;j<buttonresourceIds2.size();j++){
                if(buttonresourceIds.contains(buttonresourceIds2.get(j))){
                    System.out.println("Exist : "+buttonresourceIds2.get(j));
                }else{
                    System.out.println("Not Exist : "+buttonresourceIds2.get(j));
                }
                break;
            }
            }
    }