for (Entry<String, Data> entry : list.entrySet()) {
if(entry.getValue().getRoom() == 1){
if(entry.getValue().getName().equalsIgnoreCase("RED")){
entry.getValue().getPosition() // need to get the lowest free number
// between the range of 1-6
}
}
}
在这种情况下如何获得 getPosition 的最低空闲位置。 getPosition 值介于 1-6 之间,并且每个值 Room = 1 和 Name = RED 中只有一个。 例如,如果 getPosition 中存在 1,3,4,6(其中 room=1 且 name=red),则输出应为 2。这是特定组合中 getPosition 中可用的最小数字。希望你能帮助我。
最佳答案
嗯,听起来最简单的方法是这样的:
boolean[] taken = new boolean[7]; //(0-6 inclusive)
// You were never using the key as far as I could see...
for (Data data : list.values()) {
if (data.getRoom() == 1 && data.getName().equalsIgnoreCase("RED")) {
taken[data.getPosition()] = true;
}
}
for (int i = 1; i <= 6; i++) {
if (!taken[i]) {
return i;
}
}
// Do whatever you want if there are no free positions...
关于java - 迭代HashMap并获取Object中值的 "Lowest"Integer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12538995/