标题翻译
How to iterate through HashMap in Java
问题
我想在Java中遍历哈希映射。我无法从我的代码中获取键。它给我一些不是我想要的随机类型的键。我的键是类类型。
我尝试了许多方法,但我不知道怎么样才能确切地获取这些键。当我拥有原始类型或包装类类型的键时,我可以获取这些键,但当我从用户定义的类中输入键时却不行。
英文翻译
i want to iterate through the has map in java. i am not able to get the keys from my code. it gives me random kind of keys that's not what i want. my keys are of class type.
i have tried many things but i don't know exactly how can i get the keys . i can get the keys when i have the keys of primitive or wrapper classes type but not when i am entering a key from user defined class.
答案1
得分: 0
> 在Java中通过HashMap进行迭代,打印所有的键和值:
map.forEach((key, value) -> {
System.out.println(key + " " + value);
});
注意: 我们使用了forEach
lambda表达式,需要Java 8,但非常清晰简洁。
英文翻译
> Iterating through HashMap in Java printing all keys and values together:
map.forEach((key, value) -> {
System.out.println(key + " " + value);
});
Note: we are using the forEach
lambda expression, which needs Java 8, but it is very clear & concise.
专注分享java语言的经验与见解,让所有开发者获益!
评论