トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS

JavaでTreeMapを使ってみた のバックアップ(No.1)


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));
        }
    }

}

ちなみに、キーが重複した場合は後勝ちです。

参考サイト