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;
Answer from PotatoManager on Stack Overflow
🌐
BeginnersBook
beginnersbook.com › 2013 › 12 › how-to-compare-two-arraylist-in-java
How to compare two ArrayList in Java
In this example we have two ArrayList al1 and al2 of String type. We have compared these ArrayLists using contains() method and stored the comparison result in third ArrayList (al3 and al4). package beginnersbook.com; import java.util.ArrayList; public class Details { public static void main(String [] args) { ArrayList<String> al1= new ArrayList<String>(); al1.add("hi"); al1.add("How are you"); al1.add("Good Morning"); al1.add("bye"); al1.add("Good night"); ArrayList<String> al2= new ArrayList<String>(); al2.add("Howdy"); al2.add("Good Evening"); al2.add("bye"); al2.add("Good night"); //Storing the comparison output in ArrayList<String> ArrayList<String> al3= new ArrayList<String>(); for (String temp : al1) al3.add(al2.contains(temp) ?
Discussions

comparing elements of two arrayList in java - Stack Overflow
i got two String type arraylist ..one list containing “book1”, “book2”, “book3” and “book4”. And another arrayList contains “book1”, “book2”, “book3”. So, size of first list is 4 and second is 3. A... More on stackoverflow.com
🌐 stackoverflow.com
Comparing two arrays in java with for loop - Stack Overflow
I am having difficulties with this program. I have to compare the two arrays by reading the arrays from the console and after the user enters them, print a statement for whether or not they are tru... More on stackoverflow.com
🌐 stackoverflow.com
Comparing two Arraylists
If you would like to compare the items of two lists. You can create a loop and then you can compare each item step by step. If you would like to compare two lists independently from each other. You can use Java Comparator class with sort function. You can define code logic which has sorting rules. With this way, you can compare the fields of class whatever you want. I hope this is your expected answer ? More on reddit.com
🌐 r/javahelp
5
3
January 25, 2018
java - Compare elements from an ArrayList - Stack Overflow
I have a small problem, I want to go through a list and compare two objects of the array. Each object has 3 elements, I use a StringTokenizer to be able to remove the separator, so each object has 3 More on stackoverflow.com
🌐 stackoverflow.com
🌐
DevQA
devqa.io › java-compare-two-arraylists
Java Compare Two Lists
March 17, 2020 - The List interface in Java provides methods to be able to compare two Lists and find the common and missing items from the lists.
Top answer
1 of 2
6

Only iterate on first arraylist with larger length and check for contains in second arraylist , if found set one else do nothing

for(int counter = 0; counter < firstList.size(); counter++) {
    if(secondList.contains(firstList.get(counter))) {
          comparingList.set(counter,1);
      }
  }

Whole java program

Just try to run the below program in http://www.compileonline.com/compile_java_online.php

import java.util.ArrayList;
import java.util.List;

public class CompareArrayListTest{

 public static void main(String[] args) {

    ArrayList<String> firstList = new ArrayList<String>();

    firstList.add("book1");
    firstList.add("book2");
    firstList.add("book3");
    firstList.add("book4");

    ArrayList<String> secondList = new ArrayList<String>();

    secondList.add("book1");
    secondList.add("book2");
    secondList.add("book3");

    List<Integer> comparingList = new ArrayList<Integer>();
    // adding default values as one
    for (int a = 0; a < firstList.size(); a++) {
        comparingList.add(0);

    }

    for (int counter = 0; counter < firstList.size(); counter++) {
        if (secondList.contains(firstList.get(counter))) {
            comparingList.set(counter, 1);
        }
    }

    System.out.println(comparingList);

}

BitSet bitset = new BitSet();
// adding default values as one
for (int a = 0; a < firstList.size(); a++) {
    comparingList.add(0);

}

for (int counter = 0; counter < firstList.size(); counter++) {
    for (int counter2 = 0; counter < secondList.size(); counter++) {
        if (secondList.get(counter2).equals(firstList.get(counter))) {
            bitset.set(counter, 1);
        }
    }
}
2 of 2
2

You can transform the lists to sets, and then use Set.retainAll method for intersection between the different sets. Once you intersect all sets, you are left with the common elements, and you can transform the resulting set back to a list.

🌐
How to do in Java
howtodoinjava.com › home › collections framework › java arraylist › compare two lists in java: equality, differences, common items
Compare Two Lists in Java: Equality, Differences, Common Items
1 week ago - Compare two lists in Java with equals() or ignoring order, count duplicates correctly, and find additional, missing and common items with removeAll().
Find elsewhere
🌐
Javatpoint
javatpoint.com › how-to-compare-two-arraylist-in-java
How to Compare Two ArrayList in Java - Javatpoint
How to Compare Two ArrayList in Java with oops, string, exceptions, multithreading, collections, jdbc, rmi, fundamentals, programs, swing, javafx, io streams, networking, sockets, classes, objects etc,
🌐
GeeksforGeeks
geeksforgeeks.org › java › comparing-two-arraylist-in-java
Comparing two ArrayList In Java - GeeksforGeeks
January 23, 2026 - In Java, an ArrayList is used to store an ordered collection of elements. To compare two ArrayList objects, Java provides the equals() method.
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 322668 › comparing-elements-in-arraylist
java - Comparing elements in arraylist [SOLVED] | DaniWeb
November 4, 2010 - Just to let you know, theres not just two types of cards, there are 52 cards in a deck, so 52 different types. But N number of cards will be compared. At one time, there could be N different cards on top. It's quite complicated and has got me pulling my hair out :P ... How about looping thru the top cards (no need for the ArrayList) and building a HashTable<Card, Integer> showing the number of occurrences of each card, ie for each top card: if hashtable's keys contain this card, increment its counter else add a new entry (card, 1) Then you know exactly how many of each card you've got.
Top answer
1 of 1
1

This only answers part of your question. I could not understand the question completely, please edit it and I can edit my answer.

You should not remove items from a list whilst in a for loop, therefore you can, for example, create another boolean list with the same size divided by 3 and just fill it with true Booleans then set the position divided by 3 to false if you want to delete the three items. Then you can create a new list, iterate over the boolean list and add 3 "Objects" which are actually Strings (thanks @JB Nizet) at a time, every time the boolean list element is true. When it is false you just don't add the elements and by doing so you are practically deleting the two elements before that element together with that element.

You casted a String to an int, that does not work you have to parse the Strings.

I corrected some of your code and added the boolean list here:

    ArrayList<String> lisM = new ArrayList<>();  // here I initialise the list as an array list with strings. 

    ArrayList<Boolean> booleanList = new ArrayList<>();
    for (int i = 0; i < lisM.size() / 3; i++) {
        booleanList.add(true);
    }

    for(int i = 3; i < lisM.size();i+=3) {
        int m = Integer.parseInt(lisM.get(i)); // here I changed the casting to parsing and moved it out of the for loop, there is no need to initialize it again every single time since you do not change it in the second for loop.
        for (int j = 6; j < lisM.size(); j += 6) {
            int m1 = Integer.parseInt(lisM.get(j));// here I changed the casting to parsing again.
            if (m > m1) { // this makes no sense here because you are going over all of the elements of the list and comparing them to all of them. But I kept it here for the sake of example.
                booleanList.set(i/3,false); 
            }

// if you want to go over the whole list you will have to clear the list and start over again for every element. } } and here is how you could create the new list without the elements you do not want:

ArrayList<String> newLisM = new ArrayList<>();

for (int i = 0; i <booleanList.size(); i++) {
        if(booleanList.get(i))
            for (int j = 0; j < 3; j++) {
                newLisM.add(lisM.get(i+j));
            }
    }
🌐
Tutorjoes
tutorjoes.in › Java_example_programs › compare_two_array_list_in_java
Write a Java program to compare two array lists
The program creates a third ArrayList of Strings, named ... System.out.println. import java.util.*; public class Compare_TwoArrayList { public static void main(String[] args) { ArrayList<String> list_str1= new ArrayList<String>(); list_str1.add("Computer"); list_str1.add("Keyboard"); list_str1.add("Mouse"); list_str1.add("CPU"); list_str1.add("Printer"); list_str1.add("Derive"); ArrayList<String> list_str2 = new ArrayList<String>(); list_str2.add("Printer"); list_str2.add("Mouse"); list_str2.add("Keyboard"); System.out.println("String 1 :" + list_str1); System.out.println("String 2 :" + list_str2); ArrayList<String> list_str3 = new ArrayList<String>(); for (String e : list_str1) list_str3.add(list_str2.contains(e) ?
🌐
Medium
medium.com › @AlexanderObregon › finding-matching-items-in-two-java-arrays-43751484730f
Finding Matching Items in Two Java Arrays | Medium
July 17, 2025 - The structure is basic, but the behavior is predictable and easy to follow, and for smaller arrays, it holds up just fine. Here’s the core idea. You take one array and compare every element in it to every element in the second array.
🌐
Coderanch
coderanch.com › t › 673893 › java › Comparing-ArrayLists-objects-shared-properties
Comparing two ArrayLists containing different objects with (some) shared properties (Beginning Java forum at Coderanch)
December 15, 2016 - In short, what I'd like to code ... current solution I have in mind is to simply iterate through the two ArrayLists at the same time (after sorting them), comparing the shared properties, and if they match up, continue the loop....
🌐
Reddit
reddit.com › r/learnprogramming › a question about the comparison of elements in two arraylists in java.
r/learnprogramming on Reddit: A question about the comparison of elements in two ArrayLists in java.
April 21, 2023 -

I'm trying to do my homework

I created two ArraLists of Integer, and the content in the list is as follows:

List1:

5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 145 150

List2:

150 300 450 600 750

when i use

list1. get(list1. size() - 1) == list2. get(0);

I get false;

I have to use

list1.get(list1.size() - 1).equals(list2.get(0));

to get true;

Then I try the following code:

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

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

arrList1. add(10);

arrList2.add(10);

System.out.println(arrList1.get(0) == arrList2.get(0));

I get true;

I know that == can only be used to compare values, or Primitive types, while .equals() is used to compare references.

I'm sure what I'm storing in the ArrayList is integer, int, so why can't I use == for comparison? (And I'm also wondering why comparing 10 with == gets true and 150 gets false)

I wonder why this is happening?

Thanks in advance to everyone who helped me.

🌐
Medium
medium.com › @QA-initi › how-to-compare-two-array-list-in-java-using-comparator-cc05617175b5
How to compare two array list in Java using Comparator | by QA-init | Medium
November 14, 2023 - In order to compare two ArrayList in Java using a comparator, we can use the Collections.sort() method and pass in the ArrayList and a…