package examples;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
public class TreeMapExample
{
public static void main(String[] args)
{
TreeMap<String, String> map = new TreeMap<String, String>();
map.put("Mary", "555-1234");
map.put("Frank", "555-5678");
map.put("Joe", "555-9012");
map.put("Sylvia", "555-3456");
map.put("Vanessa", "555-7890");
map.put("Frank", "555-0987"); // Duplicate
for (Iterator<Map.Entry<String, String>> iter = map.entrySet().iterator(); iter.hasNext(); )
{
Map.Entry<String, String> entry = iter.next(); // Note NO required cast
String key = entry.getKey(); // Note NO required cast
String value = entry.getValue(); // Note NO required cast
System.out.println(key + ":" + value);
}
}
}
|