Does anybody have any idea about how I could improve the performance of this method? Note that this.allActions is a hashmap with around half a million keys.
Maybe there is a faster way of iterating through a HashMap that I don't know.
public String get_random_legal_action(String stateJSON) {
Collections.shuffle(this.allActionsKeys);
boolean legal;
HashMap<String, Integer> state = new Gson().fromJson(stateJSON, this.stateType);
for (String action : this.allActionsKeys) {
legal = true;
for (Map.Entry<String, Integer> precondition : this.allActions.get(action).precondition.entrySet()) {
try {
if (!state.get(precondition.getKey()).equals(precondition.getValue())) {
legal = false;
break;
}
} catch (NullPointerException e) {
if (!this.immutableProps.contains(precondition.getKey())) {
legal = false;
break;
}
}
}
if (legal)
return action;
}
return null;
}