Open In App
Related Articles

Java Program to Compare Elements in a Collection

Improve Article
Save Article
Like Article

A Collection is a group of individual objects represented as a single unit. Java provides Collection Framework which defines several classes and interfaces to represent a group of objects as a single unit. 


Input : List = [3, 5, 18, 4, 6]
Min value of our list : 3
max value of our list : 18

Input : List = ['a', 'a', 'a']
All elements are equal


  1. Take inputs in the list.
  2. Create two variables, minimum and maximum.
  3. Use Collections.min() method and store the return value in min variable.
  4. Use Collections.max() method and store the return value in max variable.
  5. If the minimum and maximum variables are equal then print Equal elements.
  6. Else print minimum and maximum variables.

Below is the implementation of the above approach:


// Java Program to Compare Elements in a Collection
import java.util.*;
class GFG {
    public static void main(String[] args)
        // List initialization
        List<Integer> l = new ArrayList<>();
        // Add elements in the list
        // Minimum in the list
        int minimum = Collections.min(l);
        // Maximum in the list
        int maximum = Collections.max(l);
        if (minimum == maximum) {
            System.out.println("All elements are equal");
        else {
            System.out.println("Min value of our list : "
                               + minimum);
            System.out.println("Max value of our list : "
                               + maximum);


Min value of our list : 3
Max value of our list : 18

Time Complexity: O(N), where N is the length of the list.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 04 Jan, 2021
Like Article
Save Article
Similar Reads
Complete Tutorials