美文网首页
TreeMap的用法

TreeMap的用法

作者: 加一片柠檬233 | 来源:发表于2019-02-25 17:09 被阅读0次

基于红黑二叉树实现,线程非安全,不允许键对象是null,key不可以重复,value允许重复,存入TreeMap的元素应当实现Comparable接口或者实现Comparator接口,会按照排序后的顺序迭代元素,默认升序,两个相比较的key不得抛出classCastException。主要用于存入元素的时候对元素进行自动排序,迭代输出的时候就按排序顺序输出。

1. 构造函数:

  1. 默认构造函数。使用该构造函数,TreeMap中的元素按照自然排序进行排列。
    TreeMap()
  2. 构造一个TreeMap,并将某个映射表中的所有条目添加到TreeMap中
    TreeMap(Map<? extends K, ? extends V> m)
  3. 构造一个TreeMap,并指定Tree的比较器的建进行排序
    TreeMap(Comparator<? super K> comparator)
  4. 构造一个TreeMap,并将某个映射表中的所有条目添加到TreeMap中,并使用与给定的有序映射表相同的比较器
    TreeMap(SortedMap<K, ? extends V> m)

2.常用方法:

1.public int size() 返回此映射中键 - 值映射的数量。
2.public boolean containsKey(Object key) 如果此映射包含指定键的映射,则返回true。

  1. public boolean containsValue(Object value) 如果此映射将一个或多个键映射到指定值,则返回true。
public class Test {
    public static void main(String[] args) {
        TreeMap<Integer,Integer> map = new TreeMap<>();
        map.put(1, null);
        map.put(10, 1);
        map.put(3, 2);
        map.put(2, 3);
        System.out.println(map.size());//4
        System.out.println(map.containsKey(1));//true
       System.out.println(map.containsValue(4));//false
    }
}
  1. public K ceilingKey(K key) 如果不存在这样的键在方法调用返回的最小键大于或等于键,则返回null。
public static void main(String[] args) {
      TreeMap<Integer, String> treemap = new TreeMap<Integer, String>();
      treemap.put(2, "two");
      treemap.put(1, "one");
      treemap.put(3, "three");
      treemap.put(6, "six");
      treemap.put(5, "five");
      
      System.out.println(treemap.ceilingKey(4));//5
      System.out.println( treemap.ceilingKey(5));//5
      System.out.println( treemap.ceilingKey(7));//null
   }    
}
  1. public Object clone() 返回集合的副本
public class Test {
    public static void main(String[] args) {
        TreeMap<Integer, String> map = new TreeMap<>();
        map.put(10, "Geeks");
        map.put(15, "4");
        map.put(20, "Geeks");
        map.put(25, "Welcomes");
        map.put(30, "You");
        // Displaying the TreeMap
        System.out.println(map);
        System.out.println(map.clone());
    }
}
输出:
{10=Geeks, 15=4, 20=Geeks, 25=Welcomes, 30=You}
{10=Geeks, 15=4, 20=Geeks, 25=Welcomes, 30=You}

6.public Comparator <? super K> comparator() 方法调用返回用于排序在这个映射上,或者为null键,如果此映射使用键的自然顺序比较

  public static void main(String[] args) {
     TreeMap<Integer, String> map = new TreeMap<Integer, String>();
      
      // populating tree map
      map.put(2, "two");
      map.put(1, "one");
      map.put(3, "three");
      map.put(6, "six");
      map.put(5, "five");
      Comparator comp = treemap.comparator();
      System.out.println("Comparator value: "+ comp);     
   }    
} 
输出:
Comparator value: null

7 Map.Entry<K,V> ceilingEntry(K key) 查找大于等于参数key的map里包含的最小key或entry,没有就返回null

public static void main(String[] args) {
        TreeMap<Integer, String> map = new TreeMap<>();
        map.put(10, "Geeks");
        map.put(15, "4");
        map.put(20, "Geeks");
        map.put(25, "Welcomes");
        map.put(30, "You");
        System.out.println(map.ceilingEntry(10));//10=Geeks
        System.out.println( map.ceilingEntry(15));//15=4
    }
  1. public void clear()从此映射中删除所有映射。此调用返回后,映射将为空。
public static void main(String[] args) {
        TreeMap<Integer, String> map = new TreeMap<>();
        map.put(10, "Geeks");
        map.put(15, "4");
        map.put(20, "Geeks");
        map.put(25, "Welcomes");
        map.put(30, "You");
        map.clear();
        System.out.println(map);//{}

    }

3.遍历方法:

  1. 用TreeMap的keySet()方法,生成的对象是由key对象组成的Set,再利用TreeMap的get(key)方法,得到对应的value值
public static void main(String[] args) {
        TreeMap<Integer, String> map = new TreeMap<>();
        map.put(10, "Geeks");
        map.put(15, "4");
        map.put(20, "Geeks");
        map.put(25, "Welcomes");
        map.put(30, "You");
         Iterator it = map.keySet().iterator();
            while (it.hasNext()) 
                System.out.println(map.get(it.next()));
           }
    }
输出:      
Geeks
4
Geeks
Welcomes
You

2.利用entrySet()

TreeMap<Integer,Integer> map = new TreeMap<>();
        map.put(1, null);
        map.put(10, 1);
        map.put(3, 2);
        map.put(2, 3);
        for(Entry<Integer, Integer> entry:map.entrySet())
        {
            System.out.println(entry.getKey()+":"+entry.getValue());
        }
输出:
1:null
2:3
3:2
10:1

相关文章

  • TreeMap用法总结

    TreeMap用法总结 TreeMap中的元素默认按照keys的自然排序排列。 (对Integer来说,其自然排序...

  • TreeMap的用法

    构造方法 // 默认构造函数。使用该构造函数,TreeMap中的元素按照自然排序进行排列。TreeMap() //...

  • TreeMap的用法

    基于红黑二叉树实现,线程非安全,不允许键对象是null,key不可以重复,value允许重复,存入TreeMap的...

  • TreeMap相关用法

    TreeMap类通过使用树来实现Map接口。TreeMap提供了按排序顺序存储关键字/值对的有效手段,同时允许快速...

  • TreeMap了解一下

    前言 TreeMap TreeMap类继承图 TreeMap的域 TreeMap的构造函数 TreeMap常见Ap...

  • Java集合TreeMap用法总结

    Java的TreeMap是集合框架中的一个实现类,TreeMap继承了AbstractMap。TreeMap实现了...

  • Java集合中TreeMap的用法教程

    TreeMap是Map实现类SortMap的实现类。TreeMap实现了NavigableMap接口,内部的结构是...

  • TreeMap

    还是从几个常用的方法如数: TreeMap.TreeMap() : TreeMap.put() :   目前不清楚...

  • java8中treemap源码分析

    分析大纲: treemap中的实现原理 treemap中的remove()(红黑树的删除实践) treemap中的...

  • lambda HashMap 排序

    TreeMap 按key排序生成map可以有TreeMap 完成,TreeMap可以按key的自然顺序排序(Com...

网友评论

      本文标题:TreeMap的用法

      本文链接:https://www.haomeiwen.com/subject/hwspyqtx.html