1

I have a hashmap defined like this that I cant change

private Map<String, Object> attributes;

The object is a list of Strings (sometimes size == 1, other times > 1).

I have an instance of the attributes map later on in the execution. I am wanna get the Object as List.

This:

List<String> outputs = attributes.get("keys");

throws this error.

incompatible types: java.lang.Object cannot be converted to java.util.List<java.lang.String>

How can I fix it?

user3861559
  • 973
  • 8
  • 25
  • 43

1 Answers1

3

Use an explicit cast:

List<String> outputs = (List<String>) attributes.get("keys");

Note that this is an unchecked cast and may throw ClassCastException at runtime.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
Unmitigated
  • 76,500
  • 11
  • 62
  • 80