loader

For this tutorial in video form, click here

Let’s dive a bit deeper into Collections. What are the methods that we have available with them?

insert

This function allows you to either add an individual key value pair or a Collection of key value pairs.

As you can see, it is added both key value pairs in there. It also works in an array style:

There’s also a function specifically for inserting multiple values: insertAll. That’s useful if you are adding data from a key-value collection into another key-value collection.

update

If we want to change a value in a Collection, we can use this function:

We can see it was updated from “Enterprise” to “Free”. It’s important to note that this function only lets you update one at a time, but it has some other cool features. For example, if your Collection is in the form of a list, you can have it update to a value corresponding to what number it is in the list (starting with 0).

If we index value “3”, creator will change to “Free”.

delete

This function lets you delete values.

Be careful though, it doesn’t let you delete keys, only values.

deleteKey

Unlike the standard “delete” function, this works for keys.

This is tricky though, because you’re not supposed to use this one in the array style. For that, you can get away with just using “delete”.

clear

This function wipes out everything.

containsKey

This one checks to see if a certain key is present. If we test it for “company”, it will return as “true”.

If we test it for a value that isn’t in the Collection, it returns “false”

containsValue

This checks to see if a Collection contains a certain value.

getKey

This function receives a value as input and gives you back the key that value is in.

getLastKey

This function receives a key as input and gives you the last value in that key’s collection:

isEmpty

When the variable isn’t empty, it will return ” false”.

When it is empty, it will return true.

size

This function tells you how many value pairs are present in a variable.

If we do it in a list style, we’ll get 4.

If we give it an empty list, we’ll get a 0.

keys

This returns the keys in a variable.

values

Similarly, this returns all of the values in a variable.

distinct

This function lists every unique value one time, regardless of how many times it has been included in a variable.

intersect

This one could be really fun. It lists all the common values between a pair of Collection variables.

So far, this only works in list style.

duplicate

This returns a set of elements from a given Collection whose index is between start index, inclusive, and end index exclusive.

Conclusion

Hopefully this gives you an idea of all the functions you can use with Collections. Good luck!

For this tutorial in video form, click here