Initialization of an ArrayList in one line

后端 未结 30 2681
北恋
北恋 2020-11-22 01:10

I wanted to create a list of options for testing purposes. At first, I did this:

ArrayList places = new ArrayList();
places.add(\         


        
30条回答
  •  一个人的身影
    2020-11-22 01:44

    interestingly no one-liner with the other overloaded Stream::collect method is listed

    ArrayList places = Stream.of( "Buenos Aires", "Córdoba", "La Plata" ).collect( ArrayList::new, ArrayList::add, ArrayList::addAll );
    

提交回复
热议问题