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 map
s here.
Really cool example of how to see what is inside the input settings collection for a SmartApp in the IDE.