Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Android中打印LinkedHashMap中的所有密钥_Java_Android_Hashmap_Linkedhashmap - Fatal编程技术网

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因为您在这里看起来比较新,请不要忘记标记对解决问题最有帮助的答案。