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

后端 未结 3 1993
再見小時候
再見小時候 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:20

    This works for me but is O(n^2):

        Map collect =
                list1.stream()
                        .collect(
                                toMap(Double::doubleValue, 
                                        item -> list2.get(list1.indexOf(item))));
    

提交回复
热议问题