java - 从 HashMap<Enum, Object> 返回随机值

标签 java

Room类中,我需要编写一个方法来从HashMap exits返回随机退出,或者如果没有则返回null存在于该 HashMap 中。 我尝试过使用迭代器并将其保存在 Set 中的 HashMap,但没有任何效果。

public class Room 
{
private String description;
private HashMap<Direction, Room> exits;        // stores exits of this room.
public Set<Character> chars;  // stores the characters that are in this room.

/**
 * Create a room described "description". Initially, it has
 * no exits. "description" is something like "a kitchen" or
 * "an open court yard".
 * @param description The room's description.
 * Pre-condition: description is not null.
 */
public Room(String description) 
{
    assert description != null : "Room.Room has null description";
    this.description = description;
    exits = new HashMap<Direction, Room>();
    chars = new HashSet<Character>();
    sane();
}
/**
 * Define an exit from this room.
 * @param direction The direction of the exit.
 * @param neighbor  The room to which the exit leads.
 * Pre-condition: neither direction nor neighbor are null; 
 * there is no room in given direction yet.
 */
public void setExit(Direction direction, Room neighbor) 
{
    assert direction != null : "Room.setExit gets null direction";
    assert neighbor != null : "Room.setExit gets null neighbor";
    assert getExit(direction) == null : "Room.setExit set for direction that has neighbor";
    sane();
    exits.put(direction, neighbor);
    sane();
    assert getExit(direction) == neighbor : "Room.setExit has wrong neighbor";
}
 /**
 * Return the room that is reached if we go from this room in direction
 * "direction". If there is no room in that direction, return null.
 * @param direction The exit's direction.
 * @return The room in the given direction.
 * Pre-condition: direction is not null
 */
public Room getExit(Direction direction) 
{
    assert direction != null : "Room.getExit has null direction";
    sane();
    return exits.get(direction);
}

方向:

public enum Direction
{
NORTH("north"), WEST("west"), SOUTH("south"), EAST("east");

private String name;

/**
 * Constructor with parameter.
 * Pre-condition: name is not null.
 */
private Direction(String name)
{
    assert name != null : "Direction.Direction has null name";
    this.name = name;
    assert toString().equals(name) : "Direction.Direction produces wrong toString";
}

/**
 * Return the direction name.
 */
public String toString()
{
    return name;
}
}

任何帮助将不胜感激

最佳答案

尝试将 map 转换为类似这样的列表(在 Room 类中):

public Direction getRandomExit(){
        List<Direction> directions = new ArrayList<Direction>(exits.keySet());
        if (directions.size()==0){
            return null;
        }
            Random rand = new Random();
            int randomIndex = rand.nextInt(directions.size());
            return directions.get(randomIndex);

    }

关于java - 从 HashMap<Enum, Object> 返回随机值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49438588/

相关文章:

java - 无法播放音频: Loading library gstreamer-lite from resource failed

java - 如何在 JSF Web 应用程序中播放声音?

java - JAX-RS、 jackson 和 JodaTime : not working automatic configuration

java - 如何使用JAVA在MySQL数据库中存储马拉地语(印度语)

java - 尝试实现Comparable接口(interface)

java - 迭代 3 个 vector 拆分内容并分配

java - 如何在 Ant echo 中转义特殊字符(在 xml 中无效)?

java - 我怎样才能将我的对象的百分比构造为女性?

java - 来自 Android 应用程序和 Java 应用程序的不同 HTTP 响应

java - 如何使用级联 ="all,delete-orphan"制作 hibernate 集合的副本