How to remove Duplicate Values from a list in groovy

Groovy

Groovy Problem Overview


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

Groovy Solutions


Solution 1 - Groovy

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

Solution 2 - Groovy

How about:

session.ids = session.ids.unique( false )

Update
Differentiation between unique() and unique(false) : the second one does not modify the original list. Hope that helps.

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]

Solution 3 - Groovy

Use unique

def list = ["a", "b", "c", "a", "b", "c"]
println list.unique()

This will print

[a, b, c]

Solution 4 - Groovy

def unique = myList as Set

Converts myList to a Set. When you use complex (self-defined classes) make sure you have thought about implementing hashCode() and equals() correctly.

Solution 5 - Groovy

If it is intended that session.ids contain unique ids, then you could do:

if(!session.ids)
  session.ids = [] as Set

Then when you do:

session.ids.add(params.id)

duplicates will not be added.

Also you can use this syntax:

session.ids << params.id

Solution 6 - Groovy

Merge two arrays and make elements unique:

def arr1 = [1,2,3,4]
def arr2 = [1,2,5,6,7,8]
def arr3 = [1,5,6,8,9]

Let's look at mergings:

arr1.addAll(arr2, arr3) 
// [1, 2, 3, 4, [1, 2, 5, 6, 7, 8], [1, 5, 6, 8, 9]]
def combined = arr1.flatten() 
// [1, 2, 3, 4, 1, 2, 5, 6, 7, 8, 1, 5, 6, 8, 9]
def combined = arr1.flatten().unique()
// [1, 2, 3, 4, 5, 6, 7, 8, 9]


def combined = (arr1 + arr2 + arr3).flatten().unique()

def combined = (arr1 << arr2 << arr3).flatten().unique()

def combined = arr1.plus(arr2).plus(arr3).flatten().unique()

Output will be:

println combined
[1, 2, 3, 4, 5, 6, 7, 8, 9]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDaniel AdenewView Question on Stackoverflow
Solution 1 - GroovyAllTooSirView Answer on Stackoverflow
Solution 2 - Groovytim_yatesView Answer on Stackoverflow
Solution 3 - GroovygurbietaView Answer on Stackoverflow
Solution 4 - GroovyHans WesterbeekView Answer on Stackoverflow
Solution 5 - GroovyMauro ZalloccoView Answer on Stackoverflow
Solution 6 - Groovyuzay95View Answer on Stackoverflow