Java从两个枚举类型初始化EnumBiMap

huangapple go评论64阅读模式
英文:

Java Initialize EnumBiMap From Two Enum Types

问题

什么是初始化EnumBiMap的最便捷方式使其从两种不同的枚举类型中进行初始化我有以下情景

    public enum First {
      A,
      B,
      C
    }


    public enum Second {
      ALPHA,
      BETA,
      GAMMA
    }

我尝试过类似以下的方式

            private static EnumBiMap<First, Second> typeMap =
                EnumBiMap<First, Second>.create(
                       Arrays.stream(First.values()).collect(Collectors.toMap(
                                Function.identity(), type -> {
                                    switch(type) {
                                        case First.A:
                                            return Second.ALPHA;
                                        case First.B:
                                            return Second.BETA;
                                        default:
                                            return Second.GAMMA;
                                    }})));


但是我失去了类型信息所以出现了错误有没有更好的方法来获得这个映射我也不能够通过连续使用put()来实现因为put()只会返回值而不是返回对映射的引用在我的情况下这个映射也可以是不可变的
英文:

What's the most convenient way to initialize an EnumBiMap from two different enum types? I have the following scenario:

public enum First {
  A,
  B,
  C
}

and

public enum Second {
  ALPHA,
  BETA,
  GAMMA
}

I have tried something like

        private static EnumBiMap&lt;First, Second&gt; typeMap =
            EnumBiMap&lt;First, Second&gt;.create(
                   Arrays.stream(First.values()).collect(Collectors.toMap(
                            Function.identity(), type -&gt; {
                                switch(type) {
                                    case First.A:
                                        return Second.ALPHA;
                                    case First.B:
                                        return Second.BETA;
                                    default:
                                        return Second.GAMMA
                                }})));

But I lose the type information so I get errors. Is there a nicer way to get this mapping? I also can't daisy-chain puts since put() just returns the value as opposed to a reference to the map. In my case, the map could also be immutable.

答案1

得分: 1

import com.google.common.collect.EnumBiMap;
import com.google.common.collect.Streams;
import static java.util.Arrays.stream;

EnumBiMap<First, Second> map = EnumBiMap.create(First.class, Second.class);
Streams.forEachPair(stream(First.values()), stream(Second.values()), map::put);
英文:
import com.google.common.collect.EnumBiMap;
import com.google.common.collect.Streams;
import static java.util.Arrays.stream;

EnumBiMap&lt;First, Second&gt; map = EnumBiMap.create(First.class, Second.class);
Streams.forEachPair(stream(First.values()), stream(Second.values()), map::put);

答案2

得分: 0

你所采用的映射方式似乎是一种按顺序进行映射。然后,你可以使用枚举的 .ordinal() 方法来获取枚举的序号值,以创建映射。

EnumBiMap<First, Second>.create(
      Arrays.stream(First.values())
             .collect(Collectors.toMap(Function.identity(),
                                       e -> Second.values()[e.ordinal()])));
英文:

The way you are mapping seems like mapping ordinally. Then you can use the ordinal value using .ordinal() of the enum to create the map.

EnumBiMap&lt;First, Second&gt;.create(
      Arrays.stream(First.values())
             .collect(Collectors.toMap(Function.identity(),
                                       e -&gt; Second.values()[e.ordinal()])));

huangapple
  • 本文由 发表于 2020年10月9日 02:30:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/64268626.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定