ClassCastException and Maps
For some reason i can't get either of the print statements to work.
Could someone please tell me how to fix this and what's goin wrong.
Error: ClassCastException
import java.util.*;
publicclass bag{
publicstaticvoid main(String [] args){
Map m =new TreeMap();
m.put(1, 2);
m.put(2, 6);
m.put(3, 8);
m.put(100,1);
Set s = m.entrySet();
Iterator it = s.iterator();
System.out.println(m.get(it.next()));
Object val = m.get(it.next());
System.out.println(val);
}
}
[921 byte] By [
BlackJacka] at [2007-11-27 7:37:34]

If you read the complete error message closely, you can guess at the problem. If you look at the source code, you'll see it.
get calls compare which casts to a Comparable. The iterator over the entrySet gives TreeMap.Entry objects, which don't implement Comparable.
Your iterator gives you (1, 2). If you read the docs for EntrySet, you'll see that's a Map.Entry, and if you read its docs, you'll see it has both the key and the value, so there's no reason for the get.
jverda at 2007-7-12 19:18:10 >
