1

Is it possible to convert a comma separated string to the list of java.lang.Enum by using stream?

My original code is the following, which is working:

List<String> inValuesStr = Arrays.asList(criteria.getValue().toString().split(","));
List<Enum> inValues = new ArrayList<>();
for (String val : inValuesStr){
    inValues.add(Enum.valueOf(path.getType(),val));
}

I tried to refactor it to be as code below:

List<Enum> inValues = Arrays.stream(criteria.getValue().toString().split(","))
    .map(v -> Enum.valueOf(path.getType(),v))
    .collect(Collectors.toList());

Looks very basic... but, the following compile-time error is shown:

Error:(--, --) java: incompatible types: java.lang.Object cannot be converted to java.util.List<java.lang.Enum>

I can't understand where is the mistake. Did someone had the same experience? Thanks for sharing a solution.

14
  • 1
    eclipse issue may be? what does javac report? Commented Jul 16, 2019 at 13:21
  • 1
    It works alright for me too, but I don't use the same valueOf definition with two parameters Commented Jul 16, 2019 at 13:27
  • 1
    @Arnaud Claudel. It can be any enum type. For example, I have a Person entity which has a field sex, which is Sex enum. So this code is inside of an generic function that should convert list of string values criteria.getValue() to the list of specific type of enum path.getType(). What is interesting is that if I remove abstraction, it works List<Sex> inValues = Arrays.stream(criteria.getValue().toString().split(",")) .map(v -> Sex.valueOf(v)) .collect(Collectors.toList()); But, I would like to keep it abstract Commented Jul 16, 2019 at 13:40
  • 1
    changes List<Enum> to List<Type> Commented Jul 16, 2019 at 13:41
  • 1
    show your entire code that we can re-produce, not long comments; voting to close for now. Commented Jul 16, 2019 at 13:42

1 Answer 1

0

This works fine for me:

enum Ascii {
    A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z;
}

private void test() throws InterruptedException {
    List<String> inValuesStr = Arrays.asList("A,B,C,D,E,F,G,H,I,J,K,L,M,N".split(","));
    List<Ascii> list = inValuesStr.stream()
        .map(s -> Ascii.valueOf(s))
        .collect(Collectors.toList());
}
Sign up to request clarification or add additional context in comments.

4 Comments

He uses path.getType() as first parameter of Enum::valueOf. This doesn't answer the question at all
@YassinHajaj Works just the same with Enum.valueOf(Ascii.class, s)
I suspect the problem here is that path.getType() returns Class<?>, not Class<? extends Enum>. Using path.getType().asSubclass(Enum.class) (and potentially casting the result of the Enum.valueOf call...) should get around this.
@user2478398 not with Class<?>, but with the raw type Class. But in that case, asSubclass won’t help as the result of a generic operation on a raw type will be again a raw type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.