Java 枚举映射混淆

标签 java collections enums enum-map

嗨,谁能帮我理解下面这行代码?

      private Map<EnumType, Pair<Long, Long>> processToProductLineAndIndustryMap = new EnumMap<EnumType, Pair<Long, Long>>(
        Collections.unmodifiableMap(Stream.of(
                new SimpleEntry<>(EnumType.SOME_TYPE,
                    Pair.of(Question.getProductLineQuestionId(), Question.getAdvertiserIndustryQuestionId())))
                    .collect(Collectors.toMap((e) -> e.getKey(), (e) -> e.getValue()))));

我对此很陌生。网上查了几篇文章,但无法弄清楚。

我想创建一个不可修改的 map<EnumType, Pair<Long, Long>> 。基于枚举类型,我想获取一对 Long 并查看它是否包含特定的 long 。请帮助我找出适合我的用例的最佳数据结构

最佳答案

您可以使用Collections.singletonMap(key, value)

private Map<EnumType, Pair<Long, Long>> processToProductLineAndIndustryMap = new EnumMap<EnumType, Pair<Long, Long>>(
        Collections.singletonMap(EnumType.SOME_TYPE, Pair.of(Question.getProductLineQuestionId(), Question.getAdvertiserIndustryQuestionId())));

关于Java 枚举映射混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43303618/

相关文章:

java - Spring - 如何避免协作服务之间的实现锁定

java - 接口(interface)不足以让其背后的对象工作?

c# - 如何使用 linq 将 IEnumerable 转换为 Collection

vector - 如何获取和替换 Rust Vec 中的值?

swift - 如何显示枚举的值

java - JAXB - 将元素解析为字符串

java - 为什么我可以在三元运算中将基本类型设置为 null

collections - squeak(Smalltalk) 中集合的独特或不同

android - 将枚举添加到 Android 中 Spinner 的数组适配器

.net - 获取枚举值的 GetFields 方法