Java program to compare elements in a collection



In Java, the Collection framework provides an architecture to store and manipulate a group of objects. Collections support operations such as searching, sorting, insertion, manipulation, and deletion.

In this article, we will learn how to compare elements in a collection in Java using different approaches. Before learning about the methods, let us understand the problem with the help of the following input-output scenarios:

Scenario 1

Input:
["Java", "Python", "JavaScript", "C++"]
Element to Compare: "Java"

Output:
Java is equal to Java
Python is not equal to Java
JavaScript is not equal to Java
C++ is not equal to Java

Scenario 2

Input:
["Apple", "Banana", "Mango", "Orange"]
Element to Compare: "Mango"

Output:
Apple is not equal to Mango
Banana is not equal to Mango
Mango is equal to Mango
Orange is not equal to Mango

The following are the approaches to compare elements in a collection in Java:

Comparing Elements in a Collection Using equals() Method

The equals() method is used to compare two objects for equality. It returns true if the objects are equal, otherwise false. We can use this method to compare elements in a collection.

Example

In the following example, we compare each element of a collection of strings with a given string using the equals() method:

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

public class CompareElements {
   public static void main(String[] args) {
      List<String> list = new ArrayList<>();
      list.add("Java");
      list.add("Python");
      list.add("JavaScript");
      list.add("C++");

      String elementToCompare = "Java";

      for (String element : list) {
         if (element.equals(elementToCompare)) {
            System.out.println(element + " is equal to " + elementToCompare);
         } else {
            System.out.println(element + " is not equal to " + elementToCompare);
         }
      }
   }
}

Output

Java is equal to Java
Python is not equal to Java
JavaScript is not equal to Java
C++ is not equal to Java

Comparing Elements in a Collection Using Comparator

The Comparator interface in Java is used to compare two objects. It provides the compare() method, which compares two objects and returns an integer value. We can use this interface to compare elements in a collection.

Syntax:

int compare(T o1, T o2);

Example

In the following example, we create a custom comparator to compare elements in a collection and sort them based on the comparison:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class CompareElementsUsingComparator {
   public static void main(String[] args) {
      List<String> list = new ArrayList<>();
      list.add("Java");
      list.add("Python");
      list.add("JavaScript");
      list.add("C++");

      Collections.sort(list, new Comparator<String>() {
         @Override
         public int compare(String o1, String o2) {
            return o1.compareTo(o2);
         }
      });

      System.out.println("Sorted List: " + list);
   }
}

Output

Sorted List: [C++, Java, JavaScript, Python]
Aishwarya Naglot
Aishwarya Naglot

Writing clean code… when the bugs aren’t looking.

Updated on: 2025-08-26T17:18:53+05:30

797 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements