Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检查枚举列表是否包含Java中的字符串_Java_List_Enums_Compare_String Comparison - Fatal编程技术网

检查枚举列表是否包含Java中的字符串

检查枚举列表是否包含Java中的字符串,java,list,enums,compare,string-comparison,Java,List,Enums,Compare,String Comparison,我有一个具有一种属性类型的对象列表。我希望筛选该列表,使其仅包含值在枚举列表中的对象 下面是一个简单版本的Java程序,描述了上述内容 public enum Types {SLOW("Slow"), FAST("Fast"), VERY_FAST("Running");} List<Types> playerTypes = new ArrayList<>(); playerTypes.add(Types.SLOW); List<Player> myPlaye

我有一个具有一种属性类型的对象列表。我希望筛选该列表,使其仅包含值在枚举列表中的对象

下面是一个简单版本的Java程序,描述了上述内容

public enum Types {SLOW("Slow"), FAST("Fast"), VERY_FAST("Running");}
List<Types> playerTypes = new ArrayList<>();
playerTypes.add(Types.SLOW);
List<Player> myPlayers = new ArrayList<>();
Player player = new Player("FAST");
myPlayers.add(player);
for (Player p : myPlayers) {
    if(playerTypes.contains(p.getType())) {
       System.out.println("Player type is : " + p.getType());
    }
}

我只想保留玩家列表中属于枚举列表一部分的项目。上述方法似乎不起作用。请提出实现这一目标的方法。我是用Java8做这件事的。

您的enum甚至不编译。一旦你有了一个最小的完整的例子,否则你只需要使用Collections.removeIf


我认为有两种方式:

*使用枚举名称,而不是使用枚举创建播放器类型列表:

public enum Types {
    SLOW("Slow"), FAST("Fast"), VERY_FAST("Running");
}
List<String> playerTypes = new ArrayList<>();
playerTypes.add(Types.SLOW.name());
List<Player> myPlayers = new ArrayList<>();
Player player = new Player("FAST");
myPlayers.add(player);
for (Player p : myPlayers) {
    if(playerTypes.contains(p.getType())) {
       System.out.println("Player type is : " + p.getType());
    }
}

如果你想知道一个枚举是否有一个字符串,我会给我们的枚举添加一个Hashmap,并将我们的值作为一个键添加。这样,我可以做一个简单的get并检查它是否存在

public enum PlayerSpeed {

    // Type of speeds.
    SLOW("Slow"),
    FAST("Fast"),
    VERY_FAST("Running");

    // String value that represents each type of speed.
    public final String value;
    // Hash map that let us get a speed type by it's String value.
    private static Map map = new HashMap<>();

    // Private constructor.
    private PlayerSpeed(String value) { this.value = value; }

    // Fill our hash map.
    static {
        for (PlayerSpeed playerSpeed : PlayerSpeed.values()) {
            map.put(playerSpeed.value, playerSpeed);
        }
    }

    /**
     * Given a string, look it up in our enum map and check if it exists.
     * @param searchedString String that we are looking for.
     * @return True if the string is found.
     */
    public static boolean containsString(String searchedString) {
        return map.get(searchedString) != null;
    }

}

我已经尝试过这个代码,它的工作方式与预期的一样。如果有帮助,请告诉我。

getType的返回类型是什么?它是一个字符串。我直接在这里键入代码,目的是让您了解我在做什么。您的代码的工作方式与播放器类成员类型本身是Enum一样,在我的例子中,它是字符串类型。@AtulKumarVerma关于我正在做的事情的一些想法对于这样的编程问题不是很有用。谢谢您的建议。我一定会小心的。
public enum Types {
    SLOW("Slow"), FAST("Fast"), VERY_FAST("Running");
}
List<String> playerTypes = new ArrayList<>();
playerTypes.add(Types.SLOW.name());
List<Player> myPlayers = new ArrayList<>();
Player player = new Player("FAST");
myPlayers.add(player);
for (Player p : myPlayers) {
    if(playerTypes.contains(p.getType())) {
       System.out.println("Player type is : " + p.getType());
    }
}
public enum Types {
    SLOW("Slow"), FAST("Fast"), VERY_FAST("Running");
}
List<Types> playerTypes = new ArrayList<>();
playerTypes.add(Types.SLOW);
List<Player> myPlayers = new ArrayList<>();
Player player = new Player("FAST");
myPlayers.add(player);
for (Player p : myPlayers) {
    if(playerTypes.contains(Types.valueOf(p.getType()))) {
       System.out.println("Player type is : " + p.getType());
    }
}
public enum PlayerSpeed {

    // Type of speeds.
    SLOW("Slow"),
    FAST("Fast"),
    VERY_FAST("Running");

    // String value that represents each type of speed.
    public final String value;
    // Hash map that let us get a speed type by it's String value.
    private static Map map = new HashMap<>();

    // Private constructor.
    private PlayerSpeed(String value) { this.value = value; }

    // Fill our hash map.
    static {
        for (PlayerSpeed playerSpeed : PlayerSpeed.values()) {
            map.put(playerSpeed.value, playerSpeed);
        }
    }

    /**
     * Given a string, look it up in our enum map and check if it exists.
     * @param searchedString String that we are looking for.
     * @return True if the string is found.
     */
    public static boolean containsString(String searchedString) {
        return map.get(searchedString) != null;
    }

}
Player player = new Player("FAST");
if(PlayerSpeed.constainsString(p.getType())) {
   System.out.println("Player type is : " + p.getType());
}