Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java convert a string to list of enums [duplicate]

Tags:

java

string

enums

I have a string as follows:

"[Monday, Tuesday]"

I want to convert this string to a list of enums of type "Day". My "Day" enum is as follows:

public enum Day {
    Monday,Tuesday;
}

I obtained my input string by calling toString() on a List of "Day"s. As follows:

List<Day> days = new ArrayList<Day>();
days.add(Day.Monday);
days.add(Day.Tuesday);

String input = days.toString();

I know that I can parse the input string by commas and brackets. But, is there any efficient way to do this?

To repeat, I want to covert string to a list of enums.

** EDIT **

Question is not just about converting String to enum and vice versa. It involves lists and string parsing.

like image 624
hrishikeshp19 Avatar asked Apr 15 '15 21:04

hrishikeshp19


People also ask

Can you use == for enums?

Because there is only one instance of each enum constant, it is permissible to use the == operator in place of the equals method when comparing two object references if it is known that at least one of them refers to an enum constant.

How do I list enums?

Get a list of Enum members. The idea is to use the Enum. GetValues() method to get an array of the enum constants' values. To get an IEnumerable<T> of all the values in the enum, call Cast<T>() on the array.

Can we create list of enums?

tl;dr. Can you make and edit a collection of objects from an enum? Yes. If you do not care about the order, use EnumSet , an implementation of Set .

Can you have an ArrayList of enums?

You can create a list that holds enum instances just like you would create a list that holds any other kind of objects: ? List<ExampleEnum> list = new ArrayList<ExampleEnum>(); list.


3 Answers

In case you use Java 8:

//assume your Day enum has more values
String string = "[Monday, Wednesday, Tuesday, Friday, Thursday]";
String stringWithNoBrackets = string.substring(1, string.length() - 1);
List<Days> days = Arrays.asList(stringWithNoBrackets.split(",\\s+"))
        .stream()
        .map(Days::valueOf)
        .collect(Collectors.toList());
System.out.println(days);

Also, we don't need to convert the array into a list, using less code:

List<Days> days2 = Arrays.stream(stringWithNoBrackets.split(",\\s+"))
    .map(Days::valueOf)
    .collect(Collectors.toList());
like image 179
Luiggi Mendoza Avatar answered Oct 18 '22 18:10

Luiggi Mendoza


Your question doesn't really make much sense. If you want to send or store list of enums then you can simply serialize and deserialize it since ArrayList and each enum are Serializable.

Example:

List<Day> ofiginalList = new ArrayList<Day>();
ofiginalList.add(Day.Monday);
ofiginalList.add(Day.Tuesday);

ByteArrayOutputStream bout = new ByteArrayOutputStream();

ObjectOutput out = new ObjectOutputStream(bout);
out.writeObject(ofiginalList);

ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
ObjectInput in = new ObjectInputStream(bin);

List<Day> deserializedList = (List<Day>) in.readObject();
System.out.println(deserializedList);

Output: [Monday, Tuesday].

like image 31
Pshemo Avatar answered Oct 18 '22 18:10

Pshemo


You can try doing something like

List<Day> days = new ArrayList<>();
StringTokenizer tokenizer = new StringTokenizer("[Monday, Tuesday]", "[], ");
while (st.hasMoreTokens()) {
  String token = st.nextToken();
  days.add(Day.valueOf(Day.class, token));
}
like image 20
Claudio Avatar answered Oct 18 '22 17:10

Claudio