Collect objects from an ArrayList and load them into another


I am trying to solve a problem. I have a ArrayList<Personas> and I want to collect all the records that I have in the field numeroPersonas and generate another ArrayList . First I created a ArrayList<String> to collect the different data that exist in that field and know how many data I have different. The problem comes when I have to copy an object that exists in my ArrayList all to generate another ArrayList group1 , group2 , etc. How can I pick up an object of ArrayList and save it in another ArrayList . I would go through one and if it meets some requirements, save it in another.

(It is a quick explanation of the problem since it is an ArrayList with 10000 records that will have around 100 different numbers in that field and I have to generate an object and then call a method to generate a pdf with the object. which would be to go through my ArrayList_principal - > generate my ArrayList<Personas>_grupo1 call the method passing it as parameter my arrayList and so on until I finish with the groups that I will pick up in an array the codes that I have)

Thank you very much for your time.

asked by kiristof 19.07.2017 в 18:53

4 answers


I like to understand why it does not work with while but I managed to do it with while and foreach.


        String b =;
        for(personas valor:people ){




        System.out.println("he guardado esto ");
        for(int i = 0; i < copia.size() ; i++){

            System.out.println(copia.get(i).getNumeroEmpresa());//en este punto enviaria el pdf.




Thanks for your time in the end I got it, I'm left with the desire to know why the while does not work inside another.

Greetings to everyone

answered by 22.07.2017 / 09:17

Having your Array all already loaded with the desired objects (for the example I put you a Client object, which has a method getGroup () , which returns the group to which it belongs)

ArrayList<Cliente> todos = new ArrayList<Cliente>();
        ArrayList<Cliente> grupo1 = new ArrayList<Cliente>();
        ArrayList<Cliente> grupo2 = new ArrayList<Cliente>();
        for (int i = 0; i < 10; i++) {//
            todos.add(new Cliente()); // en esta parte cargas todos los objetos de la forma que lo estas haciendo 
        }                             //    
        for (Cliente cliente : todos) {
            if(cliente.getGrupo() == 1) {
            else if(cliente.getGrupo() == 2) {
answered by 19.07.2017 в 22:55

If you can use Java 8 (at this point I hope so), it would be best if you used the features of streams :

Given a list of MiObjeto objects, and assuming you filter with a MiObjeto.cumpleFiltro() method:

List<MiObjeto> lista;

You get a stream on the list you want to filter:

Filters on the stream based on the desired criteria (in this case cumpleFiltro() ), with the filter method:>item.cumpleFiltro())

Retrieve the objects that meet the filter with the collect method, and in the form of List :>item.cumpleFiltro()).collect(Collectors.toList())

You assign the value to list 2:

 List<MiObjeto> listaFiltrada =>item.cumpleFiltro()).collect(Collectors.toList())
answered by 20.07.2017 в 10:18

Iterator code = codigo.iterator(); // he recogido mi lista de numEmpresas diferentes de mi ArraList principa ArrayList copia = new ArrayList(); //genero un array para realizar la copia de todos los registros por un mismo codigo

hile(code.hasNext()){ String b =;

while(it.hasNext()){ a =;//objeto el objeto de mi ArraList principal if(a.getNumeroEmpresa().equals(b)){ System.out.println("estoy dentro"); copia.add(a); } } }

I have managed to obtain the main object and save it in a copy array. The problem is that it only performs the first and stops. That is to say after going through the first while and traversing the second one. When you return to the first while, you do not go through the second while again. I do not understand why you did not do the second while.

Greetings and thanks for your time

answered by 20.07.2017 в 09:50