Work with TreeMap in Java
Forums:
Map<Integer, String> dic = new TreeMap<Integer, String>() {{
put(3, "three");
put(1, "one");
put(2, "two");
}};
String str = "";
for (Entry<Integer, String> entry : dic.entrySet()) {
str += (str == "" ? "{" : ", ") +
String.format("%d=%s", entry.getKey(), entry.getValue());
}
str += "}";
//str is "{1=one, 2=two, 3=three}"
//sorted by key
- Log in to post comments