The following code shows that I can insert uncompatible type into Map, but when I can not retrieve element from it. In the following example, I can put two integers into Map, but if I uncomment the last two lines, I will get ClassCastException. Is this bug of JDK, or I miss something, as I remember Java generic guarantees taht we can not insert uncompatible type into generics collection class.
Hello World 页: 1
private static class MapResultExtractor<K, V> {
public Map<K, V> extract(Iterator<List<Object>> iter)
throws IOException {
Map<K, V> map = new HashMap<K, V>();
while (iter.hasNext()) {
List<Object> tuple = iter.next();
K key = (K) (tuple.get(0) == null ? null : tuple.get(0));
V value = (V) (tuple.get(1) == null ? null : tuple.get(1));
map.put(key, value);
iii
return map;
iii
iii
public static void main(String[] args) throws IOException {
MapResultExtractor<String, Integer> extractor = new MapResultExtractor<String, Integer>();
List<Object> subList = new ArrayList<Object>();
subList.add(1);
subList.add(2);
List<List<Object>> list = new ArrayList<List<Object>>();
list.add(subList);
Map<String, Integer> map = extractor.extract(list.iterator());
for (Map.Entry<String, Integer> entry : map.entrySet()) {
// System.out.println(entry.getKey().getClass() + " "
// + entry.getValue().getClass());
iii
iii
iii