Get the first element of a list idiomatically in Groovy

47,748

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]
Share:
47,748

Related videos on Youtube

Adam Schmideg
Author by

Adam Schmideg

Updated on February 05, 2020

Comments

  • Adam Schmideg
    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
      Justin Piper about 12 years
      What 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
    Adam Schmideg about 13 years
    +1 for this trick. I could make it even more concise: foo?.find{ it }
  • tixxit
    tixxit over 12 years
    Adam, [0].find{it} returns null
  • Josh Diehl
    Josh Diehl about 12 years
    This would make a great convenience method addition to Groovy maps as "first()"
  • IT Gumby
    IT Gumby over 9 years
    thanks 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
    Steve Ardis about 7 years
    I 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
    Tony Baguette about 7 years
    Since 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
    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.
    Patrice M. about 3 years
    If 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/…