aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes more generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "more generics" Watch "more generics" New topic
Author

more generics

alan learner
Greenhorn

Joined: Oct 20, 2006
Posts: 6
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

Joined: Oct 20, 2006
Posts: 6
Argh got it: need to put
Map.Entry<K, V>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: more generics