What is Enum in Java ?
1). Enum concept introduced in 1.5 versions.
2). When compared with old languages enum java's enum is more powerful.
3). By using enum we can define our own data types which are also come enumerated data types.
Table of Content :
- Introduction
- Getting Java Enum Values
- Attaching Multiple Values
- Overriding toString()
- Caching the Enum Values
- Post/Questions related to Attach Values to Enum in Java
- Summary
Getting Java Enum Values
The valueOf(String) method is provided by java for all enum types. But to see the actual vales we have a static method to see actual values of the enum.
enum Loan {
PL("Personal Loan"),
HL("Home Loan"),
BL("Bike Loan"),
CL("Card Load"),
CCL("Credit Card Load");
public final String loanType;
private Loan(String loanType) {
this.loanType = loanType;
}
public static Loan valueOfLoan(String loanType) {
for (Loan loan: values()) {
if (loan.loanType.equals(loanType)) {
return loan;
}
}
return null;
}
}
public class EnumTest1 {
public static void main(String args[]) {
System.out.println(Loan.valueOfLoan("Credit Card Load"));
}
}
output:
CCL
Adding a Constructor and a Final Field
We can set the names into a final variable using a constructor.
package com.javacodestuffs.core.enums;
enum Loan {
PL("Personal Loan"),
HL("Home Loan"),
BL("Bike Loan"),
CL("Card Load"),
CCL("Credit Card Load");
public final String loanType;
private Loan(String loanType) {
this.loanType = loanType;
}
}
public class EnumTest2 {
public static void main(String args[]) {
System.out.println(Loan.HL.loanType);
}
}
output:
Home Loan
Here our loanType is final but our enum is not final we can set Enum values by using this loanType variable and we can also access Enum using this variable.
Attaching Multiple Values
We can attach Multiple Values to the Enum.As the Enum constructor can accept multiple values.
In the folowing example We have passed multiple parameters to Loan enum and we successfully accessed multiple values from enum.
package com.javacodestuffs.core.enums;
import java.util. * ;
enum Loan {
PL("Personal Loan", 11.5, 2),
HL("Home Loan", 8.5, 30),
BL("Bike Loan", 10.5, 2),
CL("Car Loan", 12.5, 5),
CCL("Credit Card Load", 18.5, 3);
private static final Map < String,
Loan > BY_LOAN_TYPE = new HashMap < >();
private static final Map < Double,
Loan > BY_INTEREST_RATE = new HashMap < >();
private static final Map < Integer,
Loan > BY_PERIOD = new HashMap < >();
static {
for (Loan loan: values()) {
BY_LOAN_TYPE.put(loan.loanType, loan);
BY_INTEREST_RATE.put(loan.interestRate, loan);
BY_PERIOD.put(loan.period, loan);
}
}
public final String loanType;
public final double interestRate;
public final int period;
private Loan(String loanType, double interestRate, int period) {
this.loanType = loanType;
this.interestRate = interestRate;
this.period = period;
}
public static Loan valueOfLoanType(String loanType) {
return BY_LOAN_TYPE.get(loanType);
}
public static Loan valueOfInterestRate(double interestRate) {
return BY_INTEREST_RATE.get(interestRate);
}
public static Loan valueOfPeriod(int period) {
return BY_PERIOD.get(period);
}
}
public class EnumTest3 {
public static void main(String args[]) {
System.out.println(" Loan by loanType : " + Loan.valueOfLoanType("Personal Loan"));
System.out.println(" Loan by interestRate : " + Loan.valueOfInterestRate(10.5));
System.out.println(" Loan by period : " + Loan.valueOfPeriod(3));
}
}
output:
Loan by loanType : PL
Loan by interestRate : BL
Loan by period : CCL
We can pass multiple values to the Enum constructors.
Overriding toString()
Overriding toString() is similar as overriding name()
@Override
public String toString() {
return this.loanType;
}
By default, Enum.toString() returns the same value as Enum.name().
Implementing an Interface
Enum can implement interfaces.
Let's consider this interface:
public interface Sorted {
String sort();
}
public enum Items implements Sorted {
// ...
@Override
public String sort() {
return item;
}
// ...
}
Caching the Enum Values
We can use Map to cache the enum values. This removes the need for enum to iterate. To do this, we define a static final Map.
package com.javacodestuffs.core.enums;
import java.util.* ;
enum Loan {
PL("Personal Loan"),
HL("Home Loan"),
BL("Bike Loan"),
CL("Card Load"),
CCL("Credit Card Load");
public final String loanType;
private Loan(String loanType) {
this.loanType = loanType;
}
private static final Map < String,Loan> BY_LOAN_TYPE = new HashMap < >()();
static {
for (Loan loan: values()) {
BY_LOAN_TYPE.put(loan.loanType, loan);
}
}
public static Loan valueOfLoanType(String loanType) {
return BY_LOAN_TYPE.get(loanType);
}
}
public class EnumTest4 {
public static void main(String args[]) {
System.out.println(Loan.valueOfLoanType("Personal Loan"));
}
}
output:
PL
By adding constructors, fields, and methods, we see that the enum can do a lot more than literal constants.
Post/Questions related to Attach Values to Enum in Java
How to get value from enum in java?
How to use enum constructor in java?
How to get enum key from the value in java?
How to convert the enum to list in java?
Enum in Java
How to pass enum as a parameter in java?
How to check enum contains a value in java?
How to use enum in switch case java?
How to override valueof in enum java?
What is the difference between iterator and enumeration in java?
How to check if an enum contains String
What is the advantage of enum in java?
In this article, we have seen how to Attach Values to Enum in Java with examples. All source code in the article can be found in the GitHub repository.
0 Comments
Post a Comment