Skip to content

Instantly share code, notes, and snippets.

@taikedz
Created October 16, 2023 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taikedz/7656a03328314d457a84415fd297b215 to your computer and use it in GitHub Desktop.
Save taikedz/7656a03328314d457a84415fd297b215 to your computer and use it in GitHub Desktop.
Basic comprehensions in Groovy
// Some things
List<String> items = ["banana", "pear", "apple"]
// Filtering out
List<String> e_items = items.findAll { it.contains("e") }
println(e_items)
// List from list
List<String> fruity = items.collect { "Fruit: $it" }
println fruity
// Map from list
Map<String,int> counted = items.collectEntries { ["$it", it.length() ] }
println counted
// List from map
List<int> lengths = counted.collect { it.value }
println lengths
@taikedz
Copy link
Author

taikedz commented Oct 16, 2023

Install Groovy on Ubuntu 22.04 :

sudo apt update && sudo apt install openjdk-11-jdk -y && sudo snap install --classic groovy -y

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment