Created
October 16, 2023 12:33
-
-
Save taikedz/7656a03328314d457a84415fd297b215 to your computer and use it in GitHub Desktop.
Basic comprehensions in Groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Install Groovy on Ubuntu 22.04 :