Is it possible to initialize the keys and values of a HashMap from a List of keys, setting values to 0

chasse :

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.

Deadpool :

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=193365&siteId=1