Java TreeMap lowerKey() 方法



描述

Java TreeMap lowerKey(K key) 方法用於返回嚴格小於給定鍵的最大鍵,如果不存在這樣的鍵,則返回 null。

宣告

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

public K lowerKey(K key)

引數

key − 這是要匹配的鍵。

返回值

方法呼叫返回小於 key 的最大鍵,如果不存在這樣的鍵,則返回 null。

異常

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

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

基於給定鍵從整數、整數對的 TreeMap 獲取較小鍵的示例

以下示例演示了 Java TreeMap lowerKey() 方法的使用,以獲取對映中小於給定鍵的最大鍵。我們建立了一個整數、整數對的 TreeMap 物件。然後添加了一些條目,並使用 lowerKey() 列印與給定鍵相關的鍵。

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);

      // getting greatest key for key 4          
      System.out.println("Checking Keys of the map");
      System.out.println("Key is: "+ treemap.lowerKey(3));
   }     
}

輸出

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

Checking values of the map
Value is: 2

基於給定鍵從整數、字串對的 TreeMap 獲取較小鍵的示例

以下示例演示了 Java TreeMap lowerKey() 方法的使用,以獲取對映中小於給定鍵的最大鍵。我們建立了一個整數、字串對的 TreeMap 物件。然後添加了一些條目,並使用 lowerKey() 列印與給定鍵相關的鍵。

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");

      // getting greatest key for key 4          
      System.out.println("Checking Keys of the map");
      System.out.println("Key is: "+ treemap.lowerKey(3));
   }     
}

輸出

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

Checking Keys of the map
Key is: 2

基於給定鍵從整數、物件對的 TreeMap 獲取較小條目的示例

以下示例演示了 Java TreeMap lowerKey() 方法的使用,以獲取對映中小於給定鍵的最大鍵。我們建立了一個整數、Student 物件對的 TreeMap 物件。然後添加了一些條目,並使用 lowerKey() 列印與給定鍵相關的鍵。

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(6, "Julia"));
      treemap.put(5, new Student(5, "Tom"));

      // getting greatest key for key 4          
      System.out.println("Checking Keys of the map");
      System.out.println("Key is: "+ treemap.lowerKey(3));
   }     
}
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);
   }
}

輸出

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

Checking Keys of the map
Key is: 2
java_util_treemap.htm
廣告