What is Enum in Java
Java Enum is a Java type used to define the collection of constants. A java Enum is a special kind of Java class. An enum can contain constants, fields, methods etc. Java Enum were added in Java 5.
In previous post I have discussed about Enum in Java with detail explanation Why and for what should we use Enum in Java.
In this tutorial we will go over how to compare Enum in your application. We will discuss how to compare Enum using Switch-Case statement, == operator, and equals() method.
If you have below question then you are at right place.
- Enum comparison in java
- Enum comparison string in java
- Compare enum using == operator in java
- Compare enum using switch-case statement
- Compare enum using equals method
- How to compare two enum in java
- Comparing java enum members
- Comparing using java.lang.Enum.compareTo()
We can Compare Enum in four ways.
- Switch-Case Statement
- == operator
- equals() method
- compareTo() method of Enum
Let's start
Step 1
Company.java
package com.walking.techie; public enum Company { ORACLE("Oracle"), YAHOO("Yahoo"), GOOGLE("Google"), FACEBOOK("Facebook"); private String companyName; Company(String companyName) { this.companyName = companyName; } public String getCompanyName() { return companyName; } }
Step 2
EnumComparisonDemo.java
package com.walking.techie; /* * @Author: walkingtechie */ public class EnumComparisonDemo { public static void main(String[] args) { System.out.println("=====Java Enum comparison using switch-case====="); compareUsingSwitch(Company.FACEBOOK); compareUsingSwitch(Company.GOOGLE); compareUsingSwitch(Company.YAHOO); compareUsingSwitch(Company.ORACLE); System.out.println("\n"); System.out.println("=====Java Enum comparison using == operator====="); compareUsingEqualOperator(Company.FACEBOOK); compareUsingEqualOperator(Company.GOOGLE); compareUsingEqualOperator(Company.YAHOO); compareUsingEqualOperator(Company.ORACLE); System.out.println("\n"); System.out.println("=====Java Enum comparison using equals method====="); compareUsingEqualsMethod(Company.FACEBOOK); compareUsingEqualsMethod(Company.GOOGLE); compareUsingEqualsMethod(Company.YAHOO); compareUsingEqualsMethod(Company.ORACLE); System.out.println("\n"); System.out.println("=====Java Enum comparison using java.lang.Enum.compareTo====="); compareUsingCompareTo(Company.FACEBOOK); compareUsingCompareTo(Company.GOOGLE); compareUsingCompareTo(Company.ORACLE); compareUsingCompareTo(Company.YAHOO); } private static void compareUsingCompareTo(Company company) { if (company.compareTo(Company.FACEBOOK) == 0) { System.out.println("This is Facebook"); } else if (company.compareTo(Company.GOOGLE) == 0) { System.out.println("This is Google."); } else if (company.compareTo(Company.YAHOO) == 0) { System.out.println("This is Yahoo"); } else if (company.compareTo(Company.ORACLE) == 0) { System.out.println("This is Oracle."); } else { System.out.println("Enum checked failed: " + company); } } private static void compareUsingEqualsMethod(Company company) { if (company.equals(Company.FACEBOOK)) { System.out.println("This is Facebook."); } else if (company.equals(Company.GOOGLE)) { System.out.println("This is Google."); } else if (company.equals(Company.ORACLE)) { System.out.println("This is Oracle."); } else if (company.equals(Company.YAHOO)) { System.out.println("This is Yahoo."); } else { System.out.println("Enum checked failed: " + company); } } private static void compareUsingEqualOperator(Company company) { if (company == Company.FACEBOOK) { System.out.println("\"== operator:\" This is Facebook."); } else if (company == Company.GOOGLE) { System.out.println("\"== operator:\" This is Google."); } else if (company == Company.ORACLE) { System.out.println("\"== operator:\" This is Oracle."); } else if (company == Company.YAHOO) { System.out.println("\"== operator:\" This is Yahoo"); } else { System.out.println("\"== operator:\" Enum checked failed: " + company); } } private static void compareUsingSwitch(Company company) { switch (company) { case FACEBOOK: System.out.println("This is Facebook."); break; case GOOGLE: System.out.println("This is Google."); break; case ORACLE: System.out.println("This is Oracle."); break; default: System.out.println("Enum checked failed: " + company); break; } } }
Output of above program is shown below:
=====Java Enum comparison using switch-case===== This is Facebook. This is Google. Enum checked failed: YAHOO This is Oracle. =====Java Enum comparison using == operator===== "== operator:" This is Facebook. "== operator:" This is Google. "== operator:" This is Yahoo "== operator:" This is Oracle. =====Java Enum comparison using equals method===== This is Facebook. This is Google. This is Yahoo. This is Oracle. =====Java Enum comparison using java.lang.Enum.compareTo===== This is Facebook This is Google. This is Oracle. This is Yahoo
No comments :
Post a Comment