4 回答

TA貢獻1942條經驗 獲得超3個贊
如果您愿意使用第三方庫,則可以使用Eclipse CollectionsImmutableMap
創建靜態內聯。Stream
public?static?final?ImmutableMap<String,?FooBar>?MAP_ENUM?= ????????Arrays.stream(FooBar.values()) ????????????????.collect(Collectors2.toMap(FooBar::getName,?fooBar?->?fooBar)) ????????????????.withKeyValue("xx",?FooBar.A) ????????????????.toImmutable();public?enum?FooBar?{ ????A("a"),?B("b"),?C("c");????private?String?name; ????FooBar(String?name)?{????????this.name?=?name; ????}????public?String?getName()?{????????return?this.name; ????} }
您還可以使用本機 Eclipse Collections API 稍微簡化代碼。
public?static?final?ImmutableMap<String,?FooBar>?MAP_ENUM?= ????????ArrayAdapter.adapt(FooBar.values()) ????????????????.groupByUniqueKey(FooBar::getName) ????????????????.withKeyValue("xx",?FooBar.A) ????????????????.toImmutable();

TA貢獻1865條經驗 獲得超7個贊
實際上,我認為沒有必要為此使用 Java Streams。您可以簡單地使用該static塊來初始化mapEnum它put的附加值:
public static final Map<String, FooBar> mapEnum;
static {
mapEnum = Arrays.stream(FooBar.values())
.collect(Collectors.toMap(FooBar::getName, Function.identity()));
mapEnum.put("xx", FooBar.A);
// ...
}
Collectors.toMap():不保證返回的 {@code Map} 的類型、可變性、可序列化性或線程安全性。
Map為了確保返回的可變性Collectors.toMap(),以便您Map.put()以后可以更好地使用它:
Arrays.stream(FooBar.values())
.collect(Collectors.toMap(Function.identity(), Function.identity(), (a, b) -> a, HashMap::new));
如果你真的想使用 java 流,你可以使用這個:
public static final Map<String, FooBar> mapEnum = Stream.concat(
Stream.of(FooBar.values()).map(e -> Map.entry(e.getName(), e)),
Stream.of(Map.entry("xx", FooBar.A))
).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
或者,如果您還想將所有名稱添加到枚舉值本身,您可以像這樣更改您的類:
public static enum FooBar {
A("a", "xx"), B("b"), C("c");
private String[] names;
FooBar(String... names) {
this.names = names;
}
public String[] getNames() {
return names;
}
}
并使用它來創建地圖:
public static final Map<String, FooBar> mapEnum = Stream.of(FooBar.values())
.flatMap(e -> Arrays.stream(e.getNames()).map(n -> Map.entry(n, e)))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
在 Java 9 之前使用new AbstractMap.SimpleEntry<>()而不是Map.entry(). 如果您需要對地圖進行排序,請LinkedHashMap::new使用Collectors.toMap().

TA貢獻1835條經驗 獲得超7個贊
您可以使用Collectors::collectAndThen
修改結果地圖
Arrays.stream(FooBarEnum.values()) .collect(Collectors.collectAndThen( Collectors.toMap(e-> StringUtils.upperCase(e.name), Function.identity()), FooBarEnum::addCustom));
以下方法在枚舉中
static Map<String, FooBar> addCustom(Map<String, FooBarEnum> map) { map.put("xx", FooBar.A); return map; }

TA貢獻1829條經驗 獲得超7個贊
您不能直接將它傳遞給 Collectors.toMap()。您可以在 javadoc 中看到所有可用的覆蓋:https://docs.oracle.com/javase/8/docs/api/java/util/stream/Collectors.html#toMap-java.util.function.Function-java .util.function.Function- .
但是,在使用 Stream.concat 調用 toMap 之前,您可以確保您的流具有構建地圖所需的所有對。您連接枚舉中的對,以及要添加的手動對。
我的獨立代碼必須定義 Pair 類,但由于您使用了 StringUtils,我想您有一個已經包含 Pair 的庫,因此您不需要定義它。
代碼:
import java.util.*;
import java.util.stream.*;
public class Main {
private static enum FooBar {
A("a"), B("b"), C("c");
private String name;
FooBar(String name) {
this.name = name;
}
}
public static class Pair {
String a;
FooBar b;
Pair(String a, FooBar b) {
this.a = a;
this.b = b;
}
}
public static void main(String [] args) {
System.out.println(
Stream.concat(
Arrays.stream(FooBar.values()).map(e -> new Pair(e.name.toUpperCase(), e)),
Stream.of(new Pair("xx", FooBar.A))
)
.collect(Collectors.toMap(pair -> pair.a, pair -> pair.b))
);
}
}
輸出:
{xx=A, A=A, B=B, C=C}
添加回答
舉報