
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java Program to Compare Elements in a Collection
In this article, we will understand how to compare elements in a collection. The Collection is a framework that provides architecture to store and manipulate the group of objects. Java Collections can achieve all the operations that you perform on a data such as searching, sorting, insertion, manipulation, and deletion.
Below is a demonstration of the same −
Suppose our input is −
Input list: [300, 500, 180, 450, 610]
The desired output would be −
Min value of our list : 180 Max value of our list : 610
Algorithm
Step 1 - START Step 2 - Declare a list namely input_list Step 3 - Define the values. Step 4 - Using the function = Collections.min() and Collections.max(), we fetch the min and max values of the collection. Step 5 - Display the result Step 6 - Stop
Example 1
Here, we bind all the operations together under the ‘main’ function.
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
Example 2
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
- Related Articles
- Java Program to Shuffle the Elements of a Collection
- Java Program to append all elements of other Collection to ArrayList
- Rotate elements of a collection in Java
- Add elements to LinkedHashMap collection in Java
- Java Program to Print a Collection
- Java Program to Reverse a Collection
- Golang program to compare elements in two slices
- Python Program to compare elements in two dictionaries
- C# Program to return a collection with repeated elements
- Append all elements of another Collection to a Vector in Java
- Java Program to Compare Strings
- Retrieving Elements from Collection in Java- Iterator
- Retrieving Elements from Collection in Java- ListIterator
- Retrieving Elements from Collection in Java- EnumerationIterator
- How to retain elements from a Collection in another Collection

Advertisements