我有一个具有一种属性类型的对象的列表。我想过滤该列表以仅包含其值在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中执行此操作。
答案 0 :(得分:0)
您的枚举甚至没有编译。一旦有了一个可以正常使用的最小的完整示例,您只需使用arr[arr[0]-1] = -arr[arr[0]-1];
arr[0]=-1;
if i = 1, arr[1] =-2;
if i = 2 , arr[2]=-3;
if i = 3 , arr[arr[3]-1] which will be arr[2] which is already negative, so this the duplicate element
。
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
答案 1 :(得分:0)
根据我的说法,有两种方法:
*使用枚举名称代替创建带有枚举的玩家类型列表:
[1, 3 ,5, 7, NaN, NaN, NaN, 15, NaN]
*您可以使用enum类的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());
}
}
方法将从valueOf
获得的字符串转换为Enum:
p.getType()
答案 2 :(得分:0)
如果要查找枚举是否具有字符串,我将向该枚举添加一个Hashmap并将其值添加为键。这样,我可以做一个简单的获取并检查它是否存在。
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;
}
}
然后,您需要做的就是使用我们的Enum的containsString方法检查字符串是否存在。
Player player = new Player("FAST");
if(PlayerSpeed.constainsString(p.getType())) {
System.out.println("Player type is : " + p.getType());
}
我已经尝试过此代码,并且可以按预期工作。请告诉我是否有帮助。