如何在 Java Map 中查找具有最大值的 Entry
在 Java 中,Map 是一个以键值对存储元素的对象。键是一个用于获取和接收与其关联的值的对象。键必须是唯一的,但与其关联的值可能会重复,具体取决于我们使用的 Map 类类型。
有几种方法可以在 Java Map 中查找具有最大键的条目,这些方法也取决于我们正在使用的 Map 类类型。在本文中,我们将讨论如何在 HashMap 和 TreeMap 类中查找具有最大键的条目。
Java 程序用于查找 Map 中具有最大键的条目
在本节中,我们将讨论 HashMap 和 TreeMap 类的基础知识以及获取具有最大键条目的示例程序。
HashMap 类
它是一个实现 Map 接口的泛型类,因此它可以访问该接口的所有方法。请注意,它本身没有任何额外的方法。不允许重复值,但是,我们可以存储空值和键。在存储映射时使用哈希表。
HashMap 的通用语法如下
语法
HashMap<TypeOfKey, TypeOfValue> nameOfMap = new HashMap<>();
示例 1
以下示例说明了如何在 Java HashMap 中获取具有最大键的条目。
方法
我们的第一步是导入“java.util”的必要包,以便我们可以访问 Map 的功能。
然后,定义一个 HashMap 并使用内置方法“put()”存储一些指定类型的元素。
初始化两个变量以存储最大键的条目。
现在,在 for-each 循环内使用内置方法“entrySet()”逐个检索 HashMap 的条目,然后使用 if 块获取最大条目。
最后,打印结果并退出。
import java.util.Map;
import java.util.HashMap;
public class LargestKey {
public static void main(String[] args) {
// Creating a HashMap
Map<Integer, Integer> cart = new HashMap<>();
// Adding elements in the cart map
cart.put(10, 400);
cart.put(20, 300);
cart.put(30, 150);
cart.put(40, 200);
cart.put(50, 250);
// printing all the elements of cart map
System.out.println("All elements in the map: " + cart);
// variables to store the largest key and its value
int largestKey = 0;
int largestValue = 0;
// Loop over all the entries in the Map
for (Map.Entry<Integer, Integer> entry : cart.entrySet()) {
// Get the key and value of the current entry
int key = entry.getKey();
int value = entry.getValue();
// Comparing current key with the current largest key
if (key > largestKey) {
// Update the largest key and its value
largestKey = key;
largestValue = value;
}
}
// Printing the entry with the largest key
System.out.println("The entry with the largest key is: [ Quantity: " + largestKey + ", Price: " + largestValue + "]");
}
}
输出
All elements in the map: {50=250, 20=300, 40=200, 10=400, 30=150}
The entry with the largest key is: [ Quantity: 50, Price: 250]
TreeMap 类
它是 Java 集合框架的一个类,它实现了 NavigableMap 接口。它以树形结构存储映射的元素,从而提供了一种高效的替代方案,以排序的方式存储键值对。
TreeMap 的通用语法如下
语法
TreeMap<TypeOfKey, TypeOfValue> nameOfMap = new TreeMap<>();
示例 2
以下示例演示了如何在 Java TreeMap 中获取具有最大键的条目。
方法
首先,导入“java.util”所需的包。
然后,创建一个 TreeMap 并使用内置方法“put()”存储一些指定类型的元素。
如前所述,TreeMap 以排序的方式存储其元素,因此最后一个条目将是最大的。因此,使用内置方法“lastEntry()”存储最大键的条目。
现在,从条目中检索最大键及其对应的值。
最后,打印结果并退出。
import java.util.Map;
import java.util.TreeMap;
public class LargestKey {
public static void main(String[] args) {
// Creating a TreeMap
TreeMap<Integer, Integer> cart = new TreeMap<>();
// Adding elements in the cart map
cart.put(10, 400);
cart.put(20, 300);
cart.put(30, 150);
cart.put(40, 200);
cart.put(50, 250);
// printing all the elements of cart map
System.out.println("All elements in the map: " + cart);
// Getting the last entry in the cart map
Map.Entry<Integer, Integer> entry = cart.lastEntry();
// Get the key and value of the entry
int largestKey = entry.getKey();
int largestValue = entry.getValue();
// Printing the entry with the largest key
System.out.println("The entry with the largest key is: [ Quantity: " + largestKey + ", Price: " + largestValue + "]");
}
}
输出
All elements in the map: {10=400, 20=300, 30=150, 40=200, 50=250}
The entry with the largest key is: [ Quantity: 50, Price: 250]
结论
我们从定义 Java Map 开始本文,在下一节中,我们讨论了两种从 Java Map 中查找具有最大键的条目方法。此外,我们还了解了一些 Map 类(如 HashMap 和 TreeMap)的基础知识。
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP