Getting all the names in the enumeration as a string []

What is the easiest and / or shortest way to get enumeration element names as an array of String s?

I mean, if, for example, I had the following enumeration:

 public enum State { NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, TERMINATED; public static String[] names() { // ... } } 

the names() method returns an analog array with { "NEW", "RUNNABLE", "BLOCKED", "WAITING", "TIMED_WAITING", "TERMINATED" } .

+83
java arrays enums
Dec 09
source share
19 answers

Update:

In java 8, this is one line for an arbitrary enum class using a stream:

 public static String[] getNames(Class<? extends Enum<?>> e) { return Arrays.stream(e.getEnumConstants()).map(Enum::name).toArray(String[]::new); } 

In java 7, a little less elegant, but this one-liner trick does the trick:

 public static String[] names() { return Arrays.toString(State.values()).replaceAll("^.|.$", "").split(", "); } 

In addition, here is a version of this that will work for any listing:

 public static String[] getNames(Class<? extends Enum<?>> e) { return Arrays.toString(e.getEnumConstants()).replaceAll("^.|.$", "").split(", "); } 

What would you call so:

 String[] names = getNames(State.class); // any other enum class will work too 
+72
Dec 09 '12 at 1:18
source share

Create a String[] array for the names and call the static values() method, which returns all the enumeration values, then iterates over the values ​​and populates the names array.

 public static String[] names() { State[] states = values(); String[] names = new String[states.length]; for (int i = 0; i < states.length; i++) { names[i] = states[i].name(); } return names; } 
+62
Dec 09
source share

If you can use Java 8, this works well (alternative to the Yura suggestion, more efficient):

 public static String[] names() { return Stream.of(State.values()).map(State::name).toArray(String[]::new); } 
+10
Jun 02 '15 at 5:16
source share

Here is an elegant solution using Apache commons-lang3:

 EnumUtils.getEnumList(State.class) 

Although it returns a list, you can easily convert the list using list.toArray ()

+10
Mar 01 '16 at 8:50
source share

Using java 8:

 Arrays.stream(MyEnum.values()).map(Enum::name) .collect(Collectors.toList()).toArray(); 
+8
Mar 03 '15 at 9:22
source share

I would write it like this:

 public static String[] names() { java.util.LinkedList<String> list = new LinkedList<String>(); for (State s : State.values()) { list.add(s.name()); } return list.toArray(new String[list.size()]); } 
+5
Apr 04
source share

Something like that:

 public static String[] names() { String[] names = new String[values().length]; int index = 0; for (State state : values()) { names[index++] = state.name(); } return names; } 

The documentation recommends using toString() instead of name() in most cases , but you explicitly provided a name here.

+3
Dec 09
source share

My solution, with string manipulation (not the fastest, but compact):

 public enum State { NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, TERMINATED; public static String[] names() { String valuesStr = Arrays.toString(State.values()); return valuesStr.substring(1, valuesStr.length()-1).replace(" ", "").split(","); } } 
+2
Dec 09 '12 at 3:25
source share

Other methods:

The first

 Arrays.asList(FieldType.values()) .stream() .map(f -> f.toString()) .toArray(String[]::new); 

Another way

 Stream.of(FieldType.values()).map(f -> f.toString()).toArray(String[]::new); 
+2
Aug 10 '17 at 8:39 on
source share

I would do it this way (but I would probably make the names an unmodifiable set instead of an array):

 import java.util.Arrays; enum State { NEW,RUNNABLE,BLOCKED,WAITING,TIMED_WAITING,TERMINATED; public static final String[] names=new String[values().length]; static { State[] values=values(); for(int i=0;i<values.length;i++) names[i]=values[i].name(); } } public class So13783295 { public static void main(String[] args) { System.out.println(Arrays.asList(State.names)); } } 
+1
Dec 09
source share

I have the same need and use a generic method (inside the ArrayUtils class):

 public static <T> String[] toStringArray(T[] array) { String[] result=new String[array.length]; for(int i=0; i<array.length; i++){ result[i]=array[i].toString(); } return result; } 

And just define STATIC inside enum ...

 public static final String[] NAMES = ArrayUtils.toStringArray(values()); 

Java enumerations really skip the names () and get (index) methods, they are really useful.

+1
Apr 07 '15 at 5:09 on
source share

usual way (pun intended):

 String[] myStringArray=new String[EMyEnum.values().length]; for(EMyEnum e:EMyEnum.values())myStringArray[e.ordinal()]=e.toString(); 
+1
Sep 15 '16 at 1:29
source share

I tested @Bohemian's solution a bit. Performance is better when using a naive cycle.

 public static <T extends Enum<?>> String[] getEnumNames(Class<T> inEnumClass){ T [] values = inEnumClass.getEnumConstants(); int len = values.length; String[] names = new String[len]; for(int i=0;i<values.length;i++){ names[i] = values[i].name(); } return names; } //Bohemian solution public static String[] getNames(Class<? extends Enum<?>> e) { return Arrays.stream(e.getEnumConstants()).map(Enum::name).toArray(String[]::new); } 
+1
Nov 04 '16 at 10:12
source share

If you want the shortest, you can try

 public static String[] names() { String test = Arrays.toString(values()); return text.substring(1, text.length()-1).split(", "); } 
0
Dec 09 2018-12-12T00: 00Z
source share

Just a thought: you may not need to create a method to return enum values ​​as an array of strings.

Why do you need an array of strings? You may only need to convert the values ​​when you use them, if you ever need to do this.

Examples:

 for (State value:values()) { System.out.println(value); // Just print it. } 



 for (State value:values()) { String x = value.toString(); // Just iterate and do something with x. } 



 // If you just need to print the values: System.out.println(Arrays.toString(State.values())); 
0
Dec 09
source share

Try this:

 public static String[] vratAtributy() { String[] atributy = new String[values().length]; for(int index = 0; index < atributy.length; index++) { atributy[index] = values()[index].toString(); } return atributy; } 
0
May 18 '15 at 15:48
source share

org.apache.commons.lang3.EnumUtils.getEnumMap (State.class) .keySet ()

0
Apr 12 '16 at 11:04 on
source share

Another way to do this in Java 7 or earlier is to use Guava:

 public static String[] names() { return FluentIterable.from(values()).transform(Enum::name).toArray(String.class); } 
0
Oct 12 '18 at 9:19
source share

Just use: List<Something> somethingList = Arrays.asList(Something.values());

-one
May 24 '16 at 21:11
source share



All Articles