📜  Java中的 TreeMap lowerEntry() 方法及示例

📅  最后修改于: 2022-05-13 01:55:31.109000             🧑  作者: Mango

Java中的 TreeMap lowerEntry() 方法及示例

Java.util.TreeMap类的lowerEntry()方法用于返回严格小于给定键的最大键关联的键值映射,如果没有这样的键,则返回null
句法:

public Map.Entry lowerEntry(K key)

参数:此方法将作为要找到下条目的参数。
返回值:此方法返回一个最大键小于 key的条目,如果没有这样的键,则返回null
异常:如果指定的键为空并且此映射使用自然排序,或者其比较器不允许空键,则此方法抛出NullPointerException
下面是说明lowerEntry()方法的示例
示例 1:

Java
// Java program to demonstrate
// lowerEntry() method
// for 
 
import java.util.*;
 
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
 
        try {
 
            // creating object of TreeMap
            TreeMap
                treemap = new TreeMap();
 
            // populating tree map
            treemap.put(1, "One");
            treemap.put(2, "Two");
            treemap.put(3, "Three");
            treemap.put(4, "Four");
            treemap.put(5, "Five");
 
            // printing the TreeMap
            System.out.println("TreeMap: " + treemap);
 
            // getting lowerEntry value for 3
            // using lowerEntry() method
            Map.Entry
                value
                = treemap.lowerEntry(3);
 
            // printing the value
            System.out.println("The lowerEntry value "
                               + " for 3: " + value);
        }
 
        catch (NullPointerException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}


Java
// Java program to demonstrate
// lowerEntry() method
// for NullPointerException
 
import java.util.*;
 
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
 
        try {
 
            // creating object of TreeMap
            TreeMap
                treemap = new TreeMap();
 
            // populating tree map
            treemap.put(1, "One");
            treemap.put(2, "Two");
            treemap.put(3, "Three");
            treemap.put(4, "Four");
            treemap.put(5, "Five");
 
            // printing the TreeMap
            System.out.println("TreeMap: " + treemap);
 
            // getting lowerEntry value for null
            // using lowerEntry() method
            System.out.println("Trying to get"
                               + " the lowerEntry value"
                               + " for value NULL");
 
            Map.Entry
                value
                = treemap.lowerEntry(null);
 
            // printing the value
            System.out.println("Value is: " + value);
        }
 
        catch (NullPointerException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}


输出:
TreeMap: {1=One, 2=Two, 3=Three, 4=Four, 5=Five}
The lowerEntry value  for 3: 2=Two

示例 2:对于NullPointerException

Java

// Java program to demonstrate
// lowerEntry() method
// for NullPointerException
 
import java.util.*;
 
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
 
        try {
 
            // creating object of TreeMap
            TreeMap
                treemap = new TreeMap();
 
            // populating tree map
            treemap.put(1, "One");
            treemap.put(2, "Two");
            treemap.put(3, "Three");
            treemap.put(4, "Four");
            treemap.put(5, "Five");
 
            // printing the TreeMap
            System.out.println("TreeMap: " + treemap);
 
            // getting lowerEntry value for null
            // using lowerEntry() method
            System.out.println("Trying to get"
                               + " the lowerEntry value"
                               + " for value NULL");
 
            Map.Entry
                value
                = treemap.lowerEntry(null);
 
            // printing the value
            System.out.println("Value is: " + value);
        }
 
        catch (NullPointerException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}
输出:
TreeMap: {1=One, 2=Two, 3=Three, 4=Four, 5=Five}
Trying to get the lowerEntry value for value NULL
Exception thrown : java.lang.NullPointerException