How to create a map with Java stream API using a value outside the stream?

后端 未结 3 469
灰色年华
灰色年华 2020-12-18 19:48

I want to init a Map and want to always put the same BigDecimal value from outside of the stream.

BigDeci         


        
3条回答
  •  悲&欢浪女
    2020-12-18 20:13

    Collectors#toMap expects two Functions

    set.stream().collect(Collectors.toMap(Function.identity(), x -> samePrice));
    

    You can find nearly the same example within the JavaDoc

     Map studentToGPA
         students.stream().collect(toMap(Functions.identity(),
                                         student -> computeGPA(student)));
    

提交回复
热议问题