java map返回值_Java NavigableMap ceilingKey()用法及代码示例

Java中NavigableMap接口的ceilingKey()方法用于返回大于或等于给定键的最小键;如果不存在这样的键,则返回null。

用法:

K ceilingKey(K key)

其中,K是此映射容器维护的Key的类型。

参数:它接受单个参数Key,该参数是要映射的 key ,并且属于此collection接受的 key 类型。

返回值:返回大于或等于给定键的最小键;如果不存在这样的键,则返回null。

以下程序说明了Java中的ceilingKey()方法:

程序1:当键为整数时。

// Java code to demonstrate the working of

// ceilingKey()  method

import java.io.*;

import java.util.*;

public class GFG {

public static void main(String[] args)

{

// Declaring the NavigableMap of Integer and String

NavigableMap tmmp = new TreeMap<>();

// assigning the values in the NavigableMap

// using put()

tmmp.put(2, "two");

tmmp.put(7, "seven");

tmmp.put(3, "three");

// Use of ceilingKey()

// returns 7( next greater key)

System.out.println("The next greater key of 6 is : "

+ tmmp.ceilingKey(6));

// returns "null" as no value present

// greater than or equal to number

System.out.println("The next greater key of 3 is : "

+ tmmp.ceilingKey(3));

}

}

输出:

The next greater key of 6 is : 7

The next greater key of 3 is : 3

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

// Java code to demonstrate the working of

// ceilingKey()  method

import java.io.*;

import java.util.*;

public class GFG {

public static void main(String[] args)

{

// Declaring the NavigableMap of Integer and String

NavigableMap tmmp = new TreeMap<>();

// assigning the values in the NavigableMap

// using put()

tmmp.put("one", "two");

tmmp.put("six", "seven");

tmmp.put("two", "three");

// Use of ceilingKey()

// returns 7( next greater key)

System.out.println("The next greater key of five is : "

+ tmmp.ceilingKey("five"));

// returns "null" as no value present

// greater than or equal to number

System.out.println("The next greater key of six is : " +

tmmp.ceilingKey("six"));

}

}

输出:

The next greater key of five is : one

The next greater key of six is : six

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值