Elegant way to deserialise EnumSet from String
enums, enumset, java
Solution
With Java 8 you can do something like this with Lambda expressions and streams:
EnumSet.copyOf(Arrays.asList(str.split(","))
.stream().map(FooType::valueOf).collect(Collectors.toList()))
Problem
I'd like to serialise some `EnumSet<FooType>` to `String` using its `toString()` method. E.g.: `EnumSet.of(FooType.COMMON, FooType.MEDIUM).toString()` will give `[COMMON, MEDIUM]`. The question is about an elegant way to deserialise such a string back to the `EnumSet<FooSet>`. I'm looking for some commonly known library (may be like `apache-commons`) or a standard Util-class for such things. Something like: `EnumSetUtil.valueOf(FooType.class, "[COMMON, MEDIUM]")` I've implemented this thing in such way: ``` public static <E extends Enum<E>> EnumSet<E> valueOf(Class<E> eClass, String str) { String[] arr = str.substring(1, str.length() - 1).split(","); EnumSet<E> set = EnumSet.noneOf(eClass); for (String e : arr) set.add(E.valueOf(eClass, e.trim())); return set; } ``` But, may be there is a ready solution, or a dramatically easy way for doing this.