More efficient way to compare two variables type List

0

I have two variables of type List that need to be compared against each other to check for possible duplicates.

Example of Variable Content

Variable 01: [01, Test, Active]

Variable 02: [01, Test, Active]

I'm currently using the following script in Groovy to check:

for (TransportadoraPostgreSQL transportadorasBDM : transportadorasBanco){
    for(List transportadoraAVerificar : listaTransportadora){
        if (transportadorasBDM.getCodigo().toString().equals(transportadoraAVerificar.get(0).toString())){
            //logger.info("Iguais")
        }
    }
}

But in this way when the two variables have, for example, 100 records each, there will be 10,000 checks in Loop , is there any way to be more efficient this check?

    
asked by anonymous 02.03.2017 / 12:13

1 answer

3

Use Except

var firstNotSecond = list1.Except(list2).ToList();
var secondNotFirst = list2.Except(list1).ToList();

This method is quite fast.

return !firstNotSecond.Any() && !secondNotFirst.Any();

Greatest information

    
02.03.2017 / 12:46