java-TreeMap

 原理:https://blog.csdn.net/woshimaxiao1/article/details/83748227

public class TreeMapDemo{
    public static void main(String[] args) {
        TreeMap<String,String> map = new TreeMap<>();
        map.put("1","qyc");
        map.put("6","qy");
        map.put("5","q");
        map.put("3","qycc");
        map.put("4","qyccc");

        //ceilingEntry  返回大于或等于key的最小键值对
        Map.Entry entry = map.ceilingEntry("2");
        System.out.println("entry = " + entry);
//        entry = 3=qycc

        //floorEntry 返回小于或者等于key的最大键值对
        Map.Entry entry1 = map.floorEntry("2");
        System.out.println("entry1 = " + entry1);

        //containsKey  返回boolean值,key是否有映射
        System.out.println(map.containsKey("2"));
        System.out.println(map.containsKey("3"));
        //containsValue  value是否被映射去,一个或多个   null是被映射的
        System.out.println(map.containsValue("2"));
        System.out.println(map.containsValue("q"));
//        false
//        true
//        false
//        true


        //descendingMap 返回倒序排序视图
        NavigableMap<String, String> stringStringNavigableMap = map.descendingMap();
        System.out.println("stringStringNavigableMap = " + stringStringNavigableMap);

//        返回正常顺序
        System.out.println("map.navigableKeySet() = " + map.navigableKeySet());



        //返回反响排序视图的key set
        NavigableSet<String> strings = map.descendingKeySet();
        System.out.println("strings = " + strings);

//      entrySet返回包含映射的set视图
        Set<Map.Entry<String, String>> entries = map.entrySet();
        for (Map.Entry e :
                entries) {
            System.out.println("e = " + e);
        }

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

        //foreach的lambda表达式
        map.forEach((k,v)-> {
            System.out.println("k = " + k);
            System.out.println("v = " + v);
        });


//        headMap  返回部分视图,密钥严格小于key
        SortedMap<String, String> stringStringSortedMap = map.headMap("4");
        System.out.println("stringStringSortedMap = " + stringStringSortedMap);
//        stringStringSortedMap = {1=qyc, 3=qycc}

//        tailMap 返回部分视图,密钥严格大于key
        System.out.println("map.tailMap(\"2\") = " + map.tailMap("2"));

//        第二个参数为true  表示 包括等于key
        NavigableMap<String, String> stringStringNavigableMap1 = map.headMap("4", true);
        System.out.println("stringStringSortedMap = " + stringStringNavigableMap1);
//        stringStringSortedMap = {1=qyc, 3=qycc, 4=qyccc}


        //返回严格大于key的最小健
        System.out.println("map.higherKey(\"4\") = " + map.higherKey("4"));
//        map.higherKey("4") = 5
        
        //返回严格大于key的最大值 
        System.out.println("map.lowerKey() = " + map.lowerKey("4"));
        System.out.println("map.lowerEntry() = " + map.lowerEntry("4"));

//        返回map的key视图
        System.out.println("map.keySet() = " + map.keySet());

//        返回最大值
        System.out.println("map.lastKey() = " + map.lastKey());
        System.out.println("map.lastEntry() = " + map.lastEntry());

        //返回value的collection集合
        System.out.println("map.values() = " + map.values());

        //删除最大值最小值并返回
        System.out.println("map.pollFirstEntry() = " + map.pollFirstEntry());
        System.out.println("map.pollLastEntry() = " + map.pollLastEntry());


//        subMap 返回key范围内的 视图
        System.out.println("map.subMap(\"2\",\"5\") = " + map.subMap("2","5"));
//        map.subMap("2","5") = {3=qycc, 4=qyccc}
        
        System.out.println("map = " + map);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_41835813/article/details/108586427
今日推荐