在Android中的LinkedHashMap中打印所有键

use*_*397 1 java android hashmap linkedhashmap

LinkedHashMap我的代码中有一个:

 protected LinkedHashMap<String, String> profileMap;
Run Code Online (Sandbox Code Playgroud)

我想要打印出来的所有按键profileMap.如何使用Iterator或循环?

old*_*inb 8

你应该迭代Set来自Map.keySet:

for (final String key : profileMap.keySet()) {
  /* print the key */
}
Run Code Online (Sandbox Code Playgroud)

Iterator明确使用,

final Iterator<String> cursor = profileMap.keySet().iterator();
while (cursor.hasNext()) {
  final String key = cursor.next();
  /* print the key */
}
Run Code Online (Sandbox Code Playgroud)

但是,两者在编译时或多或少相同.