Check equality of two ArrayList

1

I have two ArrayList objects (using Java), each object with several attributes of different type. I would like to know if these two ArrayList contain the same objects (neither more nor less), with the same values. The order is indifferent.

I have the following code:

ArrayList<Producto>productoRec=new ArrayList<(Producto.recuperarArray());  
for(Producto e : productoRec){  
    if(e!=null){
        System.out.println(e); 
    }
}
for(Producto e : producto){
    if(e!=null){
    System.out.println(e); 
    } 
}
if(producto.equals(productoRec)){ 
    ES.msgln("Los ArrayList son iguales.");  
}else{
    ES.msgln("Los ArrayList son diferentes.");
}  

When I run I get:

Fruit: Product code: 3 Product name: n (not seasonal fruit)

Fruit: Product code: 3 Product name: n (not seasonal fruit)

The ArrayList are different. BUILD SUCCESSFUL (total time: 3 seconds)

I do not know what I am doing wrong, but it is clear that the ArrayList are the same, but nevertheless the answer when executing it is that they are different.

    
asked by Raúl Soria Ortiz 18.04.2017 в 09:14
source

3 answers

2

I have tried to adapt the method of lois6b, but I can not get it to work. The only thing that worked for me, although it does not seem very orthodox, is to apply the toStrig method to each ArrayList and compare the results:

ArrayList<Producto>productoRec=new ArrayList<(Producto.recuperarArray());  
for(Producto e : productoRec){  
    if(e!=null){
        System.out.println(e); 
    }
}
for(Producto e : producto){
    if(e!=null){
    System.out.println(e); 
    } 
}
if(producto.toString()).equals(productoRec.toString())){ 
    ES.msgln("Los ArrayList son iguales.");  
}else{
    ES.msgln("Los ArrayList son diferentes.");
}  
    
answered by 18.04.2017 в 14:52
0

If you do not want to modify the contents of your lists, the best thing would be to dump the content of them in a Map and then go checking if they really exist in both places. Note: for this, it is necessary that your class implements the methods equals and hashCode .

public <T> boolean igualdad(List<T> lista1, List<T> lista2) {
    //check inicial: si las listas son la misma referencia
    //o ambas son nulas, entonces no comparar nada
    if (lista1 == lista2 || (lista1 == null && lista2 == null) {
        return true;
    }
    //segundo check: si una de las listas es nula, no comparar
    if ( (lista1 == null && lista2 != null) ||
            (lista1 !=null && lista2 == null) ) {
        return false;
    }

    Map<Producto, Integer> mapFuente = new HashMap<>();
    for (T elem : lista1) {
        Integer contador = mapFuente.get(elem);
        mapFuente.put(elem, contador == null ? 0 : contador + 1);
    }
    for (T elem : lista2) {
        Integer contador = mapFuente.get(elem);
        if (contador != null) {
            if (contador > 1) {
                mapFuente.put(elem, contador - 1);
            } else {
                mapFuente.remove(elem);
            }
        }
    }
    return map.isEmpty();
}
    
answered by 18.04.2017 в 14:52
-2

The simplest way to compare two ArrayList is to use the sort method of the class Collection , which is already implemented by the ArrayList interface, to sort the ArrayList and then compare if they are the same:

public  boolean equalLists(List<String> a, List<String> b){     
    // comprobar que tienen el mismo tamaño y que no son nulos
    if ((a.size() != b.size()) || (a == null && b!= null) || (a != null && b== null)){
        return false;
    }

    if (a == null && b == null) return true;

    // ordenar las ArrayList y comprobar que son iguales          
    Collections.sort(a);
    Collections.sort(b);      
    return a.equals(b);
}

This solution is taken from here

    
answered by 18.04.2017 в 09:22