LinkedHashMap遍历
时间: 2023-08-22 13:11:22 浏览: 80
Java集合系列之LinkedHashMap源码分析
LinkedHashMap可以使用Iterator和foreach循环进行遍历。使用Iterator遍历LinkedHashMap的示例代码如下:
```java
LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap();
linkedHashMap.put("a", "a");
linkedHashMap.put("b", "b");
linkedHashMap.put("c", "c");
Iterator it = linkedHashMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
System.out.println("key:" + entry.getKey() + " value:" + entry.getValue());
}
```
使用foreach循环遍历LinkedHashMap的示例代码如下:
```java
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并输出每个键值对的key和value了。\[2\]
#### 引用[.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^v91^insertT0,239^v4^insert_chatgpt"}} ] [.reference_item]
- *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^v91^insertT0,239^v4^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文