
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get Minimum Value Using Comparator in Java
First, declare an integer array and add some elements −
Integer arr[] = { 40, 20, 30, 10, 90, 60, 700 };
Now, convert the above array to List −
List list = Arrays.asList(arr);
Now, use Comparator and the reverseOrder() method. Using max() will eventually give you the maximum value, but with reverseOrder() it reverses the result −
Comparator comp = Collections.reverseOrder(); System.out.println("Minimum element = "+Collections.max(list, comp));
Example
import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; public class Demo { @SuppressWarnings("unchecked") public static void main(String args[]) { Integer arr[] = { 40, 20, 30, 10, 90, 60, 700 }; List list = Arrays.asList(arr); Comparator comp = Collections.reverseOrder(); System.out.println("Minimum element = "+Collections.max(list, comp)); } }
Output
Minimum element = 10
Advertisements