java floor()_Java TreeMap floorEntry()用法及代码示例

java.util.TreeMap。floorEntry()方法用于返回与小于或等于给定键的最大键关联的键值映射,如果没有这样的键,则返回null。

用法:

tree_map.floorEntry(K key)

参数:此方法在映射时需要一个参数键进行匹配。

返回值:此方法返回最大 key 小于或等于key的条目,如果没有这样的 key ,则返回null。

异常:

ClassCastException:如果指定的键无法与映射中当前的键进行比较,则抛出此异常。

NullPointerException :如果指定键为null且此映射使用自然顺序,或者其比较器不允许使用null键,则抛出此异常。

范例1:有钥匙时

Java

// Java program to illustrate

// TreeMap floorEntry() method

import java.util.*;

public class GFG {

public static void main(String[] args)

{

// Creating an empty TreeMap

TreeMap treemap

= new TreeMap();

// Mapping string values to int keys

treemap.put(20, "Twenty");

treemap.put(10, "Ten");

treemap.put(13, "Thirteen");

treemap.put(60, "Sixty");

treemap.put(50, "Fifty");

System.out.println("The greatest key-value less than 18 is:"

+ treemap.floorEntry(18));

}

}

输出

The greatest key-value less than 18 is:13=Thirteen

范例2:当没有这样的钥匙

Java

// Java program to illustrate

// TreeMap floorEntry() method

import java.util.TreeMap;

public class GFG {

public static void main(String args[])

{

// Creating an empty TreeMap

TreeMap treemap

= new TreeMap();

// Mapping string values to int keys

treemap.put(10, "Akash");

treemap.put(20, "Pratik");

treemap.put(30, "Vaibhav");

treemap.put(40, "Sagar");

treemap.put(50, "Abhishek");

// Printing floor entry

System.out.println("The greatest key-value less than 5 is:"

+ treemap.floorEntry(5));

}

}

输出

The greatest key-value less than 5 is:null

注意读者!现在不要停止学习。以student-friendly的价格掌握Java和Java集合基础知识课程中所有重要的Java和集合概念,并做好行业准备。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值