JavaでTreeMapを使ってみた
Last-modified: 2013-08-24 (土) 02:14:21 (3890d)
Top / JavaでTreeMapを使ってみた
JavaでTreeMap?を使ってみた †
こんな感じでエントリーがソートされます。
package example.ex5; import java.util.Iterator; import java.util.TreeMap; public class TreeMapTest { /** * @param args */ public static void main(String[] args) { // TODO 自動生成されたメソッド・スタブ TreeMap map = new TreeMap(); map.put("Name", "Tanaka"); map.put("Age", new Integer(26)); map.put("0", "0000000"); map.put("15", "0000015"); map.put("2", "0000002"); map.put("3", "0000003"); map.put("4", "0000004"); map.put("10", "0000010"); map.put("9", "0000009"); map.put("5", "000005"); map.put("0", "000000"); map.put("11", "000011"); map.put("十", "漢字の十"); map.put("二", "漢字の二"); map.put("一", "漢字の一"); map.put("第32", "第32"); map.put("第25", "第25"); map.put("第22", "第22"); map.put("杜", "漢字の一"); System.out.println(map.firstEntry().getValue()); System.out.println(map.lastEntry().getValue()); Iterator it = map.keySet().iterator(); while (it.hasNext()) { Object o = it.next(); System.out.println(o + " = " + map.get(o)); } } }
出力はこんな感じです。
0000000 000005 0 = 0000000 10 = 0000010 15 = 0000015 2 = 0000002 3 = 0000003 4 = 0000004 9 = 0000009 Age = 26 Name = Tanaka 一 = 漢字の一 二 = 漢字の二 十 = 漢字の十 杜 = 漢字の一 第22 = 第22 第25 = 第25 第32 = 第32 0 = 000000 11 = 000011 5 = 000005
ちなみに、キーが重複した場合は後勝ちです。