Article: Working with Collections in Groovy

education
groovy

(Tim Slagle) #1

Hey fellow SmartThings Devs!

This is the first of many posts to come where we talk about Groovy and SmartThings. We have a developer call every 2 weeks and during those calls the first portion of it is an education section. Every 2 weeks I will follow up that call with a article so you can have any code examples from the call. So without any further delay…


Collections in groovy. Lets first start but defining what a collection is. A collection is an object that groups multiple elements into a single unit. This can be used to store, manipulate, retrieve, and communicate data. Typically the items in a collection represent some sort of natural group. For example a telephone directory consisting of names and numbers would be considered a collection.

Now that we know what a collection is, let’s talk about how we can use the to store and communication data.


Pro-tip: You can follow along using https://groovyconsole.appspot.com/

Lets define our collection.

def list1 = ["groovy","is","awesome"]

Easy right? In this case we defined a collection of strings called list1. So now what? We have a collection with some strings in it. What if we want to see what is in the collection? Well you can try something like this:

list1.each { println "item: $it" }

Here we are iterating through the list1 collection and returning each item in the collection to the console. If you haven’t seen the each loop before you can get more information on that here.

Ok, so now we know how to get into the collection and see what is in it. What if we want to add something to it though? Easy peasy right? Yep!

Using a left shift operator you can add more strings into the collection we have build. It would look like this.

list1 << "test"

pro-tip: Add println list1 after you add something to the collection to see what happened

We added a new element to the list! Pretty simple eh? But we made a mistake and don’t really want test in our collection. No worries, we can remove it using a ingeniously named helper method remove().

list1.remove('test')

Don’t forget to use println to see what is in the collection now!

Great now we know how to add and remove items from our collection. So far pretty easy, right? Well it’s about to get a whole lot of the same amount of easy! Let’s learn how to look for data in our collection so we can use it to communicate information.


If you have been following along in the Groovy console go ahead and delete everything and start from scratch. Practice makes perfect right? So let’s define our collection again.

def list1 = ["groovy","is","awesome"]

Great! Our collection is defined and populated with elements. So what if we want to pull out a specific value from our collection? You could try something like this:

def found = list1.find {it == "groovy"}
​​​println "found in list: $found"​​​​

That’s great! Now we know how to pull certain elements out of our collection. So what if we want to modify the collection but not change how the original collection appears. For instance, if you wanted that collection to be uppercase, but didn’t want to change your original list1 collection. We can easily use the collect helper method to do that. collect takes every element in your collection and applies what you want to it.

def collectedList = list1.collect { it.toUpperCase() }
println "collectedList: $collectedList"​​​​​​​​​​​

Or, even better, Groovy offers a really nice helper called a spread operator. The spread operator allows you to easily execute a method on every item in a collection.

println "using spread operator: ${list1*.toUpperCase()}"

Ok, so now you have define a collection, added elements to it, removed the ones you don’t want, and some other cools things. But now that you have your collection the way to want it, you might want to display the value somewhere. With that I introduce the join method. Simple enough, it takes all the elements in your collection and joins them using a separator of your choice. I think you see where this is going… Let’s make a sentence with with our collection.

def sentence = list1.join(" ")
println "true fact: $sentence."​​​​​​​​​​​​​​​

Well that’s it, you’re now a collections master! Give yourself a high-five!


There are some other collection methods I did not go over, but not to fear, I have added examples for most of them below, including examples of the things we went over in this post. Also keep a lookout in the examples for somethings called a map. Find out more about maps here.


Really cool example of how to see what is inside the input settings collection for a SmartApp in the IDE. :slightly_smiling:


Next Developer Discussion will on on 03/09/16 - 5pm PST / 7pm CST / 8pm EST
Return multiple items from a method via a class or struct
How do I get a complete device listing?
(Tim Slagle) #3

This topic was automatically closed 8 days after the last reply. New replies are no longer allowed.