猿问

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

我有一个具有一种属性类型的对象列表。我想过滤该列表以仅包含其值在 Enum 列表中的那些对象。


这是上面描述的 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());

    }

}

我只想保留玩家列表中属于枚举列表的那些项目。以上似乎不起作用。请提出一种方法来实现这一目标。我在 Java 8 中这样做。


慕斯王
浏览 485回答 3
3回答

拉丁的传说

在我看来,有两种方式:*不要使用枚举创建玩家类型列表,而是使用枚举名称:public enum Types {&nbsp; &nbsp; 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) {&nbsp; &nbsp; if(playerTypes.contains(p.getType())) {&nbsp; &nbsp; &nbsp; &nbsp;System.out.println("Player type is : " + p.getType());&nbsp; &nbsp; }}*您可以使用valueOf枚举类的方法将获取的字符串p.getType()转换为枚举:public enum Types {&nbsp; &nbsp; 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) {&nbsp; &nbsp; if(playerTypes.contains(Types.valueOf(p.getType()))) {&nbsp; &nbsp; &nbsp; &nbsp;System.out.println("Player type is : " + p.getType());&nbsp; &nbsp; }}

森栏

如果你想知道一个 Enum 是否有一个 String,我会向我们的 Enum 添加一个 Hashmap 并将我们的值添加为一个键。这样,我可以做一个简单的获取并检查它是否存在。public enum PlayerSpeed {&nbsp; &nbsp; // Type of speeds.&nbsp; &nbsp; SLOW("Slow"),&nbsp; &nbsp; FAST("Fast"),&nbsp; &nbsp; VERY_FAST("Running");&nbsp; &nbsp; // String value that represents each type of speed.&nbsp; &nbsp; public final String value;&nbsp; &nbsp; // Hash map that let us get a speed type by it's String value.&nbsp; &nbsp; private static Map map = new HashMap<>();&nbsp; &nbsp; // Private constructor.&nbsp; &nbsp; private PlayerSpeed(String value) { this.value = value; }&nbsp; &nbsp; // Fill our hash map.&nbsp; &nbsp; static {&nbsp; &nbsp; &nbsp; &nbsp; for (PlayerSpeed playerSpeed : PlayerSpeed.values()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map.put(playerSpeed.value, playerSpeed);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Given a string, look it up in our enum map and check if it exists.&nbsp; &nbsp; &nbsp;* @param searchedString String that we are looking for.&nbsp; &nbsp; &nbsp;* @return True if the string is found.&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public static boolean containsString(String searchedString) {&nbsp; &nbsp; &nbsp; &nbsp; return map.get(searchedString) != null;&nbsp; &nbsp; }}然后,您需要做的就是使用 Enum 的 containsString 方法检查 String 是否存在。Player player = new Player("FAST");if(PlayerSpeed.constainsString(p.getType())) {&nbsp; &nbsp;System.out.println("Player type is : " + p.getType());}我已经尝试过这段代码,它按预期工作。如果有帮助,请告诉我。

小唯快跑啊

你枚举甚至不编译。一旦您获得了一个可以正常工作的最小完整示例,您只需要使用Collections.removeIf.import java.util.*;import java.util.stream.*;enum PlayerType {&nbsp; &nbsp; SLOW, FAST, VERY_FAST}class Player {&nbsp; &nbsp; private final PlayerType type;&nbsp; &nbsp; public Player(PlayerType type) {&nbsp; &nbsp; &nbsp; &nbsp; this.type = type;&nbsp; &nbsp; }&nbsp; &nbsp; public PlayerType type() {&nbsp; &nbsp; &nbsp; &nbsp; return type;&nbsp; &nbsp; }&nbsp; &nbsp; @Override public String toString() {&nbsp; &nbsp; &nbsp; &nbsp; return type.name();&nbsp; &nbsp; }}interface Play {&nbsp; &nbsp;static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp;Set<PlayerType> playerTypes = EnumSet.of(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PlayerType.SLOW&nbsp; &nbsp; &nbsp; &nbsp;);&nbsp; &nbsp; &nbsp; &nbsp;List<Player> myPlayers = new ArrayList<>(Arrays.asList(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Player(PlayerType.FAST)&nbsp; &nbsp; &nbsp; &nbsp;));&nbsp; &nbsp; &nbsp; &nbsp;myPlayers.removeIf(player -> !playerTypes.contains(player.type()));&nbsp; &nbsp; &nbsp; &nbsp;System.err.println(myPlayers);&nbsp; &nbsp;}}更新:原始海报说Player商店输入 a String(无论出于何种原因)。因此,需要查找枚举类型(或仅使用 a Set<String> playerTypes)。&nbsp; &nbsp;myPlayers.removeIf(player ->&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;!playerTypes.contains(PlayerType.valueOf(player.type()))&nbsp; &nbsp;);
随时随地看视频慕课网APP

相关分类

Java
我要回答