How to Read a Particular Column in Txt Using Java With Enum
Enum was introduced in Java 1.5 as a new type whose fields consists of a stock-still set up of constants. For example, we can create directions equally Coffee Enum with fixed fields as EAST, WEST, N, and SOUTH.
Java Enum
In this tutorial, we will acquire know how to create an Enum. Nosotros will also wait into the benefits of using enums in java and features of enum types. We will besides larn using Java Enum valueOf
, enum values
, EnumSet
and EnumMap
with examples.
Java Enum Example
Coffee enum keyword is used to create an enum type. Permit's take a await at the java enum example program.
package com.journaldev.enums; public enum ThreadStates { START, RUNNING, WAITING, DEAD; }
In higher up example, ThreadStates is the enum with fixed constants fields Offset, RUNNING, WAITING and DEAD.
Java Enum vs Constants
Now permit's see how java enum is amend than normal constants fields in Java classes.
Let's create a like constants grade in java.
bundle com.journaldev.enums; public course ThreadStatesConstant { public static final int Starting time = ane; public static last int WAITING = ii; public static final int RUNNING = three; public static concluding int Expressionless = 4; }
Now let'southward see how both enum and constants are used in a java plan:
/** * This method shows the benefit of using Enum over Constants */ private static void benefitsOfEnumOverConstants() { //Enum values are stock-still simpleEnumExample(ThreadStates.Showtime); simpleEnumExample(ThreadStates.WAITING); simpleEnumExample(ThreadStates.RUNNING); simpleEnumExample(ThreadStates.DEAD); simpleEnumExample(goose egg); simpleConstantsExample(1); simpleConstantsExample(2); simpleConstantsExample(iii); simpleConstantsExample(iv); //we tin can laissez passer whatever int constant simpleConstantsExample(5); } private static void simpleEnumExample(ThreadStates th) { if(th == ThreadStates.First) System.out.println("Thread started"); else if (th == ThreadStates.WAITING) System.out.println("Thread is waiting"); else if (th == ThreadStates.RUNNING) System.out.println("Thread is running"); else Organisation.out.println("Thread is dead"); } private static void simpleConstantsExample(int i) { if(i == ThreadStatesConstant.Showtime) Organization.out.println("Thread started"); else if (i == ThreadStatesConstant.WAITING) System.out.println("Thread is waiting"); else if (i == ThreadStatesConstant.RUNNING) Arrangement.out.println("Thread is running"); else System.out.println("Thread is dead"); }
If we expect at the to a higher place example, nosotros have 2 risks with using constants that are solved by the enum.
- We tin can pass any int constant to the
simpleConstantsExample
method just we can pass just stock-still values to simpleEnumExample, and so it provides type safety. - Nosotros can change the int constants value in
ThreadStatesConstant
grade but the above program volition not throw whatever exception. Our program might not piece of work as expected but if nosotros change the enum constants, we will get compile fourth dimension error that removes any possibility of runtime issues.
Java Enum Methods
Now permit's see more features of java enum with an example.
package com.journaldev.enums; import coffee.io.Closeable; import java.io.IOException; /** * This Enum instance shows all the things nosotros can do with Enum types * */ public enum ThreadStatesEnum implements Closeable{ START(ane){ @Override public String toString(){ render "Offset implementation. Priority="+getPriority(); } @Override public String getDetail() { render "Starting time"; } }, RUNNING(ii){ @Override public String getDetail() { render "RUNNING"; } }, WAITING(3){ @Override public String getDetail() { return "WAITING"; } }, DEAD(four){ @Override public String getDetail() { render "DEAD"; } }; individual int priority; public abstract String getDetail(); //Enum constructors should always be private. private ThreadStatesEnum(int i){ priority = i; } //Enum can have methods public int getPriority(){ return this.priority; } public void setPriority(int p){ this.priority = p; } //Enum can override functions @Override public String toString(){ return "Default ThreadStatesConstructors implementation. Priority="+getPriority(); } @Override public void close() throws IOException { System.out.println("Close of Enum"); } }
Coffee Enum Important Points
Below are some of the important points for Enums in Java.
- All java enum implicitly extends
java.lang.Enum
class that extends Object class and implements Serializable and Comparable interfaces. And so we can't extend any class in enum. - Since enum is a keyword, we can't end package name with it, for example
com.journaldev.enum
is non a valid packet name. - Enum tin can implement interfaces. As in in a higher place enum example, it's implementing
Closeable
interface. - Enum constructors are always private.
- We can't create instance of enum using new operator.
- We can declare abstruse methods in java enum, and so all the enum fields must implement the abstract method. In above example
getDetail()
is the abstract method and all the enum fields have implemented it. - Nosotros tin can define a method in enum and enum fields can override them too. For case,
toString()
method is defined in enum and enum field Start has overridden it. - Java enum fields has namespace, we tin can use enum field only with course name like
ThreadStates.START
- Enums can be used in switch argument, we volition see it in action in the afterwards function of this tutorial.
- We tin extend existing enum without breaking any existing functionality. For instance, we tin can add a new field NEW in ThreadStates enum without impacting whatever existing functionality.
- Since enum fields are constants, coffee all-time practice is to write them in block letters and underscore for spaces. For example Due east, WEST, EAST_DIRECTION etc.
- Enum constants are implicitly static and concluding
- Enum constants are terminal but it's variable can still be changed. For example, we can use
setPriority()
method to change the priority of enum constants. Nosotros will see information technology in usage in below example. - Since enum constants are final, nosotros can safely compare them using "==" and equals() methods. Both will have the same result.
Java EnumSet, EnumMap, valueOf()
At present we know most of the features of Enum, allow's have a look at Java Enum case plan. And so nosotros will acquire some more features of an enum.
package com.journaldev.enums; import java.io.IOException; import java.util.EnumMap; import java.util.EnumSet; import coffee.util.Set; public class JavaEnumExamples { public static void chief(String[] args) throws IOException { usingEnumMethods(); usingEnumValueOf(); usingEnumValues(); usingEnumInSwitch(ThreadStatesEnum.START); usingEnumInSwitch(ThreadStatesEnum.Dead); usingEnumMap(); usingEnumSet(); } private static void usingEnumSet() { EnumSet enumSet = EnumSet.allOf(ThreadStatesEnum.class); for(ThreadStatesEnum tsenum : enumSet){ System.out.println("Using EnumSet, priority = "+tsenum.getPriority()); } } individual static void usingEnumMap() { EnumMap<ThreadStatesEnum, String> enumMap = new EnumMap<ThreadStatesEnum,String>(ThreadStatesEnum.class); enumMap.put(ThreadStatesEnum.Starting time, "Thread is started"); enumMap.put(ThreadStatesEnum.RUNNING, "Thread is running"); enumMap.put(ThreadStatesEnum.WAITING, "Thread is waiting"); enumMap.put(ThreadStatesEnum.DEAD, "Thread is dead"); Set keySet = enumMap.keySet(); for(ThreadStatesEnum central : keySet){ System.out.println("fundamental="+cardinal.toString()+":: value="+enumMap.get(key)); } } private static void usingEnumInSwitch(ThreadStatesEnum thursday) { switch (th){ example START: System.out.println("START thread"); pause; case WAITING: System.out.println("WAITING thread"); break; case RUNNING: Organization.out.println("RUNNING thread"); pause; case DEAD: Organization.out.println("DEAD thread"); } } individual static void usingEnumValues() { ThreadStatesEnum[] thArray = ThreadStatesEnum.values(); for(ThreadStatesEnum th : thArray){ System.out.println(thursday.toString() + "::priority="+th.getPriority()); } } individual static void usingEnumValueOf() { ThreadStatesEnum th = Enum.valueOf(ThreadStatesEnum.class, "Starting time"); Organization.out.println("th priority="+th.getPriority()); } private static void usingEnumMethods() throws IOException { ThreadStatesEnum thc = ThreadStatesEnum.Expressionless; Organization.out.println("priority is:"+thc.getPriority()); thc = ThreadStatesEnum.DEAD; System.out.println("Using overriden method."+thc.toString()); thc = ThreadStatesEnum.Kickoff; System.out.println("Using overriden method."+thc.toString()); thc.setPriority(10); System.out.println("Enum Constant variable inverse priority value="+thc.getPriority()); thc.close(); } }
Earlier explaining other important features of enum, let's run into the output of the to a higher place program.
priority is:4 Using overriden method.Default ThreadStatesConstructors implementation. Priority=four Using overriden method.Kickoff implementation. Priority=1 Enum Constant variable inverse priority value=10 Close of Enum th priority=x START implementation. Priority=x::priority=ten Default ThreadStatesConstructors implementation. Priority=ii::priority=ii Default ThreadStatesConstructors implementation. Priority=3::priority=3 Default ThreadStatesConstructors implementation. Priority=four::priority=iv Offset thread DEAD thread key=Showtime:: value=Thread is started key=RUNNING:: value=Thread is running fundamental=WAITING:: value=Thread is waiting key=DEAD:: value=Thread is dead Using EnumSet, priority = 10 Using EnumSet, priority = ii Using EnumSet, priority = iii Using EnumSet, priority = four
Important Points
- The
usingEnumMethods()
methods shows how to create an enum object and how we can apply its methods. Information technology's also showing use ofsetPriority(int i)
method to change the variable of enum. -
usingEnumValueOf()
shows the usage ofjava.util.Enum
valueOf(enumType, name)
through which we tin create an enum object from String. Information technology throwsIllegalArgumentException
if the specified enum type has no abiding with the specified proper noun, or the specified form object does not stand for an enum type. It likewise throwsNullPointerException
if any of the arguments are nada. -
usingEnumValues()
method shows the usage of values() method that returns an array containing all of the values of the enum in the order they are declared. Annotation that this method is automatically generated past java compiler for every enum. You won't find values() implementation injava.util.Enum
grade. - The
usingEnumInSwitch()
method shows how to use enum constants in switch case. -
usingEnumMap()
method shows use of coffee.util.EnumMap, which is introduced in Java 1.5 Collections Framework.EnumMap
is Map implementation for use with enum type keys. All of the keys in an enum map must come from a single enum blazon that is specified, explicitly or implicitly, when the map is created. We tin't apply null as key for EnumMap and EnumMap is non synchronized. -
usingEnumSet()
method shows use of coffee.util.EnumSet, which is Set implementation for use with enum types. All of the elements in an enum ready must come from a unmarried enum type that is specified, explicitly or implicitly, when the set is created. EnumSet is not synchronized and cipher elements are not allowed. Information technology also provides some useful methods similarcopyOf(Drove<E> c)
,of(E first, E... rest)
andcomplementOf(EnumSet<E> south)
.
Reference: Oracle Doctor
Source: https://www.journaldev.com/716/java-enum
0 Response to "How to Read a Particular Column in Txt Using Java With Enum"
Post a Comment