
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
Java Integer Compare Method
Integer compare() Method
The compare() method in the Integer class is a part of the Java interface and is used to compare two integers. It provides a way to compare two Integer objects or primitive int values and determine their relative ordering. This method is particularly useful when sorting or working with collections that involve integer values.
Syntax
public static int compare(int x, int y);Where ?
- x: The first integer to be compared.
- y: The second integer to be compared.
Return Value
The compare() method returns an integer value that represents the comparison result ?
-
Negative value (less than 0): If x is less than y.
-
Zero (0): If x is equal to y.
- Positive value (greater than 0): If x is greater than y.
Comparing Two Integer Values
The compare() method can be used to compare two integer values easily. It returns a value that indicates whether the first integer is less than, equal to, or greater than the second integer. This is particularly useful when you need to perform comparisons for sorting or ordering operations.
At first, declare int values to be compared ?
int val1 = 200; int val2 = 250; int val3 = 200;
Now, compare the values ?
System.out.println(Integer.compare(val1, val2)); System.out.println(Integer.compare(val1, val3));
Example
Below is an example to implement the compare() method in Java ?
public class Main { public static void main(String[] args) { int val1 = 200; int val2 = 250; int val3 = 200; System.out.println(Integer.compare(val1, val2)); System.out.println(Integer.compare(val1, val3)); } }
Output
-1 0
Using compare() with Collections (Sorting Integers)
In addition to direct comparisons, the compare() method is highly effective in sorting operations. We can use it with Java collections, such as lists, to sort integer values based on their natural order. This can be done efficiently using the Collections.sort() method in combination with Integer.compare().
Sorting using compare() in a custom comparator ?
Collections.sort(numbers, (a, b) -> Integer.compare(a, b));
Example
Below is an example to implement the compare() method in Java using collections?
import java.util.*; public class IntegerCompareSortingExample { public static void main(String[] args) { List<Integer> numbers = Arrays.asList(50, 20, 30, 10, 40); Collections.sort(numbers, (a, b) -> Integer.compare(a, b)); System.out.println("Sorted List: " + numbers); } }
Output
Sorted List: [10, 20, 30, 40, 50]
Conclusion
The Integer.compare() method is a simple and effective way to compare integers in Java. It is especially useful in sorting and when working with collections. The method provides clear results: a negative value if the first integer is smaller, zero if they are equal, and a positive value if the first integer is greater.