Java 如何通过索引从LinkedHashMap获取值
LinkedHashMap是Java集合框架的一个泛型类,实现了Map接口。顾名思义,它是HashMap类的子类,并使用双向链表来按插入顺序存储条目。但是,LinkedHashMap不会进行任何类型的索引来存储条目,因此,使用索引从LinkedHashMap中获取指定的值是一个相当大的挑战。本文的目的是解释如何使用索引从LinkedHashMap中获取值。
在Java中通过索引从LinkedHashMap获取值的程序
在直接跳转到示例程序之前,让我们先了解一些关于LinkedHashMap的更多细节:
LinkedHashMap
如前所述,LinkedHashMap类扩展了HashMap类以实现Map接口。因此,它可以使用所有方法,并执行与HashMap类相似的操作。它维护键值对条目及其插入到映射中的顺序。键是用于获取和接收与其关联的值的对象。此外,每当我们返回它的元素时,它将按插入顺序打印。
LinkedHashMap的一般语法如下:
语法
LinkedHashMap< TypeOfKey, TypeOfValue > nameOfMap = new LinkedHashMap<>();
在上面的语法中, TypeOfKey :指定键的数据类型。 TypeOfValue :指定将要存储在映射中的值的数据类型。 nameOfMap :为您的映射给一个合适的名称。 现在,让我们创建Java程序来通过索引从LinkedHashMap中查找指定的值。
示例1
在下面的示例中,我们将使用迭代器接口通过索引从LinkedHashMap中获取指定的值。
方法
- 首先,导入“java.util”包,以便我们可以访问Map的功能。
- 然后,使用内置方法“put()”创建一个LinkedHashMap并存储指定类型的一些元素。
- 定义一个迭代器并将LinkedHashMap的值存储到迭代器中。
- 现在,初始化一个计数器变量以迭代到所需的索引,以及另一个名为“value”的变量来存储该索引的值。
- 使用while循环从指定索引提取值。
import java.util.*;
public class Example1 {
public static void main(String[] args) {
// Creating a LinkedHashMap
LinkedHashMap<String, Integer> LinkHmap = new LinkedHashMap<>();
// storing elements to the map
LinkHmap.put("TShirt", 59);
LinkHmap.put("Trouser", 60);
LinkHmap.put("Shirt", 45);
LinkHmap.put("Watch", 230);
LinkHmap.put("Shoes", 55);
// to print all entries
System.out.println("All entries from LinkedHashMap: " + LinkHmap);
// Defining an iterator
Iterator<Integer> iter = LinkHmap.values().iterator();
// Initializing a counter
int cntr = 0;
// Initializing a variable for storing the value
int value = 0;
// iterating using while loop
while (iter.hasNext() && cntr < 2) {
// to get the next value and increment the counter
value = iter.next();
cntr++;
}
// Printing the result
System.out.println("The value at index 2 from LinkedHashMap is: " + value);
}
}
输出
All entries from LinkedHashMap: {TShirt = 59, Trouser = 60, Shirt = 45, Watch = 230, Shoes = 55}
The value at index 2 from LinkedHashMap is: 60
示例2
这是另一个Java程序,演示了使用for-each循环通过索引从LinkedHashMap获取值的方法。
方法
- 按照前一个示例的前两个步骤进行。
-
然后,初始化一个’index’变量来指定所需的索引。
-
现在,采用一个for-each循环,它将遍历LinkedHashMap的键,并在每次迭代期间将索引增加1。
-
当索引增加到3时,if块将被执行并打印结果。
import java.util.*;
public class Example2 {
public static void main(String[] args) {
// Creating a LinkedHashMap
LinkedHashMap<String, Integer> LinkHmap = new LinkedHashMap<>();
// storing elements to the map
LinkHmap.put("TShirt", 59);
LinkHmap.put("Trouser", 60);
LinkHmap.put("Shirt", 45);
LinkHmap.put("Watch", 230);
LinkHmap.put("Shoes", 55);
// to print all entries
for (String unKey : LinkHmap.keySet()) {
System.out.println("Item: " + unKey + ", Quantity: " + LinkHmap.get(unKey));
}
// Initializing the index
int index = 0;
// iterating using for-each loop
for (String unKey : LinkHmap.keySet()) {
index++; // incrementing the counter
if(index == 3) { // printing the value at index 3
System.out.println("The value at index 3 from LinkedHashMap is: " + LinkHmap.get(unKey));
}
}
}
}
输出
Item: TShirt, Quantity: 59
Item: Trouser, Quantity: 60
Item: Shirt, Quantity: 45
Item: Watch, Quantity: 230
Item: Shoes, Quantity: 55
The value at index 3 from LinkedHashMap is: 45
结论
我们在本文中首先定义了Java LinkedHashMap类,然后在下一节中,我们讨论了两种通过索引从LinkedHashMap中查找值的方法。同时,我们也发现了如何创建一个LinkedHashMap集合。