For this we must use the plugin: [source,groovy]
Descargar el script ../scripts/basico/Listas.groovy
29 November 2017
Whether it’s your first steps with groovy or you’ve been worked with it certainly, you’ve been able to work with arrays and you have seen their great potential.
These objects can be found in your development in different ways: returns sql query data, a criteria, a JSON or a simple configuration file outside of your applications that contains a list with configuration parameters … You will work with them, is useful know simple methods to work with them in the simplest and most elegant way is possible.
Next we will see some practical and basic methods to work with lists:
For this we must use the plugin: [source,groovy]
@Grab(group='org.codehaus.gpars', module='gpars', version='1.0.0')
Once done it will be as simple as:
withPool(10) {
lista.eachParallel{l->
println l
}
}
With withPool
we indicate that we want it to be executed in threads, in this case 10 in 10 and with` eachParallel`
that the launch must be in parallel.
Imagine that we have two lists and we just do not want to be left with the same values for this we have:
lista_1.intersect(lista_2)
In this method, we simply go through one of the lists and search, for each map in the second list, if there is a map
with the same key
and if so, we save them in another list where at the end you will keep the objects with
the same key
.
In this case we want that in addition to different key
, the` value` of the map is different.
We can use the previous method simply by using a !row
into the if
and in the find
method include the value
but
we are going to do it in a different way:
def commons = lista_1.intersect(lista_2)
def diff_part1 = lista_1.plus(lista_2)
diff_part1.removeAll(commons)
The intersect
method gives us the same elements (key
and value
), which are in both lists. This method is not
useful for the previous case since in it we asked that they have only the same key
.
With the plus
method we have creating a new list with the toriginal values plus the list that we passed by
parameter.
Finally with removeAll
we eliminate the list of common values between the two lists
def list_sort = list.sort{it."${criterio}"}
In this case groovy offers us the sort
method which we will pass, by parameter, by which criterion we want to order
and will return the list with the indicated format.
def list_group = list.groupBy{it."${criterio}"}
To perform the grouping by a criterion within our list, it will be as simple as using the groupby
indicating by which
criteria we wish to carry out this action.
Remove values from a list
def list_rm = list.removeAll {it."${criterio}" == 1}
As in the other cases, in this case we use the method removeAll
, passing by parameter the value that we want to
eliminate.
Get non-repeated values
def list_unique = list.unique()
With this functionality, what we obtain a list of values without duplicities, obtaining an array with unique values.
We will use the unique()
method.
//tag::grab[]
@Grab(group='org.codehaus.gpars', module='gpars', version='1.0.0')
//end::grab[]
@GrabConfig(systemClassLoader=true)
import static groovyx.gpars.GParsPool.withPool
def lista_1 = [
[id:1,value:5],
[id:2,value:8],
[id:3,value:4],
[id:4,value:1],
[id:5,value:2]
]
def lista_2 = [
[id:11,value:5],
[id:12,value:18],
[id:13,value:14],
[id:14,value:11],
[id:15,value:12]
]
def lista_3 = [
[id:1,value:15],
[id:2,value:8],
[id:3,value:4],
[id:4,value:1],
[id:5,value:2]
]
def lista_4 = [
[id:1,value:1],
[id:1,value:1],
[id:2,value:2],
[id:2,value:2],
[id:5,value:2]
]
def lista_5 = [
[id:1,value:1],
[id:1,value:11],
[id:2,value:2],
[id:2,value:21],
[id:5,value:2]
]
def recorrerListaHilos(lista){
//tag::withPool[]
withPool(10) {
lista.eachParallel{l->
println l
}
}
//end::withPool[]
}
def unirValoresIgualesPorKey(lista_1,lista_2){
//tag::unir[]
def list_all = []
lista_1.each{a->
def row = lista_2.find{it.key == a.key}
if (row)
list_all << row
}
//end::unir[]
return list_all
}
def mostrarDiferencias(lista_1, lista_2){
//tag::diff[]
def commons = lista_1.intersect(lista_2)
def diff_part1 = lista_1.plus(lista_2)
diff_part1.removeAll(commons)
//end::diff[]
return diff_part1
}
def unirValoresIguales(lista_1,lista_2){
//tag::iguales[]
lista_1.intersect(lista_2)
//end::iguales[]
return lista_1.intersect(lista_2)
}
def ordenarPor(list,criterio){
//tag::sort[]
def list_sort = list.sort{it."${criterio}"}
//end::sort[]
return list_sort
}
def agruparPor(list,criterio){
//tag::groupby[]
def list_group = list.groupBy{it."${criterio}"}
//end::groupby[]
return list_group
}
def obtenerValoresUnicos(list){
//tag::unique[]
def list_unique = list.unique()
//end::unique[]
return list_unique
}
def eliminarValores(list,criterio){
//tag::remove[]
def list_rm = list.removeAll {it."${criterio}" == 1}
//end::remove[]
return list_rm
}