Captain Koloth said:
The return types for keySet and entrySet are just Set, rather than
SortedSet. This could have been changed when Java 5 began to allow
overrides/implementations with covariant return types. Does anybody
know why it wasn't?
Perhaps I'm overlooking something, but shouldn't one prefer the
superinterface?
<code>
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
/**
* @author John B. Matthews
*/
public class MapTest {
public static void main(String[] args) {
final Map<String, List<String>> map =
new HashMap<String, List<String>>();
List<String> list = new ArrayList<String>(
Arrays.asList("Alpha", "Beta", "Gamma"));
map.put("Ordinals", list);
list = new ArrayList<String>(
Arrays.asList("Aleph", "Beth", "Gimel"));
map.put("Cardinals", list);
list = new ArrayList<String>(
Arrays.asList("Alpher", "Bethe", "Gammow"));
map.put("Physicists", list);
list = new ArrayList<String>(
Arrays.asList("Actinomyces", "Bordetella", "Giardia"));
map.put("Pathogens", list);
printMap(map);
System.out.println();
final Map<String, List<String>> tm = new
TreeMap<String, List<String>>(map);
printMap(tm);
}
private static void printMap(Map<String, List<String>> map) {
for (Map.Entry entry : map.entrySet()) {
System.out.println(entry.getKey()
+ " " + entry.getValue());
}
}
}
</code>
<console>
Physicists [Alpher, Bethe, Gammow]
Ordinals [Alpha, Beta, Gamma]
Cardinals [Aleph, Beth, Gimel]
Pathogens [Actinomyces, Bordetella, Giardia]
Cardinals [Aleph, Beth, Gimel]
Ordinals [Alpha, Beta, Gamma]
Pathogens [Actinomyces, Bordetella, Giardia]
Physicists [Alpher, Bethe, Gammow]
</console>