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"))


Array Review Exercise # 10

Get Started


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


#set($map = {})


#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.


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


#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. 


#set($user = {
"alliance":"Rebel Alliance"

## returns Captain

Alternatively, dot notation can be used.


#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.


#set($key = "alliance")



## 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.


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

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


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


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


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

#foreach($currValue in $workshopTimes)

## 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")


Next Workshop Agenda Item:


Contains Methods

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

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

## returns true


## returns false


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.



#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


Advanced Velocity Part 1 Summary Exercise # 12

Get Started

More Resources