Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

several arrayList to single List

I’m working on an app that deals with animals by category.

  public class Animal { 
  private long id; 
  private color String ;
  ... 
   }

I have several arrayList with animal objects.
I manage to gather all my arrayList in another arrayList

So i have:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

ArrayList <ArrayList<Animal>> 

my goal is to put all arrayList in a single arrayList

In summary:

   Before :  ArrayList <ArrayList<Animal>>     
   After:  ArrayList<Animal> with a same list of Animals

I manage to do it in procedural programming easily but I don’t know how to do it in functional programming with Stream from Java 8

>Solution :

You can solve with Flatmap with stream, Collect

ArrayList<ArrayList<Animal>> SingleListofAnimal = new  ArrayList<ArrayList<Animal>>();
 SingleListofAnimal  = List.stream()
                        .flatMap(List::stream)
                        .collect(Collectors.toList());

Let me know if it works for you

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading