Cómo eliminar valores duplicados de una lista en groovy
Tengo una colección de ID list para guardar en la base de datos
if(!session.ids)
session.ids = []
session.ids.add(params.id)
Y descubrí que la lista tiene duplicados, como
[1, 2, 4, 9, 7, 10, 8, 6, 6, 5]
Luego quise eliminar todos los duplicados aplicando algo como:
session.ids.removeAll{ //some clousure case }
Solo encontré esto:
Http://groovy.codehaus.org/groovy-jdk/java/util/Collection.html
5 answers
No soy una persona Maravillosa, pero creo que puedes hacer algo como esto :
[1, 2, 4, 9, 7, 10, 8, 6, 6, 5].unique { a, b -> a <=> b }
Has intentado sesión.id.unique () ?
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-11-14 17:25:18
Qué tal:
session.ids = session.ids.unique( false )
Update
Diferenciación entre unique()
y unique(false)
: la segunda no modifica la lista original. Espero que eso ayude.
def originalList = [1, 2, 4, 9, 7, 10, 8, 6, 6, 5]
//Mutate the original list
def newUniqueList = originalList.unique()
assert newUniqueList == [1, 2, 4, 9, 7, 10, 8, 6, 5]
assert originalList == [1, 2, 4, 9, 7, 10, 8, 6, 5]
//Add duplicate items to the original list again
originalList << 2 << 4 << 10
// We added 2 to originalList, and they are in newUniqueList too! This is because
// they are the SAME list (we mutated the originalList, and set newUniqueList to
// represent the same object.
assert originalList == newUniqueList
//Do not mutate the original list
def secondUniqueList = originalList.unique( false )
assert secondUniqueList == [1, 2, 4, 9, 7, 10, 8, 6, 5]
assert originalList == [1, 2, 4, 9, 7, 10, 8, 6, 5, 2, 4, 10]
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-08-31 13:48:45
Use único
def list = ["a", "b", "c", "a", "b", "c"]
println list.unique()
Esto imprimirá
[a, b, c]
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-08-20 22:45:12
def unique = myList as Set
Convierte 'myList' en un Conjunto. Cuando uses complex (clases autodefinidas) asegúrate de haber pensado en implementar hashCode() e equals() correctamente.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-07-04 09:36:50
Si se pretende esa sesión.los id contienen ID únicos, entonces usted podría hacer:
if(!session.ids)
session.ids = [] as Set
Entonces cuando lo hagas:
session.ids.add(params.id)
No se añadirán duplicados.
También puede usar esta sintaxis:
session.ids << params.id
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-06-15 19:58:35