简体   繁体   English

如何将相同类型的ArrayList项目添加到另一个?

[英]How to add ArrayList items with same type to another?

Is there any easier way to add items from one List to another? 有没有更简单的方法将项目从一个列表添加到另一个列表? I've tried the code below and result is ok. 我尝试了下面的代码,结果还可以。 But I wonder is it any simpler way to do this? 但是我想知道有没有更简单的方法来做到这一点? Thank you. 谢谢。

 ArrayList<Channel_Show_Model> items=getIntent().getParcelableArrayListExtra(FORWARDED_ITEMS_FROM_MAIN);

   for(int i=0;i<items.size();i++){
       String t=items.get(i).getTitle();
       String t2=items.get(i).getMessage();
       boolean b=items.get(i).getStatus();
       data.add(new Channel_Show_Model(t,t2,b));
   }

是的你可以用这个

data.addAll(items);

In Java-8 you can use Stream#map and Stream#collect like below: 在Java-8中,您可以使用Stream#mapStream#collect如下所示:

  data = items.stream()
                .map(e -> new Channel_Show_Model(e.getTitle(),e.getMessage(),e.getStatus())) // map the Stream<Item> to Stream<Channel_Show_Model>
                .collect(Collectors.toList()); // collect the result in a list

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM