Home > Back-end >  How to iterate arraylists within an arraylist, and insert their data onto an array
How to iterate arraylists within an arraylist, and insert their data onto an array

Time:11-09

Let's say I have an array that takes instances of the type "Pessoal":

Pessoal[] teste = new Pessoal[6];

Now let's say I have 2 arraylists that are inside of an arraylist:

static ArrayList<Pessoal> lista_de_profs; // This one has 4

static ArrayList<Pessoal> lista_de_infos; // And this one has 2, matching the 6 on "teste"

// these arrayslists have instances of the type "Pessoal"
ArrayList<ArrayList<Pessoal>> lista_de_docentes = new ArrayList<>();
 
lista_de_docentes.add(lista_de_profs); 
lista_de_docentes.add(lista_de_infos);

How do I iterate through the arraylist (lista_de_docentes) that contains more arraylists (lista_de_profs & lista_de_infos), and get their instances, so I can put them inside the array?

This works if teste's length is 4

for (int i = 0; i < teste.length; i  ){
     teste[i] = lista_de_profs.get(i);
}

But this only covers one arraylist, and I want all of the ones inside "lista_de_docentes", which are: "lista_de_profs" and "lista_de_infos"

*Some code in here, the language is in Portuguese, but I can change it to english, if it becomes confusing.

I hope I was clear, and thanks.

CodePudding user response:

You can collect all your "Pessoal" elements into a single arraylist and then move the elements to your array

List<Pessoal> all= lista_de_docentes.stream()
    .flatMap(List::stream)
    .collect(Collectors.toList());

for (int i =0; i < all.size(); i  )
    teste[i] = all.get(i);

Or you can directly create the array (in case you don't care about the size)

Pessoal[] teste = lista_de_docentes.stream()
    .flatMap(List::stream)
    .collect(Collectors.toList())
    .toArray(Pessoal[]::new);

CodePudding user response:

You can do this with the stream API.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.stream.Collectors;

public class Pessoal {

    public static void main(String[] args) {
        ArrayList<Pessoal> lista_de_profs = new ArrayList<>();
        ArrayList<Pessoal> lista_de_infos = new ArrayList<>();
        ArrayList<ArrayList<Pessoal>> lista_de_docentes = new ArrayList<>();
        lista_de_docentes.add(lista_de_profs); 
        lista_de_docentes.add(lista_de_infos);
        Pessoal[] teste = lista_de_docentes.stream()
                                           .flatMap(l -> l.stream())
                                           .collect(Collectors.toList())
                                           .toArray(new Pessoal[0]);
        System.out.println(Arrays.toString(teste));
    }
}
  • lista_de_docentes.stream() creates a stream where every element has type ArrayList<Pessoal>.
  • flatMap returns a stream where every element has type Pessoal.
  • All the stream [Pessoal] elements are collected and put into a List.
  • Method toArray (of interface List) converts the List to an array.

Alternatively, if you don't want to use streams:

import java.util.ArrayList;
import java.util.Arrays;

public class Pessoal {

    public static void main(String[] args) {
        ArrayList<Pessoal> lista_de_profs = new ArrayList<>();
        ArrayList<Pessoal> lista_de_infos = new ArrayList<>();
        ArrayList<ArrayList<Pessoal>> lista_de_docentes = new ArrayList<>();
        lista_de_docentes.add(lista_de_profs); 
        lista_de_docentes.add(lista_de_infos);
        ArrayList<Pessoal> temp = new ArrayList<>();
        for (ArrayList<Pessoal> list : lista_de_docentes) {
            for (Pessoal p : list) {
                temp.add(p);
            }
        }
        Pessoal[] teste = new Pessoal[temp.size()];
        temp.toArray(teste);
        System.out.println(Arrays.toString(teste));
    }
}

By the way, you should consider adopting Java naming conventions.

  • Related