vendredi 19 octobre 2018

In Java can I use reflection to determine the concrete collection type of a collection interface? [duplicate]

This question already has an answer here:

If I wanted to reflect on a Java collection, like this:

public class JavaColl {
    Map<String,Integer> items;
}

val jmap = new HashMap[String, Integer]()
jmap.put("a", 5)
jmap.put("b", 6)
jmap.put("c", 7)

val inst = new co.blocke.test.JavaColl()
inst.setItems(jmap)

Now if I reflect on my JavaColl instance, I can find that items is of type Map, but is there a way to see that it is actually a HashMap, or is this lost to type erasure?





Aucun commentaire:

Enregistrer un commentaire