Java 在Android中打印LinkedHashMap中的所有密钥
我的代码中有一个Java 在Android中打印LinkedHashMap中的所有密钥,java,android,hashmap,linkedhashmap,Java,Android,Hashmap,Linkedhashmap,我的代码中有一个LinkedHashMap: protected LinkedHashMap<String, String> profileMap; 受保护的LinkedHashMap profileMap; 我想打印profileMap中的所有键。如何使用迭代器或循环进行此操作?您应该从以下位置迭代集合: 显式使用迭代器 final Iterator<String> cursor = profileMap.keySet().iterator(); while (c
LinkedHashMap
:
protected LinkedHashMap<String, String> profileMap;
受保护的LinkedHashMap profileMap;
我想打印
profileMap
中的所有键。如何使用迭代器
或循环进行此操作?您应该从以下位置迭代集合
:
显式使用迭代器
final Iterator<String> cursor = profileMap.keySet().iterator();
while (cursor.hasNext()) {
final String key = cursor.next();
/* print the key */
}
final Iterator cursor=profileMap.keySet().Iterator();
while(cursor.hasNext()){
最后一个字符串键=cursor.next();
/*打印钥匙*/
}
但是,在编译时,两者或多或少是相同的。您应该从以下位置迭代
集合
:
显式使用迭代器
final Iterator<String> cursor = profileMap.keySet().iterator();
while (cursor.hasNext()) {
final String key = cursor.next();
/* print the key */
}
final Iterator cursor=profileMap.keySet().Iterator();
while(cursor.hasNext()){
最后一个字符串键=cursor.next();
/*打印钥匙*/
}
但是,在编译时,两者或多或少是相同的。您可以迭代
映射条目
,并且可以根据您的选择选择打印e.getKey()
或e.getValue()
for(Map.Entry<String, String> e : map.entrySet()) {
System.out.println(e.getKey());
}
for(Map.Entry e:Map.entrySet()){
System.out.println(e.getKey());
}
您可以遍历映射条目
,并根据您的选择选择打印e.getKey()
或e.getValue()
for(Map.Entry<String, String> e : map.entrySet()) {
System.out.println(e.getKey());
}
for(Map.Entry e:Map.entrySet()){
System.out.println(e.getKey());
}
但是如果你只对钥匙感兴趣。。。只需使用keySet(),但如果您只对键感兴趣。。。只需使用keySet()似乎是的完全复制似乎是的完全复制,您就可以摆脱所有的决赛。同样对于迭代器,您也可以使用for循环(更好,因为它限制了光标的范围)。@SteveKuo当然您可以删除final
,但我看不出原因——尤其是如果只打印键。如果出于任何原因,他需要字段是可变的,他可以删除final
,因为这只是一个模板。@user268397因为您在这里看起来比较新,请不要忘记标记对解决问题最有帮助的答案。您可以删除所有的final。同样对于迭代器,您也可以使用for循环(更好,因为它限制了光标的范围)。@SteveKuo当然您可以删除final
,但我看不出原因——尤其是如果只打印键。如果出于任何原因,他需要字段是可变的,他可以删除final
,因为这只是一个模板。@user268397因为您在这里看起来比较新,请不要忘记标记对解决问题最有帮助的答案。