Quickref - ArrayBuffer, HashMap, and HashSet for gaming
Note – An Arraybuffer is a type of sequence (containing a sequence of elements one after the other). In other languages, ArrayBuffers are variously known as Lists, Arrays, Vectors, ArrayLists, etc.
- 1 ArrayBuffer
- 1.1 Creation
- 1.2 Addition of elements at the end
- 1.3 Iteration over all the elements
- 2 HashMap
- 2.1 Creation
- 2.2 Addition of key, value pairs
- 2.3 Lookup of values based on keys
- 3 HashSet
- 3.1 Creation
- 3.2 Addition of objects
- 3.3 Iteration over all the elements
- 3.4 Removal of objects
- 3.5 Containment check
An ArrayBuffer is a sequence of elements. Every element can be located based on its position or index within the ArrayBuffer.
Positions (or indices) start from 0 and go upto length - 1.
Here is a pictorial representation of an ArrayBuffer:

The following are the primary operations that you can do with an ArrayBuffer:
ArrayBuffer.empty[type]- creates an empty ArrayBuffer which will hold elements of the given type.
Example:
ArrayBuffer.empty[Int]
ArrayBuffer.empty[Picture]
ArrayBuffer(e1, e2, e3)- creates an ArrayBuffer with the given elements
Example:
ArrayBuffer(4, 9, 7)
ArrayBuffer(pic1, pic2, pic3, pic4)
1.2 Addition of elements at the end
ab.append(element)- addselementto the end of an ArrayBuffer calledab.
Assume that you have created an ArrayBuffer of Ints called ab1 and and ArrayBuffer of Pictures called ab2, and that you have a Picture called pic5.
Example:
ab1.append(7)
ab2.append(pic5)
1.3 Iteration over all the elements
Iterating over the elements means going over all the elements (to do something with each)
repeatFor(ab) { e => /* do whatever with e /* }- iterators over all the elements of an ArrayBuffer calledab. While iterating, the current element is calledein the given example. Your code can do whatever it wants withe.
Assume that you have created an ArrayBuffer of Ints called ab1 and an ArrayBuffer of Pictures called ab2.
Example:
repeatFor(ab1) { n =>
println(n)
}
repeatFor(ab2) { p =>
draw(p)
}
Note – A HashMap is a type of Map (mapping a key to a value). In other languages, HashMaps are variously known as Maps, Dictionaries, etc.
A HashMap is a collection of key-value pairs. You can add key-value pairs to a HashMap, and quickly look up the values for given keys.
Here is a pictorial representation of a HashMap:

The following is a description of the primary operations that you can do with a HashMap:
HashMap.empty[type1, type2]- creates an empty HashMap which will hold keys oftype1and values oftype2.
Example:
HashMap.empty[String, Int]
HashMap.empty[Picture, Vector2D]
2.2 Addition of key, value pairs
hm(key) = value- adds the pair(key, value)to a HashMap calledhm.
Assume that you have created a HashMap of String to Int called hm1 and a HashMap of Pictures to Vector2Ds called hm2, and that you have Pictures called pic1 and pic2.
Example:
hm1("Aditya") = 12
hm1("Ushi") = 12
hm2(pic1) = Vector2D(1, 1)
hm2(pic2) = Vector2D(12, 13)
2.3 Lookup of values based on keys
hm(key)- gives you the value that corresponds to the givenkeyin a HashMap calledhm.
Assume that you have created a HashMap of String to Int called hm1 and a HashMap of Pictures to Vector2Ds called hm2, and that you have Pictures called pic1 and pic2.
Example:
println(hm1("Aditya"))
println(hm1("Ushi"))
pic1.translate(hm2(pic1))
pic2.translate(hm2(pic2))
Note – A HashSet is a type of Set (an unordered collection of values).
A HashSet is an unordered collection of objects. Checking if an object is present in a set, or removing an object from a set - is very efficient.
Here is a pictorial representation of a HashSet:

The following is a description of the primary operations that you can do with a HashSet:
HashSet.empty[type]- creates an empty HashSet which will hold elements of the giventype.
Example:
HashSet.empty[String]
HashSet.empty[Picture]
hs.add(value)- addsvalueto a HashSet calledhs.
Assume that you have created a HashSet of String called hs1 and a HashSet of Picture called hs2, and that you have Pictures called pic1 and pic2.
Example:
hs1.add("Aditya")
hs1.add("Ushi")
hs2.add(pic1)
hs2.add(pic2)
3.3 Iteration over all the elements
As mentioned earlier, iterating over the elements means going over all the elements (to do something with each)
repeatFor(hs) { e => /* do whatever with e /* }- iterators over all the elements of a HashSet calledhs. While iterating, the current element is calledein the given example. Your code can do whatever it wants withe.
Assume that you have created a HashSet of Strings called hs1 and a HashSet of Pictures called hs2.
Example:
repeatFor(hs1) { n =>
println(n)
}
repeatFor(hs2) { p =>
draw(p)
}
hs.remove(value)- removesvaluefromhs.
Assume that you have created a HashSet of String called hs1 and a HashSet of Picture called hs2, and that you have Pictures called pic1 and pic2 that you have already put inside hs2.
Example:
hs1.remove("Aditya"))
hs2.remove(pic1)
hs2.remove(pic2)
hs(value)orhs.contains(value)- tells you whethervalueis present inhs.
Assume that you have created a HashSet of String called hs1 and a HashSet of Picture called hs2, and that you have Pictures called pic1 and pic2.
Example:
println(hs1("Aditya"))
println(hs1.contains("Ushi"))
println(hs2(pic1))
println(hs2.contains(pic2))
Copyright © 2010–2025 Kogics Foundation. Licensed as per Terms of Use.