欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

Core Java Tutorial -,corejavatutorial

来源: javaer 分享于  点击 44472 次 点评:91

Core Java Tutorial -,corejavatutorial


Java TreeMap 是一个 Map 的实现,它是 Java 集合框架的一部分。

Java TreeMap

TreeMap 一些需要记住的重点:

Java TreeMap Example

让我们看一下 TreeMap 的示栗程序,来查看它在行动中的自然排序。

package TreeMap;

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

public class JavaTreeMapExample {

    public static void main(String[] args) {
        Map<Integer, String> map = new TreeMap<>();

        map.put(10, "10");
        map.put(1, "1");
        map.put(5, "5");

        System.out.println(map);

        map = new TreeMap<>(new Comparator<Integer>() {

            @Override
            public int compare(Integer x, Integer y) {
                return (x > y) ? -1 : ((x == y) ? 0 : 1);
            }

        });

        Map<Integer, String> reverseMap;

        reverseMap = new TreeMap<>((x, y) -> (x < y) ? -1 : ((x == y) ? 0 : 1));

        map.put(10, "10");
        map.put(1, "1");
        map.put(5, "5");
        System.out.println(map);

        reverseMap.put(10, "10");
        reverseMap.put(1, "1");
        reverseMap.put(5, "5");
        System.out.println(reverseMap);

    }
}

输出:

{1=1, 5=5, 10=10}
{10=10, 5=5, 1=1}
{1=1, 5=5, 10=10}

注意,当我们在创建 TreeMap 没有提供 Comparator 时,它使用 Integer compareTo 方法来对键进行排序。这就是为什么即使我们以任何顺序插入键,键的顺序仍然在增加。

TreeMap vs HashMap

TreeMap 和 HashMap 都实现了 Map 接口,并且是集合框架的一部分。我们来看看 TreeMap 和 HashMap 之间的一些区别。

When to use TreeMap in Java

大多数时候,作为 Map 实现的 HashMap 就够用了。但是如果你由一些关于排序、查找更低或者更高的键的特殊需求,使用 submap,你就要用 TreeMap 了。

我们来看一个简单的 TreeMap 示栗程序,显示 NavigableMap 方法的用法。

package TreeMap;

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

public class JavaTreeMapNavigationExamples {

    public static void main(String[] args) {

        // we have to define object as TreeMap to use NavigableMap functions
        TreeMap<Integer, String> map = new TreeMap<>();
        for (int i = 0; i < 10; i++) {
            map.put(i, i + "");
        }

        System.out.println(map);

        // find id closest to 5, lower and higher
        Map.Entry<Integer, String> entry = map.lowerEntry(5);
        System.out.println("Closest Lower key than 5 is " + entry);
        entry = map.higherEntry(5);
        System.out.println("Closest Higher key than 5 is " + entry);

        System.out.println("Closest Lower key than 4 is " + map.lowerKey(4));

        entry = map.floorEntry(5);
        System.out.println("Closest floor entry than 5 is " + entry);

        entry = map.ceilingEntry(4);
        System.out.println("Closest ceiling key than 4 is " + entry);

        entry = map.firstEntry();
        System.out.println("First Entry is " + entry);

        entry = map.lastEntry();
        System.out.println("Last Entry is " + entry);

        Map<Integer, String> reversedMap = map.descendingMap();
        System.out.println("Reversed Map: " + reversedMap);

        // poll and remove first, last entries
        entry = map.pollFirstEntry();
        System.out.println("First Entry is " + entry);
        entry = map.pollLastEntry();
        System.out.println("Last Entry is " + entry);
        System.out.println("Updated Map: " + map);

        // submap example
        Map<Integer, String> subMap = map.subMap(2, true, 6, true);
        System.out.println("Submap: " + subMap);

        subMap = map.headMap(5, true);
        System.out.println("HeadMap: " + subMap);

        subMap = map.tailMap(5, true);
        System.out.println("TailMap: " + subMap);
    }
}

输出:

{0=0, 1=1, 2=2, 3=3, 4=4, 5=5, 6=6, 7=7, 8=8, 9=9}
Closest Lower key than 5 is 4=4
Closest Higher key than 5 is 6=6
Closest Lower key than 4 is 3
Closest floor entry than 5 is 5=5
Closest ceiling key than 4 is 4=4
First Entry is 0=0
Last Entry is 9=9
Reversed Map: {9=9, 8=8, 7=7, 6=6, 5=5, 4=4, 3=3, 2=2, 1=1, 0=0}
First Entry is 0=0
Last Entry is 9=9
Updated Map: {1=1, 2=2, 3=3, 4=4, 5=5, 6=6, 7=7, 8=8}
Submap: {2=2, 3=3, 4=4, 5=5, 6=6}
HeadMap: {1=1, 2=2, 3=3, 4=4, 5=5}
TailMap: {5=5, 6=6, 7=7, 8=8}

相关文章

    暂无相关文章
相关栏目:

用户点评