Java 8 List into Map

前端 未结 22 2726
半阙折子戏
半阙折子戏 2020-11-22 03:38

I want to translate a List of objects into a Map using Java 8\'s streams and lambdas.

This is how I would write it in Java 7 and below.

private Map&l         


        
22条回答
  •  余生分开走
    2020-11-22 04:34

    If you don't mind using 3rd party libraries, AOL's cyclops-react lib (disclosure I am a contributor) has extensions for all JDK Collection types, including List and Map.

    ListX choices;
    Map map = choices.toMap(c-> c.getName(),c->c);
    

提交回复
热议问题