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.
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.
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.
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 .
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.
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());
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]
.
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));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With