BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

How to compare two ArrayList in Java

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

In this tutorial we will learn how to compare two ArrayList. We would be using contains() method for comparing two elements of different ArrayList.

public boolean contains(Object o)
It returns true if the list contains the Object o else it returns false.

Example:

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) ? "Yes" : "No");
          System.out.println(al3);

          //Storing the comparison output in ArrayList<Integer>
          ArrayList<Integer> al4= new ArrayList<Integer>();
          for (String temp2 : al1)
               al4.add(al2.contains(temp2) ? 1 : 0);
          System.out.println(al4);
     }
}

Output:

[No, No, No, Yes, Yes]
[0, 0, 0, 1, 1]

What is the logic in above code?
If the first element of ArrayList al1 is present in al2 then ArrayList al3 would be having “Yes” and al4 would be having “1” However if the element is not present “No” would be stored in al3 and 0 would be in al4.

Top Related Articles:

  1. Java ArrayList addAll(int index, Collection c) Method example
  2. How to synchronize ArrayList in java with example
  3. How to find length of ArrayList in Java
  4. How to Convert an array to ArrayList in java
  5. Copy Elements of One ArrayList to Another ArrayList in Java

Tags: Collections, Java-ArrayList

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. Minhazur says

    August 24, 2015 at 6:49 PM

    Hello sir,
    What is temp here??

    Reply
    • manoj kumar says

      September 21, 2015 at 11:58 AM

      its only a reference variable that is using for the printing of arraylist content.

      Reply
  2. Rajeev says

    August 31, 2015 at 5:02 PM

    Hi Chaithanya,

    can you explain these following code..Actually I didn’t understand what you are trying to explain..
    ArrayList al3= new ArrayList();
    for (String temp : al1)
    al3.add(al2.contains(temp) ? “Yes” : “No”);
    System.out.println(al3);

    Thanks in Advance

    Reply
    • Arvind says

      April 11, 2016 at 5:38 AM

      Thats the enhanced for loop introduced in Java 5.0.

      Reply
    • Shilpa says

      April 11, 2016 at 11:56 AM

      A new arraylist is initialized as al3.
      The al1 ArrayList is stored in temp using the for each loop.
      Then the content of temp is compared with the 2nd ArrayList al2,
      If present YES else NO. This is done by using ternary condition.
      That Yes or No is now added to the al3 and finally output is displayed.

      Reply
      • Sam says

        May 15, 2016 at 2:32 AM

        How can we use Question mark and colon between yes and no without using inverted commas?
        Secondly error comes when i remove question mark.

        Reply
    • NAVEEN SAI says

      January 12, 2017 at 11:55 AM

      ArrayList al3= new ArrayList();
      for (String temp : al1)
      al3.add(al2.contains(temp) ? “Yes” : “No”);
      System.out.println(al3);
      >>>>> this is advanced for loop. so after iterating it stores the values in temp and later we are comparing arraylist al2 and temp(reference variable)..i.e we are comparing al2 &temp

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap