Get the first element of a list idiomatically in Groovy
Solution 1
Not sure using find is most elegant or idiomatic, but it is concise and wont throw an IndexOutOfBoundsException.
def foo
foo = ['bar', 'baz']
assert "bar" == foo?.find { true }
foo = []
assert null == foo?.find { true }
foo = null
assert null == foo?.find { true }
--Update Groovy 1.8.1
you can simply use foo?.find() without the closure. It will return the first Groovy Truth element in the list or null if foo is null or the list is empty.
Solution 2
You could also do
foo[0]
This will throw a NullPointerException when foo is null, but it will return a null value on an empty list, unlike foo.first()
which will throw an exception on empty.
Solution 3
Since Groovy 1.8.1 we can use the methods take() and drop(). With the take() method we get items from the beginning of the List. We pass the number of items we want as an argument to the method.
To remove items from the beginning of the List we can use the drop() method. Pass the number of items to drop as an argument to the method.
Note that the original list is not changed, the result of take()/drop() method is a new list.
def a = [1,2,3,4]
println(a.drop(2))
println(a.take(2))
println(a.take(0))
println(a)
*******************
Output:
[3, 4]
[1, 2]
[]
[1, 2, 3, 4]
Related videos on Youtube
Adam Schmideg
Updated on February 05, 2020Comments
-
Adam Schmideg over 4 years
Let the code speak first
def bars = foo.listBars() def firstBar = bars ? bars.first() : null def firstBarBetter = foo.listBars()?.getAt(0)
Is there a more elegant or idiomatic way to get the first element of a list, or null if it's not possible? (I wouldn't consider a try-catch block elegant here.)
-
Justin Piper about 12 yearsWhat does #listBars return? Groovy shouldn't throw if you try to get an element that doesn't exist from a list.
final l = []
assert l[0] == null
assert l.getAt(0) == null
assert l instanceof ArrayList
-
-
Adam Schmideg about 13 years+1 for this trick. I could make it even more concise:
foo?.find{ it }
-
tixxit over 12 yearsAdam, [0].find{it} returns null
-
Josh Diehl about 12 yearsThis would make a great convenience method addition to Groovy maps as "first()"
-
IT Gumby over 9 yearsthanks for sharing! I was getting stuck around idiomatic solution for first element after findAll on a list that could be empty in the first place, or after the findAll and this gave me what I needed
-
Steve Ardis about 7 yearsI know this is an older thread, but I was looking for a similar answer. At first I tried a "foo?[0]" syntax. I think that would be pretty cool, so you could do "foo?[1]", "foo?[2]",... as well.
-
Tony Baguette about 7 yearsSince Groovy 1.8.1, you can simply use
foo?.find()
without the closure. It will return the first Groovy Truth element in the list or null if foo is null or the list is empty. source -
Vitaliy Borisok about 6 years@AdamSchmideg, want to extend tixxit comment:
[null, new Object()].find {it}
will return object, but not null element. -
Patrice M. about 3 yearsIf you're going to copy-n-paste the text directly from the Excellent MrHaki, at least include a link to his site: blog.mrhaki.com/2011/09/…