Java中NavigableMap floorEntry()方法

Java中NavigableMap floorEntry()方法

Java中NavigableMap接口的floorEntry()方法用于返回与小于或等于给定键的最大键相关联的键值映射,如果没有这样的键则返回null。

语法 :

Map.Entry<K, V> floorEntry(K key)

其中,key是此映射维护的键。

参数 :key – 键

返回值 : 返回一个具有小于或等于key的最大键的条目,如果没有这样的键则返回null。

下面的程序说明了Java中的floorEntry()方法:

程序1 : 当键为整数时。

// Java代码以展示floorEntry()方法的工作原理

import java.io.*;
import java.util.*;

public class GFG {

    public static void main(String[] args)
    {

        // 声明NavigableMap of Integer and String
        NavigableMap<Integer, String> nmmp = new TreeMap<>();

        // 使用put()在NavigableMap中指定值
        nmmp.put(2, "two");
        nmmp.put(7, "seven");
        nmmp.put(3, "three");

        System.out.println("Greatest key associated with the mapping is : "
                                   + nmmp.floorEntry(2));
    }
}
Greatest key associated with the mapping is : 2=two

程序2 : 当键为字符串时。

// Java代码以展示floorEntry()方法的工作原理
  
import java.io.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // 声明NavigableMap of Integer and String
        NavigableMap<String, String> tmmp = new TreeMap<>();
  
        // 使用put()在NavigableMap中指定值
        tmmp.put("one", "two");
        tmmp.put("six", "seven");
        tmmp.put("two", "three");
  
        System.out.println("Greatest key associated with the mapping is : "
                           + tmmp.floorEntry("one"));
    }
}
Greatest key associated with the mapping is : one=two

参考 : https://docs.oracle.com/javase/10/docs/api/java/util/NavigableMap.html#floorEntry(K)

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程