Java TreeMap ceilingEntry() 方法



描述

Java TreeMap ceilingEntry(K key) 方法用於返回與大於或等於給定鍵的最小鍵關聯的鍵值對映,如果不存在這樣的鍵,則返回 null。

宣告

以下是 java.util.TreeMap.ceilingEntry() 方法的宣告。

public Map.Entry<K,V> ceilingEntry(K key)

引數

key − 要匹配的鍵。

返回值

方法呼叫返回一個條目,該條目具有大於或等於 key 的最小鍵,如果不存在這樣的鍵,則返回 null。

異常

  • ClassCastException − 如果指定的鍵無法與當前對映中的鍵進行比較,則丟擲此異常。

  • NullPointerException − 如果指定的鍵為 null 且此對映使用自然排序,或者其比較器不允許 null 鍵,則丟擲此異常。

從 Integer、Integer 對的 TreeMap 獲取上限鍵值條目示例

以下示例演示瞭如何使用 Java TreeMap ceilingEntry() 方法獲取與大於或等於給定鍵的最小鍵關聯的鍵值對映。我們建立了一個 Integer、Integer 對的 TreeMap 物件。然後添加了一些條目,並使用 ceilingEntry() 列印兩個相關條目。

package com.tutorialspoint;

import java.util.TreeMap;

public class TreeMapDemo {
   public static void main(String[] args) {
      
      // creating tree map 
      TreeMap<Integer, Integer> treemap = new TreeMap<>();

      // populating tree map
      treemap.put(2, 2);
      treemap.put(1, 1);
      treemap.put(3, 3);
      treemap.put(6, 6);
      treemap.put(5, 5);

      System.out.println("Ceiling entry for 4: "+ treemap.ceilingEntry(4));
      System.out.println("Ceiling entry for 5: "+ treemap.ceilingEntry(5));
   }
}

輸出

讓我們編譯並執行以上程式,這將產生以下結果。

Ceiling entry for 4: 5=5
Ceiling entry for 5: 5=5

從 Integer、String 對的 TreeMap 獲取上限鍵值條目示例

以下示例演示瞭如何使用 Java TreeMap ceilingEntry() 方法獲取與大於或等於給定鍵的最小鍵關聯的鍵值對映。我們建立了一個 Integer、String 對的 TreeMap 物件。然後添加了一些條目,並使用 ceilingEntry() 列印兩個相關條目。

package com.tutorialspoint;

import java.util.TreeMap;

public class TreeMapDemo {
   public static void main(String[] args) {
      
      // creating tree map 
      TreeMap<Integer, String> treemap = new TreeMap<>();

      // 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("Ceiling entry for 4: "+ treemap.ceilingEntry(4));
      System.out.println("Ceiling entry for 5: "+ treemap.ceilingEntry(5));
   }
}

輸出

讓我們編譯並執行以上程式,這將產生以下結果。

Ceiling entry for 4: 5=five
Ceiling entry for 5: 5=five

從 Integer、Object 對的 TreeMap 獲取上限鍵值條目示例

以下示例演示瞭如何使用 Java TreeMap ceilingEntry() 方法獲取與大於或等於給定鍵的最小鍵關聯的鍵值對映。我們建立了一個 Integer、Student 對的 TreeMap 物件。然後添加了一些條目,並使用 ceilingEntry() 列印兩個相關條目。

package com.tutorialspoint;

import java.util.TreeMap;

public class TreeMapDemo {
   public static void main(String[] args) {
      
      // creating tree map 
      TreeMap<Integer, Student> treemap = new TreeMap<>();

      // populating tree map
      treemap.put(2, new Student(2, "Robert"));
      treemap.put(1, new Student(1, "Julie"));  
      treemap.put(3, new Student(3, "Adam"));
      treemap.put(6, new Student(5, "Julia"));
      treemap.put(5, new Student(5, "Tom"));

      System.out.println("Ceiling entry for 4: "+ treemap.ceilingEntry(4));
      System.out.println("Ceiling entry for 5: "+ treemap.ceilingEntry(5));
   }
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
   @Override
   public boolean equals(Object obj) {
      if(obj == null) return false;
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }
}

輸出

讓我們編譯並執行以上程式,這將產生以下結果。

Ceiling entry for 4: 5=[ 5, Tom ]
Ceiling entry for 5: 5=[ 5, Tom ]
java_util_treemap.htm
廣告

© . All rights reserved.