Search code examples
groovy

How to remove Duplicate Values from a list in 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


Solution

  • 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() ?