Logan said:
Map.Entry entry = (Map.Entry) i.next();
System.out.println("key is " + entry.getKey()
+ " and value is " + entry.getValue());
The above assumes you're not using generics. I would probably do it
with generics, though:
SortedMap<String,String> map = new TreeMap<String,String>();
map.put("2", "Two");
map.put("1", "One");
map.put("3", "Three");
for (Map.Entry<String,String> entry : map.entrySet()) {
System.out.println("key is " + entry.getKey()
+ " and value is " + entry.getValue());
}
The above code is untested, but I'm fairly sure it's right.
- Logan
Excellent, thanks. Yep, it worked.
import java.util.Collection;
import java.util.TreeMap;
import java.util.Iterator;
import java.util.Set;
import java.util.Map;
import java.util.SortedMap;
public class IterateExample {
SortedMap<String,String> map = new TreeMap<String,String>();
public static void main (String [] args) {
IterateExample ie = new IterateExample();
ie.map.put("2", "Two");
ie.map.put("1", "One");
ie.map.put("3", "Three");
for (Map.Entry<String,String> entry : ie.map.entrySet()) {
System.out.println("key is " + entry.getKey()
+ " and value is " + entry.getValue());
}
}
}