Search

Dark theme | Light theme

January 1, 2010

Groovy Goodness: Removing Duplicate Elements in a Collection

We can easily remove duplicate elements in a collection with the different unique() methods. Without any parameters the default comparator for the objects is used to determine duplicate elements. But we can also use a closure to define the rules for duplicate elements. The closure can have one parameter and then we must return a value that is used for comparison, or the closure has two parameters and then we must return an Integer value where 0 means the items are not unique. Finally we can pass our own Comparator implementation to the unique() method where we define when an element is unique or not.

class User implements Comparable {
    String username, email
    boolean active
    
    int compareTo(Object other) {
        username <=> other.username
    }
}

def mrhaki1 = new User(username: 'mrhaki', email: 'mrhaki@localhost', active: false)
def mrhaki2 = new User(username: 'mrhaki', email: 'user@localhost', active: true)
def hubert1 = new User(username: 'hubert', email: 'user@localhost', active: false)
def hubert2 = new User(username: 'hubert', email: 'hubert@localhost', active: true)

// Caution: unique() changes the original collection, so 
// for the sample we invoke unique each time on a fresh new
// user list.
def uniqueUsers = [mrhaki1, mrhaki2, hubert1, hubert2].unique()
assert 2 == uniqueUsers.size()
assert [mrhaki1, hubert1] == uniqueUsers

// Use closure with one parameter.
def uniqueEmail = [mrhaki1, mrhaki2, hubert1, hubert2].unique { user -> 
    user.email 
}
assert 3 == uniqueEmail.size()
assert [mrhaki1, mrhaki2, hubert2] == uniqueEmail

// Use closure with two parameters.
def uniqueActive = [mrhaki1, mrhaki2, hubert1, hubert2].unique { user1, user2 ->
    user1.active <=> user2.active
}
assert 2 == uniqueActive.size()
assert [mrhaki1, mrhaki2] == uniqueActive

// Use a comparator.
def emailComparator = [
    equals: { delegate.equals(it) },
    compare: { first, second ->
        first.email <=> second.email
    }
] as Comparator
def unique = [mrhaki1, mrhaki2, hubert1, hubert2].unique(emailComparator)
assert 3 == unique.size()
assert [mrhaki1, mrhaki2, hubert2] == unique
​