从hashmap中选择第一个键符合if语句

时间:2015-03-04 16:04:15

标签: java random hashmap

有没有办法从符合特定条件的hashmap中选择第一个键?

作为课程的一部分,我需要写一艘战舰"游戏"。

我有两个哈希图,一个是遇到船只可以战斗而另一个是所有船只。遭遇和战斗有战斗水平,战斗技能较高的人获胜。

我遇到的问题是用户只能进入遭遇号码。他想要打架。一旦选择了遭遇,那么它应该从散列图中选择一个随机发货,看看船是否可以在该遭遇类型中战斗(并非每艘船都能在每次遭遇中战斗),如果可以,那么它会比较战斗技能并返回最终结果

基本上我不知道让我的程序从我的hashmap中选择第一个键,如果那个不能打,那么它会选择下一个,直到找到一个可以战斗的那个。

public String fightEncounter(int encNo)
{                     
    String s = "";
    if ( encounterList.get(encNo) != null)
    {
        if (!allActiveShips.isEmpty())
        {
            if ( canFight(encNo) == true)
            {
                for (Ship sh : allActiveShips.values())
                {
                if (encounterList.get(encNo).getSkillReq() < allActiveShips.get(sh).getBattleSkill())
                {
                        s = s + (" Encounter won by : " + s.toString());
                        chest = chest + encounterList.get(encNo).getPlunder();
                        sh.setShipState(ShipState.RESTING);
                }        
                else if ( allActiveShips.isEmpty() )
                {
                        s = s + (" Encounter lost as no ships available ");
                        chest = chest - encounterList.get(encNo).getPlunder();
                }   
                else if ( encounterList.get(encNo).getSkillReq() > allActiveShips.get(sh).getBattleSkill() )
                {
                        s = s + (" Encounter lost on battle skill leve and " + sh.getShipName() + " sunk ");
                        chest = chest - encounterList.get(encNo).getPlunder();
                        sh.setShipState(ShipState.SUNK);
                        if ( isDefeated() == true )
                        {
                            s = s + (" You have been defeated");
                        }
                }
            }
            }
            else
            {
                    return("CANT FIGHT");  
            }

        }
        else 
        {
            return("No ship to available");
        }
    }  
    return s;
}

我还在同一个班级写了一个私人方法,检查船是否可以在选定的遭遇中战斗,这是我无法写的方法:

private boolean canFight(int encNo)      
{
        if ( encounterList.containsValue(encNo))
        {
            for (Ship s : allActiveShips.values())
                {
                if (s.getShipType() == "ManOwar" && encounterList.get(encNo).getEncounterType() == EncounterType.BATTLE && encounterList.get(encNo).getEncounterType() == EncounterType.BLOCKADE )
                {
                    return true;
                }
                else if (s.getShipType() == "Sloop" && encounterList.get(encNo).getEncounterType() == EncounterType.BATTLE && encounterList.get(encNo).getEncounterType() == EncounterType.SKIRMISH )
                {
                    return true;
                }
                else if (s.getShipType() == "Frigate" && ((Frigate)s).isPinnace() == true )
                {
                    return true;
                }
                else if (s.getShipType() == "Frigate" && encounterList.get(encNo).getEncounterType() == EncounterType.BATTLE && encounterList.get(encNo).getEncounterType() == EncounterType.SKIRMISH )
                {
                    return true;
                }
            }   
        }
        return false;
   }

2 个答案:

答案 0 :(得分:2)

我们说密钥是Ship类型,并且有一个谓词函数

boolean testConditionOn(Ship key) {...}

Java7之前

Ship foundShip = null;
for (Ship ship : ships.keySet()) {
   if (testConditionOn(ship)) {
      foundShip = ship;
      break;
   }
}
// Do what you need with the foundShip

<强> Java8

Optional<Ship> foundShip = 
   ships.keySet().stream().filter(key -> testConditionOn(key)).findFirst();
if (foundShip.isPresent()) {
   Ship theShip = foundShip.get();
   // do what you need with theShip
}    

答案 1 :(得分:0)

这应该打印“42”或“34”,具体取决于迭代键的顺序。 HashMap不保证任何订购。

import java.util.HashMap;
import java.util.Map;

public class Main {

    public static void main(String[] args) {
        Map<String, String> source = new HashMap<>();
        source.put("fourty two", "42");
        source.put("thirty four", "34");

        for(String key : source.keySet()) {
            if (key.contains("four")) {
                System.out.println(source.get(key));
                break;
            }
        }
    }
}