Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove Duplicate Values from a list in groovy

Tags:

groovy

I have a collection of ID list to be saved into the database

if(!session.ids) session.ids = []  session.ids.add(params.id)  

and I found out that list has duplicates, like

[1, 2, 4, 9, 7, 10, 8, 6, 6, 5] 

Then I wanted to remove all duplicates by applying something like :

session.ids.removeAll{ //some clousure case } 

I found only this:

http://groovy.codehaus.org/groovy-jdk/java/util/Collection.html

like image 921
Daniel Adenew Avatar asked Jul 04 '13 08:07

Daniel Adenew


1 Answers

I am not a Groovy person , but I believe you can do something like this :

[1, 2, 4, 9, 7, 10, 8, 6, 6, 5].unique { a, b -> a <=> b } 

Have you tried session.ids.unique() ?

like image 86
AllTooSir Avatar answered Sep 19 '22 14:09

AllTooSir