0

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

4
  • You are creating a new Map using EnumMaps constructor. Commented Apr 9, 2017 at 6:11
  • The above is giving error "The method getKey() is undefined for object" ? Commented Apr 9, 2017 at 6:12
  • The Collections.unmodifiableMap doesn't do anything. This appears to be a complicated way to create a map with one entry. Commented Apr 9, 2017 at 6:16
  • I will edit this question with my requirement. Please help me in choosing the best data structure for it. :) Commented Apr 9, 2017 at 6:18

1 Answer 1

1

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())));
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the help on weekends :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.