0
我已經做了一個枚舉常量,有一些字符串作爲屬性。字符串的數量在每個常量中是不同的,所以我使用了可變參數(第一次我這樣做)。這是我的枚舉:獲取整個枚舉中的字符串,並返回枚舉常量
enum CursorInfo {
NORMAL("Walk-to"),
TAKE("Take"),
USE("Use"),
TALK("Talk-to"),
FISH("Net", "Bait", "Cage", "Harpo");
String[] toolTip;
CursorInfo(String... toolTip) {
this.toolTip = toolTip;
}
};
現在我希望能夠鍵入類似:
CursorInfo.getCursor("Bait");
,然後我想這回:「魚」或序(我不介意它返回的),即:「4」。 我問別人這件事,他說我應該將其添加到枚舉:
private static final Set<CursorInfo> SET = EnumSet.allOf(CursorInfo.class);
public static Optional<CursorInfo> getCursor(String toolTip) {
return SET.stream().filter(info -> Arrays.stream(info.toolTip).anyMatch(option -> option.contains(toolTip))).findAny();
}
但我不知道如何利用這一點,如果這甚至工作。 簡而言之:當我使用其中一個字符串作爲參數時,如何返回枚舉常量id/name?
的可能重複http://stackoverflow.com/questions/11047756/getting-enum-associated-with-int-value –
我去看看,如果我能得到它,如果我遵循崗位工作,將其更改爲我的需要,謝謝! – xX4m4zingXx
改爲使用地圖,然後退出RSPS。 –