LinkedHashMap是Java Collection Framework的一个通用类,它实现了Map接口。顾名思义,它是 HashMap 类的子类,使用双向 LinkedList 按插入顺序存储条目。它维护条目的键值对。 Key 是一个用于获取和接收与其关联的值的对象。因此,我们可以使用此键与“get()”方法一起从 LinkedHashMap 中获取所有值。本文的目的是解释打印 LinkedHashMap 所有值的不同方法。
获取LinkedHashMap所有值的Java程序
在直接跳到示例程序之前,我们先来了解一下LinkedHashMap的几个要点:
LinkedHashMap
正如我们之前讨论的,LinkedHashMap 类扩展了 HashMap 类来实现 Map 接口。因此,它可以使用 HashMap 类的所有方法并执行类似的操作。它将映射的元素按照插入的顺序存储在 LinkedList 中,因此,每当我们返回其元素时,它将按照插入顺序打印。
LinkedHashMap的一般语法如下:
语法
LinkedHashMap< TypeOfKey, TypeOfValue > nameOfMap = new LinkedHashMap<>();
在上面的语法中,
TypeOfKey:指定Keys的数据类型。
TypeOfValue:指定要存储在地图中的值的数据类型。
nameOfMap:为您的地图指定一个合适的名称。
现在,让我们创建 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 循环迭代直到可用值结束并提取值。另外,在每次迭代期间将“索引”增加 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 中所有值的不同方法。对于这个操作,我们已经看到了两个示例程序。
以上是如何在Java中获取LinkedHashMap的所有值?的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

记事本++7.3.1
好用且免费的代码编辑器