What will be the output of the program?
import java.util.*;
public class Case91
{
public static void main(String[] args)
{
Map<String, Integer> hashed = new HashMap<String, Integer>();
Map<String, Integer> sorted = new TreeMap<String, Integer>();
for (String k : new String[] {"z", "a"})
{
hashed.put(k, k.length());
sorted.put(k, k.length());
}
System.out.println(hashed.equals(sorted) + " " + sorted.toString());
}
}
Answer: C
Map equality is defined across the interface as the same set of entries, so the implementation class is irrelevant and a HashMap can equal a TreeMap. Ordering plays no part in equality but does decide printing, and a TreeMap sorts its keys, so it shows a before z.
Please log in or register to report this question.