-1

我想创建一个Map<Long, Enum< ? extends SomeInterface>. 哪个对我来说是最好的选择?

我试过这个

private Map<Long, Enum<? extends SomeInterface>[]> questionIdToanswersMapping = Collections.unmodifiableMap(Stream.of(
        new SimpleEntry<>(QuestionEnum1.getQuestionId(), AnswerEnum1.values()),
        new SimpleEntry<>(QuestionEnum2.getQuestionId(), AnswerEnum2.values()),
        new SimpleEntry<>(QuestionEnum3.getQuestionId(), AnswerEnum3.values()),
        new SimpleEntry<>(QuestionEnum4.getQuestionId(), AnswerEnum4.values()),
        new SimpleEntry<>(QuestionEnum5.getQuestionId(), AnswerEnum5.values()))
.collect(Collectors.toMap((e) -> e.getKey(), (e) -> e.getValue())));

但它给出了错误“无法转换Map<Object,Object>Map<Long,Enum<? extends SomeEnum>[]>”。我是新来的。请帮忙!

我需要不可修改的问题 ID 映射到相应的可能答案值。可能的答案是枚举

可能的答案是这样包装的:

public class RecognizedAnswers {
public enum AnswerEnum1 implements SomeInterface;
public enum Answer2 implements SomeInterface;
 }
4

2 回答 2

1

我认为命名存在一个小问题:

您不能在 java 中用另一个枚举扩展一个枚举,而是使用具有所需方法的接口,如下所示

下面的代码工作正常:

   @Test
    public void test() {
        Map<Long, Enum<? extends SomeEnum>[]> questionIdToanswersMapping = Collections.unmodifiableMap(Stream.of(
                new AbstractMap.SimpleEntry<>(QuestionEnum1.A.getQuestionId(), AnswerEnum1.values()),
                new AbstractMap.SimpleEntry<>(QuestionEnum1.B.getQuestionId(), AnswerEnum1.values()),
                new AbstractMap.SimpleEntry<>(QuestionEnum1.C.getQuestionId(), AnswerEnum2.values()),
                new AbstractMap.SimpleEntry<>(QuestionEnum1.D.getQuestionId(), AnswerEnum2.values())
                )
                .collect(Collectors.toMap((e) -> e.getKey(), (e) -> e.getValue())));

        System.out.print(questionIdToanswersMapping.size());
    }

    enum QuestionEnum1 {
        A, B, C, D;

        Long getQuestionId() {
            return (long) name().hashCode(); // my mocked values
        }

    }

    interface SomeEnum {

    }

    enum AnswerEnum1 implements SomeEnum {

    }
    enum AnswerEnum2 implements SomeEnum {

    }
于 2017-04-09T07:39:03.733 回答
0

我试图复制你的例子(因为你混淆了枚举类型,我自己编了),它似乎编译得很好:

enum SomeEnum { FOO, BAR }

private Map<Long, Enum<? extends SomeEnum>[]> exampleMap =
    Collections.unmodifiableMap(Stream.of(
        new SimpleEntry<>(1L, SomeEnum.values()))
      .collect(Collectors.toMap(SimpleEntry::getKey, SimpleEntry::getValue)));

我的猜测是,您要么缺少括号,要么QuestionEnum1.getQuestionId()返回 anint而不是 a long,而这些事情使编译器感到困惑,以至于它无法给出明确的错误消息。

我会注意到 Stream API 确实不是构造常量映射的干净方法。简单地“正常”构建这样的地图Map.put()可能会更简单,更容易阅读,即使它需要一个static {}块或辅助函数。您可以使用Guava不可变集合做得更好,可以这样使用:

private final ImmutableMap<Long, Enum<? extends SomeEnum>[]> questionIdToanswersMapping =
    ImmutableMap.builder()
        .put(QuestionEnum1.getQuestionId(), AnswerEnum1.values())
        .put(QuestionEnum2.getQuestionId(), AnswerEnum2.values())
        .put(QuestionEnum3.getQuestionId(), AnswerEnum3.values())
        .put(QuestionEnum4.getQuestionId(), AnswerEnum4.values())
        .put(QuestionEnum5.getQuestionId(), AnswerEnum5.values())
        .build();

更清晰,更容易阅读(和写作)。

于 2017-04-09T07:39:57.803 回答