Arrays and HashMaps

Advanced Velocity Workshop #LiveFromRTC2019

Brief Array Review

Initialize an Array:

#set($arr = [])

Declare Values of an Array:

#set($alphabet = ["a","b","c","d", "e"])

Adding a Value:

#set($temp = $alphabet.add("z"))

Practice

Array Review Exercise # 10

Get Started

HashMaps

Velocity is a Java-based template language, and Java does not support associative arrays. HashMaps, however, functions like an associative array by allowing key/value pair collections.

Works like an associative array

Contains key and value pairs

Cannot contain duplicate keys

Can access values by keys

Creating a HashMap

Initialize:

#set($map = {})

Syntax:

#set($map   = {"key":"value"})
#set($user = {"first":"Han", "last":"Solo"})

Add Data

To add a pair of key and value to a HashMap, use the put() method with the set directive.

Be sure to use a temporary variable so that the original variable doesn't get altered.

Example:

#set($map = {}})
#set($x = $map.put(KEY, VALUE))

Example:

#set($temp = $user.put("occupation":"Captain"))

What do you think will happen if the method is invoked without setting it to a variable?

Retrieve Data

A value of a HashMap pair can be retrieved using its key with the get() method. 

Example:

#set($user = {
"first":"Han",
"last":"Solo",
"occupation":"Captain",
"alliance":"Rebel Alliance"
})
$user.get("occupation")

## returns Captain

Alternatively, dot notation can be used.

Example:

#set($firstName = $user.first)

## $firstName returns Han

 

Arbitrary Key

In this context, a key is arbitrary when it is captured in a variable. If this is the case, the original get() method works. Dot notation does not work, but square brackets can be used.

Example:

#set($key = "alliance")
$user.get($key)

Example:

$user[$key]

## returns Rebel Alliance

Key and Value Sets

You can also retrieve all keys or all values within a HashMap by using one of the two available methods.

Example 1: keySet() returns an array of all keys.

$user.keySet()

## returns ["first", "last", "occupation", "alliance"]

Example 1: values() returns an array of all values.

$user.values()

## returns ["Han", "Solo", "Captain, "Rebel Alliance"]

Looping

When iterating through a HashMap, the loop variable is the currently iterated value.

Example:

#set( $workshopTimes = {
"Advanced Velocity-1":"9:00am",
"Lunch":"12:00pm",
"Advanced Velocity-2":"1:00pm",
})

#foreach($currValue in $workshopTimes)
$currValue
#end

## returns 9:00am 12:00pm 1:00pm

keySet() and values() can be useful when looping through HashMaps.

Example 2:

Next Workshop Agenda Item: 
#foreach($key in $workshopTimes.keySet())
#if($workshops[$key] == "12:00pm")
$key
#end
#end

Result:

Next Workshop Agenda Item:

Lunch

Contains Methods

containsKey() and containValue() methods check against respective data in a HashMap and returns a boolean.

$workshopTimes.containsKey("Advanced Velocity-1") 

## returns true

$workshopTimes.containsValue("2:00pm")

## returns false

Practice

Hashmap Review Exercise # 11

Get Started

Array of HashMaps

An array of HashMaps is ultimately a two-dimensional array. Initializing an array of HashMaps would be the same as initializing an array. To declare an array, insert HashMaps as elements of the array.

Example:

 

#set($characters= [
    {"name":"Chewbacca", "species":"Wookiee"},
    {"name":"R2-D2", "species":"Droid"},
    {"name":"Luke Skywaker", "species":"Human"},
    {"name":"Master Yoda", "species":"Unknown"},
    {"name":"Darth Vader", "species":"Human"}
])


$characters.size()  ## returns 5
$characters.get(0) ## returns {"name":"Chewbacca", "species":"Wookiee"}
$characters.get(1).species ## returns Droid

Practice

Advanced Velocity Part 1 Summary Exercise # 12

Get Started

More Resources