Creating Map composed of 2 Lists using stream().collect in Java

后端 未结 3 2014
再見小時候
再見小時候 2021-01-11 11:58

As for example, there are two lists:

List list1 = Arrays.asList(1.0, 2.0);
List list2 = Arrays.asList(\"one_point_zero\", \"two_p         


        
3条回答
  •  無奈伤痛
    2021-01-11 12:36

    Instead of using an auxiliary list to hold the indices, you can have them generated by an IntStream.

    Map map = IntStream.range(0, list1.size())
                .boxed()
                .collect(Collectors.toMap(i -> list1.get(i), i -> list2.get(i)));
    

提交回复
热议问题