K
Kaiser S.
What do you think of this code ? Is there a better way do enforce this
kind of cast ?
public static <K, V> TreeMap<K, V> treemap(Object o, Class<K>
keyClass, Class<V> valueClass) {
TreeMap<?, ?> tm = (TreeMap)o; // warning 1
for (Map.Entry<?, ?> couple : tm.entrySet()) {
keyClass.cast(couple.getKey());
valueClass.cast(couple.getValue());
}
return (TreeMap)o; // warning 2
}
called with:
TreeMap<String, Double> tm = treemap(o, String.class, Double.class);
kind of cast ?
public static <K, V> TreeMap<K, V> treemap(Object o, Class<K>
keyClass, Class<V> valueClass) {
TreeMap<?, ?> tm = (TreeMap)o; // warning 1
for (Map.Entry<?, ?> couple : tm.entrySet()) {
keyClass.cast(couple.getKey());
valueClass.cast(couple.getValue());
}
return (TreeMap)o; // warning 2
}
called with:
TreeMap<String, Double> tm = treemap(o, String.class, Double.class);