Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
one liner in groovy
//turn list into map
assert [1:2,3:4] == [[1,2],[3,4],[1,2]].collectEntries {it} //{[it[0],it[1]]}
//sublist from nth to end
assert [3,4,5,6] == [1,2,3,4,5,6][2..-1]
assert [3,4,5,6] == [1,2,3,4,5,6][2, 3..-1]
//string to number
assert 1.2 == "1.2" as double
//if x exists then x else default
assert "hello" ==["hello"] ?: "hello"
//remove null from list
assert [1,"a"] == [1,null,"a"] - null
//remove duplicate entries
assert [1,2] == [1,1,2].unique()
//regex matcher as collection
assert [1,2,3] == ("1 2 3" =~ /\d/).collect{it as Integer}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.