-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
  • 225
  • 2
  • 9

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));
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98