Compare Elements in a Collection in Java



In this article, we will understand how to compare elements in a collection. In Java, Collection is a framework that provides an architecture to store and manipulate a group of objects. Java Collections can achieve all the operations that you perform on data such as searching, sorting, insertion, manipulation, and deletion.

Problem Statement

Write a program in Java to compare elements in a collection. Below is a demonstration of the same ?

Input

Input list: [300, 500, 180, 450, 610]

Output

Min value of our list : 180
Max value of our list : 610

Different approaches

Following are the different approaches to comparing elements in a collection ?

Using main() method

Following are the steps to compare elements in a collection using the main() method ? 

  • Import the necessary Java utility packages.
  • Define the main() method to contain all operations.
  • Create a list (input_list) of integers and add specific values to it.
  • Print the input_list to show its contents.
  • Use Collections.min() to find the minimum value in input_list and store it in a variable minimum_value.
  • Use Collections.max() to find the maximum value in input_list and store it in a variable maximum_value.
  • Check if minimum_value is equal to maximum_value. If true, print a message that all elements in the list are equal.
  • If the values are not equal, print the minimum and maximum values of the list.

Example

Here, we bind all the operations together under the main() method ?

import java.util.*;
public class Demo {
   public static void main(String[] args){
      List<Integer> input_list = new ArrayList<>();
      input_list.add(300);
      input_list.add(500);
      input_list.add(180);
      input_list.add(450);
      input_list.add(610);
      System.out.println("The list is defined as: " +input_list);
      int minimum_value = Collections.min(input_list);
      int maximum_value = Collections.max(input_list);
      if (minimum_value == maximum_value) {
         System.out.println("All the elements of the list are equal");
      }
      else {
         System.out.println("\nMin value of our list : " + minimum_value);
         System.out.println("Max value of our list : " + maximum_value);
      }
   }
}

Output

The list is defined as: [300, 500, 180, 450, 610]

Min value of our list : 180
Max value of our list : 610

Using encapsulation

Following are the steps to compare elements in a collection using encapsulation ?

  • Import the necessary Java utility packages.
  • Define a class method min_max() that takes a list (input_list) as a parameter.
  • Inside min_max(), use Collections.min() to find the minimum value and store it in minimum_value.
  • Use Collections.max() to find the maximum value and store it in maximum_value.
  • Check if minimum_value is equal to maximum_value. If true, print a message indicating all elements in the list are equal.
  • If the values are not equal, print the minimum and maximum values of the list.
  • In the main() method, create and initialize input_list with values.
  • Print input_list to show its contents.
  • Call the min_max() method and pass input_list as an argument to perform the comparison.

Example

Here, we encapsulate the operations into functions exhibiting object-oriented programming ?

import java.util.*;
public class Demo {
   static void min_max(List<Integer> input_list){
      int minimum_value = Collections.min(input_list);
      int maximum_value = Collections.max(input_list);
      if (minimum_value == maximum_value) {
         System.out.println("All the elements of the list are equal");
      }
      else {
         System.out.println("\nMin value of our list : " + minimum_value);
         System.out.println("Max value of our list : " + maximum_value);
      }
   }
   public static void main(String[] args){
      List<Integer> input_list = new ArrayList<>();
      input_list.add(300);
      input_list.add(500);
      input_list.add(180);
      input_list.add(450);
      input_list.add(610);
      System.out.println("The list is defined as: " +input_list);
      min_max(input_list);
   }
}

Output

The list is defined as: [300, 500, 180, 450, 610]

Min value of our list : 180
Max value of our list : 610
Updated on: 2024-11-11T19:17:28+05:30

734 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements