简体   繁体   English

遍历HashMap并获取对象中值的“最低”整数

[英]Iterate over HashMap and get “Lowest” Integer of the value in the Object

    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
            } 
        }
    }

How to get the lowest free spot of the getPosition in this situation. 在这种情况下如何获得getPosition的最低可用点。 getPosition values are between 1-6 and there are only one of each value Room = 1 and Name = RED. getPosition值在1-6之间,并且每个值Room = 1和Name = RED中只有一个。 For example if 1,3,4,6 exists in getPosition(with room=1 and name=red) then the output should be 2. That is the lowest number that is free in getPosition in the specific combination. 例如,如果getPosition中存在1,3,4,6(其中room = 1并且name = red),则输出应为2。这是特定组合中getPosition中可用的最小数字。 Hope u can help me out. 希望你能帮助我。

Well, it sounds like the simplest approach would be something like: 好吧,听起来最简单的方法是这样的:

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...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM