TreeMap

package edu.xcdq.map;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class TreeMapDemo {
    public static void main(String[] args) {
        TreeMap map = new TreeMap<>();  // <> 钻石语法
        map.put(1 , "hahah");   // Entry
        map.put(2 , "hehe");
        map.put(3 , "heihei");
        map.put(4 , "huihui");

        System.out.println(map);

        for (Map.Entry e : map.entrySet() ) {
            System.out.print(e.getKey() + " = " + e.getValue()  +"\t");
        }
        System.out.println();

        for( Integer i : map.keySet() ) {
            System.out.print( i +" = "+ map.get(i)  + "\t");
        }
        System.out.println();
        for (String v :  map.values()) {
            System.out.print( v +"\t" );
        }

        System.out.println();

        System.out.println("firstKey => "+   map.firstKey());
        System.out.println("firstEntry => "+ map.firstEntry());
        System.out.println("lastKey => "+    map.lastKey());
        System.out.println("lastEntry => "+  map.lastEntry());


    }
}

你可能感兴趣的:(TreeMap)