Groovy Goodness: Convert Collection to Set with Only Unique Elements
Since Groovy 1.8 we can convert an array or collection to a Set with only the unique elements of the original array or collection. We use the toSet() method to do this.
def numbers = [1,2,1,4,1,2] as int[] assert numbers.toSet() == [1,2,4] as Set def list = ['Groovy', 'Java', 'Grails', 'Groovy'] assert list.toSet() == ['Groovy', 'Java', 'Grails'] as Set
Read from Source
| Print article | This entry was posted by admin on April 27, 2011 at 20:52, and is filed under Java. Follow any responses to this post through RSS 2.0. Both comments and pings are currently closed. |
Comments are closed.