java.util.sortedmap_Java SortedMap lastKey()用法及代码示例

Java中SortedMap接口的lastKey()方法用于返回此映射中当前的最后一个或最大键。

用法:

K lastKey()

其中,K是此Set维护的 key 的类型。

参数:此函数不接受任何参数。

返回值:返回当前Map中的最后一个或最大的键

异常:如果此映射为空,则抛出NoSuchElementException。

以下示例程序旨在说明上述方法:

程序1:

// A Java program to demonstrate

// working of SortedSet

import java.util.*;

public class Main {

public static void main(String[] args)

{

// Create a TreeSet and inserting elements

SortedMap mp = new TreeMap<>();

// Adding Element to SortedSet

mp.put(1, "One");

mp.put(2, "Two");

mp.put(3, "Three");

mp.put(4, "Four");

mp.put(5, "Five");

// Returning the last key from the map

System.out.print("Last Key in the map is : "

+ mp.lastKey());

}

}

输出:

Last Key in the map is : 5

程序2:

// A Java program to demonstrate

// working of SortedSet

import java.util.*;

public class Main {

public static void main(String[] args)

{

// Create a TreeSet and inserting elements

SortedMap mp = new TreeMap<>();

// Adding Element to SortedSet

mp.put("One", "Geeks");

mp.put("Two", "For");

mp.put("Three", "Geeks");

mp.put("Four", "Code");

mp.put("Five", "It");

// Returning the last key from the map

System.out.print("Last Key in the map is : "

+ mp.lastKey());

}

}

输出:

Last Key in the map is : Two

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值