🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The compareTo(E o) method in Java is used to compare the ordinal value of one enum constant to another.
Table of Contents
- Introduction
compareTo(E o)Method Syntax- Understanding
compareTo(E o) - Examples
- Basic Usage
- Sorting Enums
- Real-World Use Case
- Conclusion
Introduction
Enums in Java are a special data type that represents a group of constants. Each enum constant has an ordinal value, which is its position in the enum declaration, starting from 0. The compareTo(E o) method compares the ordinal values of two enum constants.
compareTo(E o) Method Syntax
The syntax for the compareTo(E o) method is as follows:
public final int compareTo(E o)
Parameters:
o: The enum constant to be compared.
Returns:
- A negative integer, zero, or a positive integer if the ordinal of this enum constant is less than, equal to, or greater than the ordinal of the specified enum constant.
Understanding compareTo(E o)
The compareTo(E o) method is defined in the java.lang.Enum class, which all enums implicitly extend. This method allows enums to be compared based on their ordinal values, which can be useful for sorting and other comparisons.
Examples
Basic Usage
To demonstrate the basic usage of compareTo(E o), we will create a simple example comparing two enum constants.
Example
public enum Day {
SUNDAY,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY
}
public class EnumCompareToExample {
public static void main(String[] args) {
Day day1 = Day.MONDAY;
Day day2 = Day.WEDNESDAY;
int comparison = day1.compareTo(day2);
if (comparison < 0) {
System.out.println(day1 + " comes before " + day2);
} else if (comparison > 0) {
System.out.println(day1 + " comes after " + day2);
} else {
System.out.println(day1 + " is the same as " + day2);
}
}
}
Output:
MONDAY comes before WEDNESDAY
Sorting Enums
You can use the compareTo(E o) method to sort an array or list of enum constants.
Example
import java.util.Arrays;
public class EnumSortingExample {
public static void main(String[] args) {
Day[] days = Day.values();
Arrays.sort(days);
for (Day day : days) {
System.out.println(day);
}
}
}
Output:
SUNDAY
MONDAY
TUESDAY
WEDNESDAY
THURSDAY
FRIDAY
SATURDAY
Real-World Use Case
Enum-Based Priority
Enums can be used to define priorities, and the compareTo(E o) method can be used to compare these priorities. For example, consider an enum representing task priorities.
Example
public enum Priority {
LOW,
MEDIUM,
HIGH,
CRITICAL
}
public class EnumPriorityExample {
public static void main(String[] args) {
Priority p1 = Priority.HIGH;
Priority p2 = Priority.MEDIUM;
int comparison = p1.compareTo(p2);
if (comparison < 0) {
System.out.println(p1 + " has lower priority than " + p2);
} else if (comparison > 0) {
System.out.println(p1 + " has higher priority than " + p2);
} else {
System.out.println(p1 + " has the same priority as " + p2);
}
}
}
Output:
HIGH has higher priority than MEDIUM
Conclusion
The compareTo(E o) method in Java provides a way to compare the ordinal values of enum constants. By using this method, you can determine the order of enum constants and perform sorting operations. Whether you are comparing days of the week, task priorities, or other sets of constants, the compareTo(E o) method offers a reliable way to manage and compare enum values.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment