[英]compare values in enum java
所以這是我的情況,我正在將WallEnums的兩種情況與其他情況進行比較。
import static com.gowallgo.enumtypes.WallEnums.CAW;
"" ( and the rest )
/**
* {@link Set} of cancel {@link WallEnums}s
*/
private static final Set<WallEnums> WALL_CODES = asSet(RES, CAW, AAP, ASV, CQP, OQR);
// more stuff and then I use it here .
if (wallEnum != WALL_CODES.contains(wallEnum)){}
乞求重構。 我應該從哪里開始,所以我不需要為每個代碼進行靜態導入?
如果您不想導入每個枚舉值,則可以使用
import com.gowallgo.enumtypes.WallEnums
WallEnums enum = WallEnums.RES; //Now you have to use EnumName.VALUE
您不必導入每種枚舉類型,可以嘗試使用以下方法導入枚舉的每個元素:
import static com.gowallgo.enumtypes.WallEnums.*;
使用EnumSet
:
// Do not import anything
// This creates a Set that contains all posible values
// In case you need a subset use: EnumSet.of(WallEnums.RES, WallEnums.CAW, etc)
private static final Set<WallEnums> WALL_CODES = EnumSet.allOf(WallEnums.class);
// Later...
if (WALL_CODES.contains(someWallEnum)) {
// Do stuff if someWallEnum belongs to WALL_CODES set
}
這段代碼使用優化的EnumSet
類創建了一組枚舉。 然后,您可以照常使用任何Set
操作,即contains()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.