Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert string array to enum array in java

Tags:

java

arrays

enums

I have a string array that contains the enum values taken from the user. How do I now convert this string array to enum array so that the elements can then be iterated and used further in other methods? This needs to be done in Java.

Basically I am asking is that for example if I have this array

String [] names = {"Autumn", "Spring", "Autumn", "Autumn" };

and I have this enum

enum Season
{ 
    Autumn, Spring;
}

How do I now convert the above array of String type to an array of enum Season type?

like image 650
pratzy Avatar asked Nov 17 '12 11:11

pratzy


People also ask

How do I convert a string to an enum in Java?

You can create Enum from String by using Enum. valueOf() method. valueOf() is a static method that is added on every Enum class during compile-time and it's implicitly available to all Enum along with values(), name(), and cardinal() methods.

Can we convert string array to string in Java?

So how to convert String array to String in java. We can use Arrays. toString method that invoke the toString() method on individual elements and use StringBuilder to create String. We can also create our own method to convert String array to String if we have some specific format requirements.

Is enum better than string?

The advantage of an enum is that they are a strongly typed value. There are no advantages to using strings.


4 Answers

Here's a complete code example:

private static List<Season> foo(List<String> slist) {
    List<Role> list = new ArrayList<>();
    for (String val : slist) {
        list.add(Season.valueOf(val));
    }
    return list;
}

Now if you want to make a generic method that would do this for any Enum, it gets a bit tricker. You have to use a "generic method":

private static <T extends Enum<T>> List<T> makeIt(Class<T> clazz, List<String> values) {
    List<T> list = new ArrayList<>();
    for (String level : values) {
        list.add(Enum.valueOf(clazz, level));
    }
    return list;
}

You'd have to call this as follows:

List<Strings> slist = ....
List<Season> elist= makeIt(Season.class, slist);
like image 51
DaBlick Avatar answered Sep 29 '22 23:09

DaBlick


Like this: code from here

public enum Weekdays {
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
    Sunday
}

If you need to do a lookup you can do:

Weekdays weekday = Weekdays.valueOf("Monday");
System.out.println(weekday);

But be carefull as this will throw an IllegalArgumentException if the provided String does not exists.

like image 25
Frank Avatar answered Sep 29 '22 22:09

Frank


to answer the actual question:

public <T extends Enum<T>> T[] toEnums(String[] arr, Class<T> type)
{
    T[] result = (T[]) Array.newInstance(type, arr.length);
    for (int i = 0; i < arr.length; i++)
        result[i] = Enum.valueOf(type, arr[i]);
    return result;
}

Season[] seasons = toEnums(names, Season.class);
like image 42
pstanton Avatar answered Sep 29 '22 22:09

pstanton


from Java 8 onwards you can make use of stream to achieve this in one line

String [] names = {"Autumn", "Spring", "Autumn", "Autumn" };
List<Season> seasons = Arrays.stream(names).map(Season::valueOf).collect(Collectors.toList());
like image 37
Ajithkumar_sekar Avatar answered Sep 29 '22 23:09

Ajithkumar_sekar