我有一个具有一种属性类型的对象列表。我想过滤该列表以仅包含其值在 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中这样做。
据我说,有两种方法:
*不要使用枚举创建玩家类型列表,而是使用枚举名称:
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());
}
}
*您可以使用 enum 类的valueOf
方法将从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());
}
}
枚举甚至不编译。一旦你有一个最小的完整示例,否则你只需要使用Collections.removeIf
.
import java.util.*;
import java.util.stream.*;
enum PlayerType {
SLOW, FAST, VERY_FAST
}
class Player {
private final PlayerType type;
public Player(PlayerType type) {
this.type = type;
}
public PlayerType type() {
return type;
}
@Override public String toString() {
return type.name();
}
}
interface Play {
static void main(String[] args) {
Set<PlayerType> playerTypes = EnumSet.of(
PlayerType.SLOW
);
List<Player> myPlayers = new ArrayList<>(Arrays.asList(
new Player(PlayerType.FAST)
));
myPlayers.removeIf(player -> !playerTypes.contains(player.type()));
System.err.println(myPlayers);
}
}
更新:原始海报说Player
商店输入String
(无论出于何种原因(。因此,需要查找枚举类型(或仅使用Set<String> playerTypes
(。
myPlayers.removeIf(player ->
!playerTypes.contains(PlayerType.valueOf(player.type()))
);
如果你想找到一个枚举是否有字符串,我会在我们的枚举中添加一个哈希映射,并将我们的值添加为键。这样,我可以做一个简单的获取并检查它是否存在。
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;
}
}
然后,您需要做的就是使用我们的枚举的 containsString 方法检查字符串是否存在。
Player player = new Player("FAST");
if(PlayerSpeed.constainsString(p.getType())) {
System.out.println("Player type is : " + p.getType());
}
我已经尝试过这段代码,它按预期工作。如果有帮助,请告诉我。