Store a sub-list of objects

2

I have a question about how to make a partition of an Object List. What I want is that I have a List with 1000 objects and I want to create a sublist to have 500 objects on one side and another 500 on the other.

I have an object class called orders and I have created an order ArrayList with 1000 order objects.

What I want to get is to put those two List in a new arrayList to be able to pass it by parameter. I need it to be that way since when calling the method it is not possible to work. (I could send my Order ArrayList and take what I I would need but the method needs it to be List).

It's what I've come up with to be able to work separately on the data, but if there was another option that I do not fall right now, I could apply it to just send one part and then the other to the method.

// the only way to partition and then pass the two List of objects would be like this, but I do not know how to put it in a List to be called or if there is another way.

List<pedidos> pedido1 = pedidos.subList(0, 500); 
List<pedidos> pedido1 = pedidos.subList(500, 100);  
    
asked by kiristof 30.08.2017 в 17:09
source

3 answers

-1

Answer 1

Assuming that what you want to do is to take a List of 1000 objects and split it into 2 List (of 500 each) and then go back to join them, you just have to do the following:

List<pedidos> listanueva;
listanueva.addAll(pedidos1);
listanueva.addAll(pedidos2);

Answer 2

If you want to create a List of the 2 lists you created ( pedidos1 and pedidos2 ) then simply create a List<List<pedidos>> :

List<pedidos> pedidos1 = pedidos.subList(0, 500); 
List<pedidos> pedidos2 = pedidos.subList(500, 1000);
List<List<pedidos>> listaPedidos = new List<List<pedidos>>();
listaPedidos.add(pedidos1);
listaPedidos.add(pedidos2);

I hope I have resolved your doubt.

    
answered by 30.08.2017 / 17:37
source
1

Code

package javaapplication8;

import java.util.ArrayList;
import java.util.List;

public class JavaApplication8 {

    public static void main(String[] args) {

        List<Object> lista = new ArrayList<>();

        /* Llenamos la lista */
        for (int i = 0; i < 1000; i++) {
            lista.add(i);
        }

        List<Object> primeraLista = lista.subList(0, 500);
        List<Object> segundaLista = lista.subList(500, lista.size());

        System.out.println("La primera lista tiene " + primeraLista.size() + " elementos.");
        System.out.println("La segunda lista tiene " + segundaLista.size() + " elementos.");

    }

}

Result

La primera lista tiene 500 elementos.
La segunda lista tiene 500 elementos.

Explanation

According to what we can find about the function subList() , this does not work as it does a substring() , that is, you must indicate a valid range strong>, so that she can make the "cut" of the list.

Therefore, when we execute this line:

List<pedidos> pedido1 = pedidos.subList(0, 500);

We tell the compiler something like:

  

Returns the list of items from index 0 to index 500.

But when we execute this:

List<pedidos> pedido1 = pedidos.subList(500, 100);

We would indicate something like:

  

Return the list of elements from index 500 to 100 ???

So we can get this error:

  

Exception in thread "main" java.lang.IllegalArgumentException: fromIndex (500) > toIndex (100)

Therefore, in the end we have only left this line in place :

List<pedidos> pedido2 = pedidos.subList(500, pedidos.size());

Because pedidos.size() in our case returns the number 1000 , for which we tell the compiler something like:

  

Returns the list of items from index 500 to index 1000.

Which does not generate an error .

Example online!

    
answered by 30.08.2017 в 17:38
1

If you want to treat the lists separately, the method sublist(int indiceInicial, int indiceFinal) returns a new List , but that List is backed by the original List.

This means that non-structural operations (not reordering) in those sub-lists affect the original List .

Example

List<Integer> listaPrincipal = new ArrayList<>();
for (int i = 0; i < 10; i++) {
    listaPrincipal.add(i);
}

listaPrincipal.forEach(System.out::println);

List<Integer> subLista2= listaPrincipal.subList(5, 10);
List<Integer> subLista1= listaPrincipal.subList(0, 5);

subLista1.replaceAll(i -> i += 10);
subLista2.replaceAll(i -> i += 100);

listaPrincipal.forEach(System.out::println);

Output

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
105
106
107
108
109
  

If you want to create another list that has exactly the same objects and of which you do not care about the order, use the original List.

    
answered by 30.08.2017 в 21:26