Java 如何在Map中找到具有最大值的条目

Java 如何在Map中找到具有最大值的条目

在Java中,Map是一个对象,它以键值对的形式存储其元素。键是用于获取和接收与其关联的值的对象。键必须是唯一的,但与其关联的值根据我们使用的Map类的类型可以重复。

有几种方法可以在Java map中找到具有最大键的条目,并且这些方法也取决于我们正在使用的Map类的类型。在本文中,我们将讨论如何在HashMap和TreeMap类中找到具有最大键的条目。

在Map中查找具有最大键的Java程序

在本节中,我们将讨论HashMap和TreeMap类的基础知识,以及获取具有最大键的条目的示例程序。

HashMap类

这是一个实现了Map接口的通用类,因此它具有访问该接口所有方法的权限。请注意,它没有任何自己的附加方法。不允许重复值,但可以存储null值和键。在存储映射时使用哈希表。

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类

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。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程