遍历LinkedHashMap
时间: 2023-10-26 10:23:22 浏览: 251
遍历LinkedHashMap可以使用Iterator遍历或者使用foreach循环遍历。使用Iterator遍历LinkedHashMap的示例代码如下:
```
LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap();
linkedHashMap.put("a", "a");
linkedHashMap.put("b", "b");
linkedHashMap.put("c", "c");
Iterator<Map.Entry<String, String>> it = linkedHashMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String> entry = it.next();
System.out.println("key: " + entry.getKey() + " value: " + entry.getValue());
}
```
使用foreach循环遍历LinkedHashMap的示例代码如下:
```
LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap();
linkedHashMap.put("a", "a");
linkedHashMap.put("b", "b");
linkedHashMap.put("c", "c");
for (Map.Entry<String, String> entry : linkedHashMap.entrySet()) {
System.out.println("key: " + entry.getKey() + " value: " + entry.getValue());
}
```
以上是两种常用的遍历LinkedHashMap的方法。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *3* [java集合之LinkedHashMap](https://blog.csdn.net/qq_41820066/article/details/126978093)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *2* [如何遍历LinkedHashMap](https://blog.csdn.net/qq_40351478/article/details/87949852)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文