Reputation: 269
I made getKey()
function to get a key from HashMap<ArrayList<String>,String>
variable.
But It is not working, I totally don't know why it is not working.
private ArrayList<String> getKey(String value) {
Iterator<ArrayList<String>> it=history_map.keySet().iterator();
while(it.hasNext()) {
if(history_map.get(it.next()).equals(value)) {
return it.next();
}
}
return null;
}
value is "Function". and history_map
has key type ArrayList<String>
.
So I find value comparing every value of key. What is the problem?
It always return null
value.
I use this function in below.
public void setFunction(String function) {
getKey("Function").add(function);
}
public void setEnergyLevel(int energy) {
getKey("EnergyLevel").add(String.valueOf(energy));
}
public void setTemperature(int temp) {
getKey("Temperature").add(String.valueOf(temp));
}
public void setHumidity(int hum) {
getKey("Humidity").add(String.valueOf(hum));
}
public void setSpeed(int speed) {
getKey("Fastest speed").add(String.valueOf(speed));
}
Upvotes: 0
Views: 264
Reputation: 4224
One reason could be inside the if:
if(history_map.get(it.next()).equals(value)) {
return it.next();
}
If the match is found, iterators next() method is called again while executing return statement. If map consist of single key, then it will throw java.util.NoSuchElementException.
Upvotes: 1