溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

TreeMap簡單教程

發(fā)布時(shí)間:2020-07-01 15:00:11 來源:網(wǎng)絡(luò) 閱讀:120 作者:ckllf 欄目:編程語言

  TreeMap簡介

  在Map集合框架中,除了HashMap以外,TreeMap也是常用到的集合對(duì)象之一。

  與HashMap相比,TreeMap是一個(gè)能比較元素大小的Map集合,會(huì)對(duì)傳入的key進(jìn)行了大小排序。其中,可以使用元素的自然順序,也可以使用集合中自定義的比較器來進(jìn)行排序;

  不同于HashMap的哈希映射,TreeMap實(shí)現(xiàn)了紅黑樹的結(jié)構(gòu),形成了一顆二叉樹。

  TreeMap繼承于AbstractMap,實(shí)現(xiàn)了Map, Cloneable, NavigableMap, Serializable接口。

  (1)TreeMap 繼承于AbstractMap,而AbstractMap實(shí)現(xiàn)了Map接口,并實(shí)現(xiàn)了Map接口中定義的方法,減少了其子類繼承的復(fù)雜度;

  (2)TreeMap 實(shí)現(xiàn)了Map接口,成為Map框架中的一員,可以包含著key-value形式的元素;

  (3)TreeMap 實(shí)現(xiàn)了NavigableMap接口,意味著擁有了更強(qiáng)的元素搜索能力;

  (4)TreeMap 實(shí)現(xiàn)了Cloneable接口,實(shí)現(xiàn)了clone()方法,可以被克隆;

  (5)TreeMap 實(shí)現(xiàn)了Java.io.Serializable接口,支持序列化操作;

  TreeMap具有如下特點(diǎn):

  不允許出現(xiàn)重復(fù)的key;

  可以插入null鍵,null值;

  可以對(duì)元素進(jìn)行排序;

  無序集合(插入和遍歷順序不一致);

  TreeMap基本操作

  public class TreeMapTest {

  public static void main(String[] agrs){

  //創(chuàng)建TreeMap對(duì)象:

  TreeMap treeMap = new TreeMap();

  System.out.println("初始化后,TreeMap元素個(gè)數(shù)為:" + treeMap.size());

  //新增元素:

  treeMap.put("hello",1);

  treeMap.put("world",2);

  treeMap.put("my",3);

  treeMap.put("name",4);

  treeMap.put("is",5);

  treeMap.put("huangqiuping",6);

  treeMap.put("i",6);

  treeMap.put("am",6);

  treeMap.put("a",6);

  treeMap.put("developer",6);

  System.out.println("添加元素后,TreeMap元素個(gè)數(shù)為:" + treeMap.size());

  //遍歷元素:

  Set> entrySet = treeMap.entrySet();

  for(Map.Entry entry : entrySet){

  String key = entry.getKey();

  Integer value = entry.getValue();

  System.out.println("TreeMap元素的key:"+key+",value:"+value);

  }

  //獲取所有的key:

  Set keySet = treeMap.keySet();

  for(String strKey:keySet){

  System.out.println("TreeMap集合中的key:"+strKey);

  }

  //獲取所有的value:

  Collection valueList = treeMap.values();

  for(Integer intValue:valueList){

  System.out.println("TreeMap集合中的value:" + intValue);

  }

  //獲取元素:

  //獲取集合內(nèi)元素key為"huangqiuping"的值

  Integer getValue = treeMap.get("huangqiuping");

  //獲取集合內(nèi)第一個(gè)元素

  String firstKey = treeMap.firstKey();

  //獲取集合內(nèi)最后一個(gè)元素

  String lastKey =treeMap.lastKey();

  //獲取集合內(nèi)的key小于"huangqiuping"的key

  String lowerKey =treeMap.lowerKey("huangqiuping");

  //獲取集合內(nèi)的key大于等于"huangqiuping"的key

  String ceilingKey =treeMap.ceilingKey("huangqiuping");

  //獲取集合的key從"a"到"huangqiuping"的元素

  SortedMap sortedMap =treeMap.subMap("a","my");

  //刪除元素:

  //刪除集合中key為"huangqiuping"的元素

  Integer removeValue = treeMap.remove("huangqiuping");

  //清空集合元素:

  treeMap.clear();

  //判斷方法:

  //判斷集合是否為空

  boolean isEmpty = treeMap.isEmpty();

  //判斷集合的key中是否包含"huangqiuping"

  boolean isContain = treeMap.containsKey("huangqiuping");

  }

  }

  TreeMap排序

  (1)使用元素自然排序

  在使用自然順序排序時(shí)候,需要區(qū)分兩種情況:一種是Jdk定義的對(duì)象,一種是自己定義的對(duì)象;

  public class SortedTest implements Comparable {

  private int age;

  public SortedTest(int age){

  this.age = age;

  }

  public int getAge() {

  return age;

  }

  public void setAge(int age) {

  this.age = age;

  }

  //自定義對(duì)象,實(shí)現(xiàn)compareTo(T o)方法:

  public int compareTo(SortedTest sortedTest) {

  int num = this.age - sortedTest.getAge();

  //為0時(shí)候,兩者相同:

  if(num==0){

  return 0;

  //大于0時(shí),傳入的參數(shù)小:

  }else if(num>0){

  return 1;

  //小于0時(shí),傳入的參數(shù)大:

  }else{

  return -1;

  }

  }

  }

  public class TreeMapTest {

  public static void main(String[] agrs){

  //自然順序比較

  naturalSort();

  }

  //自然排序順序:

  public static void naturalSort(){

  //第一種情況:Integer對(duì)象

  TreeMap treeMapFirst = new TreeMap();

  treeMapFirst.put(1,"huangqiuping");

  treeMapFirst.put(6,"huangqiuping");

  treeMapFirst.put(3,"huangqiuping");

  treeMapFirst.put(10,"huangqiuping");

  treeMapFirst.put(7,"huangqiuping");

  treeMapFirst.put(13,"huangqiuping");

  System.out.println(treeMapFirst.toString());

  //第二種情況:SortedTest對(duì)象

  TreeMap treeMapSecond = new TreeMap();

  treeMapSecond.put(new SortedTest(10),"huangqiuping");

  treeMapSecond.put(new SortedTest(1),"huangqiuping");

  treeMapSecond.put(new SortedTest(13),"huangqiuping");

  treeMapSecond.put(new SortedTest(4),"huangqiuping");

  treeMapSecond.put(new SortedTest(0),"huangqiuping");

  treeMapSecond.put(new SortedTest(9),"huangqiuping");

  System.out.println(treeMapSecond.toString());

  }

  }鄭州看婦科哪家醫(yī)院好 http://www.hnzzkd.com/

  在自然順序比較中,需要讓被比較的元素實(shí)現(xiàn)Comparable接口,否則在向集合里添加元素時(shí)報(bào):"java.lang.ClassCastException: com.huangqiuping.collection.map.SortedTest cannot be cast to java.lang.Comparable"異常;

  這是因?yàn)樵谡{(diào)用put()方法時(shí),會(huì)將傳入的元素轉(zhuǎn)化成Comparable類型對(duì)象,所以當(dāng)你傳入的元素沒有實(shí)現(xiàn)Comparable接口時(shí),就無法轉(zhuǎn)換,遍會(huì)報(bào)錯(cuò);

  (2)使用自定義比較器排序

  使用自定義比較器排序,需要在創(chuàng)建TreeMap對(duì)象時(shí),將自定義比較器對(duì)象傳入到TreeMap構(gòu)造方法中;

  自定義比較器對(duì)象,需要實(shí)現(xiàn)Comparator接口,并實(shí)現(xiàn)比較方法compare(To1,To2);

  使用自定義比較器排序的話,被比較的對(duì)象無需再實(shí)現(xiàn)Comparable接口了;

  public class SortedTest {

  private int age;

  public SortedTest(int age){

  this.age = age;

  }

  public int getAge() {

  return age;

  }

  public void setAge(int age) {

  this.age = age;

  }

  }

  public class SortedTestComparator implements Comparator {

  //自定義比較器:實(shí)現(xiàn)compare(To1,To2)方法:

  public int compare(SortedTest sortedTest1, SortedTest sortedTest2) {

  int num = sortedTest1.getAge() - sortedTest2.getAge();

  if(num==0){//為0時(shí)候,兩者相同:

  return 0;

  }else if(num>0){//大于0時(shí),后面的參數(shù)?。?/p>

  return 1;

  }else{//小于0時(shí),前面的參數(shù)?。?/p>

  return -1;

  }

  }

  }

  public class TreeMapTest {

  public static void main(String[] agrs){

  //自定義順序比較

  customSort();

  }

  //自定義排序順序:

  public static void customSort(){

  TreeMap treeMap = new TreeMap(new SortedTestComparator());

  treeMap.put(new SortedTest(10),"hello");

  treeMap.put(new SortedTest(21),"my");

  treeMap.put(new SortedTest(15),"name");

  treeMap.put(new SortedTest(2),"is");

  treeMap.put(new SortedTest(1),"huangqiuping");

  treeMap.put(new SortedTest(7),"world");

  System.out.println(treeMap.toString());

  }

  }


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI