如何将单个键值添加到现有的 Java 8 流?

这是我填充静态地图的方法


public static final Map<String, FooBar> mapEnum = 


Arrays.stream(FooBarEnum.values())

            .collect(Collectors.toMap(e-> StringUtils.upperCase(e.name), e -> e));

我想向这张地图添加另一个键值对。


mapEnum.put("xx", FooBar.A);

这是枚举


public enum FooBar {

   A("a"), B("b"), C("c");

}

构建地图后,我的静态地图将如下所示


{"a":FooBar.A, "b": FooBar.B, "c": FooBar.C, "xx": Foobar.A}

是否可以将显式 put 调用包括在内Collectors.toMap()?


人到中年有点甜
浏览 80回答 4
4回答

手掌心

如果您愿意使用第三方库,则可以使用Eclipse CollectionsImmutableMap创建静态内联。Streampublic 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();

莫回无

实际上,我认为没有必要为此使用 Java Streams。您可以简单地使用该static块来初始化mapEnum它put的附加值:public static final Map<String, FooBar> mapEnum;static {&nbsp; &nbsp; mapEnum = Arrays.stream(FooBar.values())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toMap(FooBar::getName, Function.identity()));&nbsp; &nbsp; mapEnum.put("xx", FooBar.A);&nbsp; &nbsp; // ...}Collectors.toMap():不保证返回的 {@code Map} 的类型、可变性、可序列化性或线程安全性。Map为了确保返回的可变性Collectors.toMap(),以便您Map.put()以后可以更好地使用它:Arrays.stream(FooBar.values())&nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toMap(Function.identity(), Function.identity(), (a, b) -> a, HashMap::new));如果你真的想使用 java 流,你可以使用这个:public static final Map<String, FooBar> mapEnum = Stream.concat(&nbsp; &nbsp; &nbsp; &nbsp; Stream.of(FooBar.values()).map(e -> Map.entry(e.getName(), e)),&nbsp; &nbsp; &nbsp; &nbsp; Stream.of(Map.entry("xx", FooBar.A))).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));或者,如果您还想将所有名称添加到枚举值本身,您可以像这样更改您的类:public static enum FooBar {&nbsp; &nbsp; A("a", "xx"), B("b"), C("c");&nbsp; &nbsp; private String[] names;&nbsp; &nbsp; FooBar(String... names) {&nbsp; &nbsp; &nbsp; &nbsp; this.names = names;&nbsp; &nbsp; }&nbsp; &nbsp; public String[] getNames() {&nbsp; &nbsp; &nbsp; &nbsp; return names;&nbsp; &nbsp; }}并使用它来创建地图:public static final Map<String, FooBar> mapEnum = Stream.of(FooBar.values())&nbsp; &nbsp; &nbsp; &nbsp; .flatMap(e -> Arrays.stream(e.getNames()).map(n -> Map.entry(n, e)))&nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));在 Java 9 之前使用new AbstractMap.SimpleEntry<>()而不是Map.entry(). 如果您需要对地图进行排序,请LinkedHashMap::new使用Collectors.toMap().

qq_花开花谢_0

您可以使用Collectors::collectAndThen修改结果地图Arrays.stream(FooBarEnum.values()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.collect(Collectors.collectAndThen( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Collectors.toMap(e->&nbsp;StringUtils.upperCase(e.name),&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Function.identity()),&nbsp;FooBarEnum::addCustom));以下方法在枚举中static&nbsp;Map<String,&nbsp;FooBar>&nbsp;addCustom(Map<String,&nbsp;FooBarEnum>&nbsp;map)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;map.put("xx",&nbsp;FooBar.A); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;map; }

千巷猫影

您不能直接将它传递给 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 {&nbsp; &nbsp; private static enum&nbsp; FooBar {&nbsp; &nbsp; &nbsp; &nbsp; A("a"), B("b"), C("c");&nbsp; &nbsp; &nbsp; &nbsp; private String name;&nbsp; &nbsp; &nbsp; &nbsp; FooBar(String name) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.name = name;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public static class Pair {&nbsp; &nbsp; &nbsp; &nbsp; String a;&nbsp; &nbsp; &nbsp; &nbsp; FooBar b;&nbsp; &nbsp; &nbsp; &nbsp; Pair(String a, FooBar b) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.a = a;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.b = b;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public static void main(String [] args) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Stream.concat(&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Arrays.stream(FooBar.values()).map(e -> new Pair(e.name.toUpperCase(), e)),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Stream.of(new Pair("xx", FooBar.A))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toMap(pair -> pair.a, pair -> pair.b))&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; }}输出:{xx=A, A=A, B=B, C=C}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java