Java TreeMap iterator not working correctly for String keys?

It works just fine: import java.util. Comparator; import java.util. Iterator; import java.util.

TreeMap; public class test2 { public static class City { public final String m_name; public City(String aName) { m_name = aName; } } public static class CityNameComparator implements Comparator { public int compare (String c1, String c2) { return c1. CompareTo(c2); } } public static class CityMap { TreeMap nameDictionary = new TreeMap(new CityNameComparator()); public Iterator getNameIterator(){ return nameDictionary.values().iterator(); } public City put(String aName) { return nameDictionary. Put(aName, new City(aName)); } } public static void main(String args) { CityMap cityMap = new CityMap(); cityMap.

Put("d"); cityMap. Put("b"); cityMap. Put("c"); cityMap.

Put("a"); for (Iterator cities = cityMap.getNameIterator(); cities.hasNext(); ) { City city = cities.next(); System.out. Println(city. M_name); } } } Output: a be c d.

That would preserve the order the entries were added to the map. Or perhaps there is a bug in the code that is adding entries, putting the wrong value with a key. Iterate over the entries, and see what is in the map: for (Map.

Entry e : dictionary.entrySet()) System.out. Println(e.getKey() + " --> " + e.getValue()).

Sorry, stupid bug. I was assigning a different iterator based on a bug somewhere else. It works fine now.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions