如何在Java中迭代遍历HashMap。

huangapple 未分类评论59阅读模式
标题翻译

How to iterate through HashMap in Java

问题

我想在Java中遍历哈希映射。我无法从我的代码中获取键。它给我一些不是我想要的随机类型的键。我的键是类类型。

我尝试了许多方法,但我不知道怎么样才能确切地获取这些键。当我拥有原始类型或包装类类型的键时,我可以获取这些键,但当我从用户定义的类中输入键时却不行。

英文翻译

my code loook at the out put

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.

huangapple
  • 本文由 发表于 2020年1月30日 18:52:43
  • 转载请务必保留本文链接:https://java.coder-hub.com/59984320.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定