Java 如何获取LinkedHashMap的所有值
LinkedHashMap是Java集合框架的一个泛型类,实现了Map接口。顾名思义,它是HashMap类的一个子类,并使用一个双向链表来按插入顺序存储条目。它维护了Key-Value对的条目。Key是用来获取和接收与之关联的值的对象。因此,我们可以使用这个键以及’get()’方法从LinkedHashMap中获取所有的值。本文的目的是解释打印LinkedHashMap所有值的不同方法。
在Java程序中获取LinkedHashMap的所有值
在直接跳转到示例程序之前,让我们先了解一些关于LinkedHashMap的更多细节:
LinkedHashMap
我们之前已经讨论过,LinkedHashMap类扩展了HashMap类来实现Map接口。因此,它可以使用所有方法,并且可以执行与HashMap类相似的操作。它将地图的元素存储在LinkedList中,顺序为插入顺序,因此,每当我们返回其元素时,它将按照插入顺序打印出来。
LinkedHashMap的通用语法如下:
语法
LinkedHashMap< TypeOfKey, TypeOfValue > nameOfMap = new LinkedHashMap<>();
在上面的语法中,
TypeOfKey :指定Key的数据类型。
TypeOfValue :指定要存储在map中的值的数据类型。
nameOfMap :给你的map一个合适的名称。
现在,让我们创建Java程序来打印LinkedHashMap中的所有值。
示例1
以下示例演示了如何使用for-each循环从LinkedHashMap获取所有值。
方法
- 首先,导入’java.util’包,以便我们可以访问Map的功能。
-
然后,创建一个LinkedHashMap,并使用内置的’put()’方法存储指定类型的几个元素。
-
接下来,初始化一个’index’变量来指定索引。
-
现在,使用for-each循环遍历LinkedHashMap的键,并在每次迭代时将索引加1。同时,使用’get()’方法打印值。
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 items from LinkedHashMap :");
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()) {
// printing the result
System.out.println("The value at index " + index + " from LinkedHashMap is: " + LinkHmap.get(unKey));
index++; // incrementing the index
}
}
}
输出
All items from LinkedHashMap :
Item: TShirt, Quantity: 59
Item: Trouser, Quantity: 60
Item: Shirt, Quantity: 45
Item: Watch, Quantity: 230
Item: Shoes, Quantity: 55
The value at index 0 from LinkedHashMap is: 59
The value at index 1 from LinkedHashMap is: 60
The value at index 2 from LinkedHashMap is: 45
The value at index 3 from LinkedHashMap is: 230
The value at index 4 from LinkedHashMap is: 55
示例2
在下面的示例中,我们将使用迭代器接口来获取LinkedHashMap的所有值。
方法
- 按照上一个示例的前两个步骤进行操作。
-
定义一个迭代器并将LinkedHashMap的值存储到其中。
-
然后,初始化一个’index’变量来指定索引。
-
使用while循环迭代至可用值的末尾,并提取值。同时,在每次迭代时将’index’增加1。
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
System.out.println("All items from LinkedHashMap :");
for (String unKey : LinkHmap.keySet()) {
System.out.println("Item: " + unKey + ", Quantity: " + LinkHmap.get(unKey));
}
// Defining an iterator
Iterator<Integer> iter = LinkHmap.values().iterator();
// Initializing the index
int index = 0;
// iterating using while loop
while (iter.hasNext()) {
// to get the next value and increment the index
System.out.println("The value at index " + index + " from LinkedHashMap is: " + iter.next());
index++;
}
}
}
输出
All items from LinkedHashMap :
Item: TShirt, Quantity: 59
Item: Trouser, Quantity: 60
Item: Shirt, Quantity: 45
Item: Watch, Quantity: 230
Item: Shoes, Quantity: 55
The value at index 0 from LinkedHashMap is: 59
The value at index 1 from LinkedHashMap is: 60
The value at index 2 from LinkedHashMap is: 45
The value at index 3 from LinkedHashMap is: 230
The value at index 4 from LinkedHashMap is: 55
结论
在本文中,我们学习了LinkedHashMap以及如何创建一个LinkedHashMap。本文的目的是找到从LinkedHashMap打印所有值的不同方法。为此,我们看到了两个示例程序。