javacollectionsenumsenum-map

Java enummap confusion


Hi Can anyone help me in understanding the following line of code?

      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()))));

I am new to this. Have went through several articles online but could not able to figure out.

I want to create an unmodifiable map<EnumType, Pair<Long, Long>>. Based on enumtype i want to get the pair of Longs and see if it contains a particular long or not. Please help me in figuring out the best data structure for my usecase


Solution

  • You can use 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())));