How to convert List to Map with indexes using stream - Java 8?

前端 未结 4 1494
南笙
南笙 2020-12-15 03:00

I\'ve created method whih numerating each character of alphabet. I\'m learning streams(functional programming) and try to use them as often as possible, but I don\'t know ho

4条回答
  •  臣服心动
    2020-12-15 03:43

    It is better to use Function.identity() in place of i->i because as per answer for this question:

    As of the current JRE implementation, Function.identity() will always return the same instance while each occurrence of identifier -> identifier will not only create its own instance but even have a distinct implementation class.

    IntStream.range(0, alphabet.size())
             .boxed()
             .collect(toMap(alphabet::get, Function.identity()));
    

提交回复
热议问题