使用entrySet循环遍历hashmap

时间:2017-10-26 17:27:53

标签: java hashmap

我在自己的类中有一个hashMap在另一个文件中。

public class className {

private HashMap<String,String> myMap = new HashMap<String,String>();

}

我想循环遍历hashMap并获取密钥,如果我将值传递给它。我在使用entrySet时遇到了问题,因为我一直在阅读以下问题。 Stackoverflow Question

每个循环的代码是:

for(Map.Entry<String, String> entry : className.entrySet()){


}

className是包含我的hashMap的类的名称,我收到错误:

  

找不到符号

我猜这与没有entrySet方法有关,但我不确定我上面提到的问题并没有提到这一点。

有人可以提供一些建议吗?

3 个答案:

答案 0 :(得分:1)

如果myMap位于另一个classprivate,那么您需要public getter method,如下所示。

    public class className {

    private HashMap<String,String> myMap = new HashMap<String,String>();

    public Map getMyMap()
    {
       return myMap;
    }

}




for(Map.Entry<String, String> entry : objectOfClassName.getMyMap().entrySet()){


}

答案 1 :(得分:0)

您需要输入Hashmap的名称。不是classname

fn2(a?: number, b: number, c?: number) { /* ... */ }

答案 2 :(得分:0)

您应该遍历设置的def imageTag = 'gcr.io/project-id/tag' def version = version from pom sh './mvnw package' sh "docker build -t $imageTag:$version ." sh('gcloud auth activate-service-account --key-file=$FILE') sh('docker login -p $(gcloud auth print-access-token) -u _token https://gcr.io') sh("gcloud docker -- push $imageTag:$version") 条目:

c6ff94654483: Preparing
209db64c273a: Preparing
762429e05518: Preparing
2be465c0fdf6: Preparing
5bef08742407: Preparing
c6ff94654483: Retrying in 5 seconds
5bef08742407: Retrying in 5 seconds
209db64c273a: Retrying in 5 seconds
2be465c0fdf6: Layer already exists
762429e05518: Layer already exists
c6ff94654483: Retrying in 4 seconds
5bef08742407: Retrying in 4 seconds
209db64c273a: Retrying in 4 seconds
c6ff94654483: Retrying in 3 seconds
5bef08742407: Retrying in 3 seconds
209db64c273a: Retrying in 3 seconds
c6ff94654483: Retrying in 2 seconds
5bef08742407: Retrying in 2 seconds
209db64c273a: Retrying in 2 seconds
c6ff94654483: Retrying in 1 second
5bef08742407: Retrying in 1 second
209db64c273a: Retrying in 1 second
5bef08742407: Retrying in 10 seconds
...
unexpected EOF