Saturday, 5 March 2016

Difference between toSet() and unique() in Groovy

toSet() and unique() are used for used for getting unique elements from given array. the difference is toSet() didn't modify the given array, it returns result array. but unique modifies the given array that means, the result is stored in given array.

Code:

class GroovyController {

def index(){
println '-----------------------------------'
def array = [1,2,2,3,4,5]
println 'use set : '+array.toSet()
println 'result array : '+array

array = [1,2,2,3,4,5]
println 'use unique : '+array.unique()
println 'result array : '+array
render ''
}

}

Output:

use set : [1, 2, 3, 4, 5]
result array : [1, 2, 2, 3, 4, 5]
use unique : [1, 2, 3, 4, 5]
result array : [1, 2, 3, 4, 5]

No comments:

Post a Comment