Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

more generics  RSS feed

 
alan learner
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why will the following not compile:
public static <K, V> Map<V, K> mangle(Map<K, V> in){
Map<V, K> ut = new HashMap<V, K>();
for ( Map.Entry entry: in.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue());
ut.put(entry.getValue(), entry.getKey());
}
return ut;
}


But it will compile if I cast the put parameters as follows:
public static <K, V> Map<V, K> mangle(Map<K, V> in){
Map<V, K> ut = new HashMap<V, K>();
for ( Map.Entry entry: in.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue());
ut.put((V)entry.getValue(), (K)entry.getKey());
}
return ut;
}
 
alan learner
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Argh got it: need to put
Map.Entry<K, V>
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!