Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print all enum values in Java?

Tags:

enum generalInformation {     NAME {         @Override         public String toString() {             return "Name";         }     },     EDUCATION {         @Override         public String toString() {             return "Education";         }     },     EMAIL {         @Override         public String toString() {             return "Email";         }     },     PROFESSION {         @Override         public String toString() {             return "Profession";         }     },     PHONE {         @Override         public String toString() {             return "Phone";         }     } } 

I have that information are avaiable in enum.

  1. How to print all enum values like: print.generalInformation?

That outputs:

Name
Education
Email
Phone

  1. How to pass that enum generalInformation as an arg in another function?
like image 297
Reegan Miranda Avatar asked Jan 19 '13 10:01

Reegan Miranda


People also ask

Can you print enums java?

By default, when you print an enum constant, it prints its literal value e.g. if the name of the enum instance is RED, then it will print RED. This is also the value that is returned by the name() method of java. lang. Enum class.

How do I print an enum constant?

You can use name() to print the constant name of the enum: Returns the name of this enum constant, exactly as declared in its enum declaration.

How do you iterate over an enum?

Enums don't have methods for iteration, like forEach() or iterator(). Instead, we can use the array of the Enum values returned by the values() method.

What does enum valueOf return?

valueOf. Returns the enum constant of the specified enum type with the specified name. The name must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)


2 Answers

System.out.println(java.util.Arrays.asList(generalInformation.values())); 

Your second part... Just the same as an interface or a class

like image 75
Stephen Connolly Avatar answered Oct 10 '22 21:10

Stephen Connolly


Firstly, I would refactor your enum to pass the string representation in a constructor parameter. That code is at the bottom.

Now, to print all enum values you'd just use something like:

// Note: enum name changed to comply with Java naming conventions for (GeneralInformation info : EnumSet.allOf(GeneralInformation.class)) {     System.out.println(info); } 

An alternative to using EnumSet would be to use GeneralInformation.values(), but that means you have to create a new array each time you call it, which feels wasteful to me. Admittedly calling EnumSet.allOf requires a new object each time too... if you're doing this a lot and are concerned about the performance, you could always cache it somewhere.

You can use GeneralInformation just like any other type when it comes to parameters:

public void doSomething(GeneralInformation info) {     // Whatever } 

Called with a value, e.g.

doSomething(GeneralInformation.PHONE); 

Refactoring using a constructor parameter

public enum GeneralInformation {     NAME("Name"),     EDUCATION("Education"),     EMAIL("Email"),     PROFESSION("Profession"),     PHONE("Phone");      private final String textRepresentation;      private GeneralInformation(String textRepresentation) {         this.textRepresentation = textRepresentation;     }      @Override public String toString() {          return textRepresentation;     } } 

With your current values, you could actually just convert the name to title case automatically - but that wouldn't be very flexible for the long term, and I think this explicit version is simpler.

like image 39
Jon Skeet Avatar answered Oct 10 '22 21:10

Jon Skeet