-1

So I have an array list:

static List<Integer> KEYS = Arrays.asList(KEY1, KEY2, KEY3);

and I need to create a HashMap<Integer, Double> whereby the keys are KEYS, and the values are initialized to 0D.

Can I do this in one line of code?

Thx.

chasse
  • 183
  • 1
  • 7

1 Answers1

3

By using java-8 stream you can stream the list and collect them into Map

Map<Integer, Double> map = KEYS.stream()
        .collect(Collectors.toMap(Function.identity(), i -> 0d));
YCF_L
  • 51,266
  • 13
  • 85
  • 129
Deadpool
  • 33,221
  • 11
  • 51
  • 91