6

I am making bukkit plugin and one function returns List<Map<?, ?>>, if I to do that:

List<Map<String, String>> circle = FileManager.area.getMapList("circles");

I get error that it can't be converted. What do to?

The error:

List<Map<?, ?>> cannot be converted into List<Map<String, String>>
3
  • What's your exact error. Always post your exact error when asking a question. Errors are meaningful responses by any programming language. Not posting one is like saying "My car broke please help" and attaching a picture of the car. Commented Jan 6, 2015 at 18:35
  • exact error - List<Map<?, ?>> cannot be converted into List<Map<String, String>> Commented Jan 6, 2015 at 18:37
  • 2
    upload.wikimedia.org/wikipedia/commons/b/bb/… -- please help Commented Jan 6, 2015 at 18:38

4 Answers 4

6

You can cast away all the generics by casting to the raw type, (List):

List<Map<String, String>> circle = (List) FileManager.area.getMapList("circles");

Note that as with most casts this is unsafe - it's better to find a way to pass the correct type information through, as other answers suggest.

Sign up to request clarification or add additional context in comments.

Comments

1

Map<String, String> is a subtype of Map<?, ?>, but since generics are not invariant this does not mean that List<Map<String, String>> is a subtype of List<Map<?, ?>>.

If you are certain that all the keys and values are String instances you can do this

List<Map<String, String>> circle = (List<Map<String, String>>) (Object) FileManager.area.getMapList("circles");

However I wouldn't recommend it because if any of the keys or values are not Strings you may get a ClassCastException later on, so you are throwing away the guarantee that generics are supposed to provide.

An alternative would be to create a new ArrayList<Map<String, String>>() and iterate over the List<Map<?, ?>>, then use instanceof to check all the keys and values in each Map<?, ?> are String. Only if the instanceof checks succeed should you add the entry.

Comments

1

getMapList returns the type List<Map<?, ?>> into List<Map<String, String>>. Since generics are invariant you need to match the types

List<Map<?, ?>> circle = FileManager.area.getMapList("circles");

4 Comments

Can't use List<Map<Object, Object>> instead?
Sorry but thats method of bukkit's API, i can't change it
@MarounMaroun Not even that, compiler is very particular about types, only exact mapping allowed. Remember ? is a source collection, PECS, etc
@XsergeiX this answer suggests to change your declaration not that of the API
0

is mostly used for assigning from some defined value like: Map m = new HashMap();

And using is not good, because you need to explicitly cast it(which means that you know your runtime type beforehand).

Usually you explicitly specify the return values.So let's see by cases:

1) You want to write very generic function which gets string and returns List>, then you can write like this

<K, V> List<Map<K, V>> getMapList(String s) {
      ... 
} 

Java will inference the type of K, V depending on what you're assigning to.

2) You want to return some specific type, then use that type:

<Integer, String> List<Map<Integer, String>> getMapList(String s) {
      ... 
} 

Comments

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.