Java HashMap 像 ArrayList 一样放入增强的 for 循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27867598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 05:10:46  来源:igfitidea点击:

Java HashMap put in an enhanced for loop just like an ArrayList

javafor-looparraylisthashmap

提问by SwordW

For example, I can loop an ArrayListlike this

例如,我可以ArrayList像这样循环

for (String temp : arraylist)

Can I loop a HashMap by using the similar method?

我可以使用类似的方法循环 HashMap 吗?

采纳答案by Eran

You can iterate over the keys, entries or values.

您可以迭代键、条目或值。

for (String key : map.keySet()) {
    String value = map.get(key);
}

for (String value : map.values()) {
}

for (Map.Entry<String,String> entry : map.entrySet()) {
    String key = entry.getKey();
    String value = entry.getValue();
}

This is assuming your map has String keys and String values.

这是假设您的地图具有字符串键和字符串值。

回答by Christopher Rivera

you can do

你可以做

for (String key : hashmap.keySet()) {
    String value = hashmap.get(key);
}

回答by Mureinik

You can't directly loop a Maplike that in Java.

你不能Map在 Java 中直接循环 a 。

You can, however, loop the keys:

但是,您可以循环键:

for (SomeKeyObject key : map.keySet())

The values:

价值:

for (SomeValueObject value : map.values())

Or even its entries:

甚至它的条目:

for (Map.Entry<SomeKeyObject, SomeValueObject> entry : map.entrySet())

回答by huseyin tugrul buyukisik

public class IterableHashMap<T,U> extends HashMap implements Iterable
    {

        @Override
        public Iterator iterator() {
            // TODO Auto-generated method stub

            return this.keySet().iterator();
        }



    }



    public static void main(String[] args) {

        testA a=new testA();
        IterableHashMap test=a.new IterableHashMap<Object,Object>();
        for(Object o:test)
        {

        }
    }

回答by Tim Hallyburton

You can do it by using an Iteratorhttp://docs.oracle.com/javase/7/docs/api/java/util/Iterator.html.

您可以使用Iterator http://docs.oracle.com/javase/7/docs/api/java/util/Iterator.html 来完成

    HashMap<String, String> yourHashMap = new HashMap<>();
    Iterator<Map.Entry<String, String>> it = yourHashMap.entrySet().iterator();
    while(it.hasNext()){
        it.next();
        System.out.println(yourHashMap.get(it));
    }

At first sight it might be tempting to use a for-loop instead of an Iterator, but

you will need an Iterator if you want to modify the elements in your HashMap while iterating over them!

When using a for-loop you cannot remove elements from your map while

乍一看,使用 for 循环而不是 Iterator 可能很诱人,但是

如果您想在迭代它们时修改 HashMap 中的元素,则需要一个 Iterator!

使用 for 循环时,您无法从地图中删除元素,而

 it.remove()

would work well in the above example.

在上面的例子中效果很好。

回答by Rahul Jangra

Use this

用这个

map.forEach((k, v) -> System.out.printf("%s %s%n", k, v));