I am trying to loop over a HashMap with the keySet() method as below:
for (String key : bundle.keySet()) {
String value = bundle.get(key);
...
}
I use a lot of for-each loops on HashMaps in other parts of my code, but this one as a weird behavior: its size is 7 (what’s normal) but keySet, entrySet and values are null (according to the Eclipse debugger)!
The “bundle” variable is instantiated and populated as follows (nothing original…):
Map <String, String> privVar;
Constructor(){
privVar = new HashMap<String, String>();
}
public void add(String key, String value) {
this.privVar.put(key, value);
}
What do you mean by
keySet,entrySetandvalues? If you mean the internal fields ofHashMap, then you should not look at them and need not care about them. They are used for caching.For example in the Java 6 VM that I use
keySet()is implemented like this:So the fact that
keySetisnullis irrelevant.keySet()(the method) will never returnnull.The same is true for
entrySet()andvalues().