TreeMap 的 tailMap()、headMap()、firstKey() 方法使用

import java.util.SortedMap;
import java.util.TreeMap;

/**
 * Author:  heatdeath
 * Date:    2018/5/13
 * Desc:
 */
public class TreeMapDemo {
    public static void main(String[] args) {
        // creating maps
        TreeMap treemap = new TreeMap<>();
        SortedMap treemapincl;

        // populating tree map
        treemap.put(2, "two");
        treemap.put(1, "one");
        treemap.put(3, "three");
        treemap.put(6, "six");
        treemap.put(5, "five");

        System.out.println("Getting tail map");
        treemapincl = treemap.tailMap(3);
        System.out.println("Tail map values: " + treemapincl);

        treemapincl = treemap.headMap(3);
        System.out.println("Head map values: " + treemapincl);

        System.out.println("First key is: " + treemap.firstKey());
    }
}
Getting tail map
Tail map values: {3=three, 5=five, 6=six}
Head map values: {1=one, 2=two}
First key is: 1

你可能感兴趣的:(Java基础)