[英]how to iterate Map<String, List<Map<String, Object>>> in java?
我有一個方法返回Map<String, List<Map<String, Object>>>
如何迭代像Map<String, List<Map<String, Object>>>
這樣的Map<String, List<Map<String, Object>>>
你會像迭代其他所有地圖一樣迭代它。 您只需要在迭代循環中“遞歸地”迭代 List 和另一個 Aap。
一種可能的方式(沒有功能代碼):
Map<String, List<Map<String, Object>>> yourMap = ...;
//iterate over outer map
for(Map.Entry<String, List<Map<String, Object>>> topEntry : yourMap.entrySet()) {
String topKey = topEntry.getKey();
//iterate over list
for(Map<String, Object> innerMap : topEntry.getValue()) {
//iterate over inner map
for(Map.Entry<String, Object> innerEntry : innerMap.entrySet()) {
String innerKey = innerEntry.getKey();
Object innerValue = innerEntry.getValue();
}
}
}
如果您需要知道列表索引,您還可以將遍歷 List 的 foreach 循環切換為 fori 循環 [ for(int i=0; i<list.size(); i++)
]。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.