Warning: include_once(): http:// wrapper is disabled in the server configuration by allow_url_include=0 in /home/users/2/moriya-h5/web/confrage/java/map/treemap/treemap.html on line 2

Warning: include_once(http://www.confrage.com/bin/header.php): failed to open stream: no suitable wrapper could be found in /home/users/2/moriya-h5/web/confrage/java/map/treemap/treemap.html on line 2

Warning: include_once(): Failed opening 'http://www.confrage.com/bin/header.php' for inclusion (include_path='.:/usr/local/php/5.6/lib/php') in /home/users/2/moriya-h5/web/confrage/java/map/treemap/treemap.html on line 2
TreeMapとComparator | Java | 株式会社CONFRAGE
Warning: include_once(/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/menu.php): failed to open stream: No such file or directory in /home/users/2/moriya-h5/web/confrage/java/map/treemap/treemap.html on line 17

Warning: include_once(): Failed opening '/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/menu.php' for inclusion (include_path='.:/usr/local/php/5.6/lib/php') in /home/users/2/moriya-h5/web/confrage/java/map/treemap/treemap.html on line 17
TreeMap
TreeMapはMapインターフェースを実装したクラスで、挿入した値を、キーの昇順に保持します。
getすると、キーの昇順で値が返ります。以下、実行例です。

import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

public class ExIterator {
    public static void main(String[] args) {
        Map map = new TreeMap();
        map.put("a","test2");
        map.put("c","test1");
        map.put("b","test3");
        
        StringBuffer buf = new StringBuffer();
        
        for (Iterator itr = map.keySet().iterator();itr.hasNext();){
            String key   = (String)itr.next();
            String value = (String)map.get(key);
            System.out.println(key +" = " + value);
        }
    }
}
結果は以下の通りです。

a = test2
b = test3
c = test1
Comparatorを実装する
TreeMapのコンストラクタにはComparatorインタフェースをimplementsしたクラスを引数にすることができます。
Comparatorはソートする順を定義するクラスです。
以下、実装例です。

package purejava;

import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

public class CompTest{
    public static void main(String[] args) {
        Map map = new TreeMap(new Comp());
        map.put( a , test2 );
        map.put( c , test1 );
        map.put( b , test3 );
        
        for (Iterator<String> itr = map.keySet().iterator();itr.hasNext();){
            String key   = (String)itr.next();
            String value = (String)map.get(key);
            System.out.println(key +  =   + value);
        }
    }
    // ソートするルールを定義
    static class Comp implements Comparator{
        public int compare(Object arg0,Object arg1) {
            return ((Comparable)arg0).compareTo(arg1) * -1;// 降順でソート
        }
    }
}
結果は以下の通りです。

c = test1
b = test3
a = test2

Back to top

Information

Warning: include_once(/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/side_bar.php): failed to open stream: No such file or directory in /home/users/2/moriya-h5/web/confrage/java/map/treemap/treemap.html on line 132

Warning: include_once(): Failed opening '/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/side_bar.php' for inclusion (include_path='.:/usr/local/php/5.6/lib/php') in /home/users/2/moriya-h5/web/confrage/java/map/treemap/treemap.html on line 132

Warning: include_once(/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/seo.php): failed to open stream: No such file or directory in /home/users/2/moriya-h5/web/confrage/java/map/treemap/treemap.html on line 141

Warning: include_once(): Failed opening '/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/seo.php' for inclusion (include_path='.:/usr/local/php/5.6/lib/php') in /home/users/2/moriya-h5/web/confrage/java/map/treemap/treemap.html on line 141