?? private Map<String, User> userList = new HashMap<String, User>();對userList進行了初始化userList.put("1", new User("1","yonghu1","121","[email protected]"));?? ??? ?userList.put("2", new User("2","yonghu2","122","[email protected]"));?? ??? ?userList.put("3", new User("3","yonghu3","123","[email protected]"));然后下面這些代碼什么意思Iterator<Map.Entry<String, User>> iterator = userList.entrySet().iterator();?? ??? ?while (iterator.hasNext()) {?? ??? ??? ?Map.Entry<String, User> entry = iterator.next();?? ??? ??? ?System.out.println(entry.getKey() + " " + entry.getValue().getUsername());?? ??? ?}
1 回答

大咪
TA貢獻785條經驗 獲得超332個贊
接口 Map.Entry<K,V>
所有已知實現類:
AbstractMap.SimpleEntry, AbstractMap.SimpleImmutableEntry
正在封閉接口:
Map<K,V>
public static interface Map.Entry<K,V>
映射項(鍵-值對)。Map.entrySet 方法返回映射的 collection 視圖,其中的元素屬于此類。獲得映射項引用的唯一 方法是通過此 collection 視圖的迭代器來實現。這些 Map.Entry 對象僅 在迭代期間有效;更確切地講,如果在迭代器返回項之后修改了底層映射,則某些映射項的行為是不確定的,除了通過 setValue 在映射項上執行操作之外。?
getValue
V getValue()
返回與此項對應的值。如果已經從底層映射中移除了映射關系(通過迭代器的 remove 操作),則此調用的結果是不確定的。
返回:
與此項對應的值
拋出:
IllegalStateException - 如果已經從底層映射中移除了該項,則實現可能,但不要求,拋出此異常。
添加回答
舉報
0/150
提交
取消