So far, I’ve only talked about single values, but in programming, you often have to work with large collections of values. For this, we have many data structures that are built into the language. These are similar for Java, Groovy, Scala, and even JavaScript.
Arrays
An array is a fixed size collection of data values.
As you can see, the first index of an array is zero. Things tend to start at zero when programming; try to remember this.
Patient 0 Here’s a helpful metaphor: the first person to start an outbreak (a zombie outbreak, for example) is known as patient zero, not patient one. Patient one is the second person infected.
The Array object in JavaScript is more like a Java List. Java arrays are a somewhat low-level structure used only for performance reasons. In Groovy 3, the Java-style declaration of arrays is supported. In previous versions you would have to use the List style, which we cover next.
Lists
Of course, we don’t always know how many elements we need to store in an array. For this reason (and many others), programmers invented List, a re-sizable collection of ordered elements.
The class between the first angle brackets (<>) defines the generic type of the list—what can go into the list (in this case it is Vampire). The second set of angle brackets can be empty since Java can infer the generic type from the left side of the expression. You can now add vampires to this list all day, and it will expand, as necessary in the background.
size(): Gets the size of List
get(int index): Gets the value at that index
remove(int index): Removes the value at that index
remove(Object o): Removes the given object
isEmpty(): Returns true only if List is empty
clear(): Removes all values from List
In Java, List is an interface (we’ll cover interfaces in depth in Chapter 8) and has many different implementations, but here are two:
java.util.ArrayList
java.util.LinkedList
The only difference you should care about is that, in general, LinkedList grows faster when inserting a value at an arbitrary index, while ArrayList’s get() method is faster at an arbitrary index.
You’ll learn how to loop through lists, arrays, and sets (and what “loop” means) in the next chapter. For now, just know that lists are a fundamental concept in programming.
Groovy Lists
Scala Lists
Also, this actually creates a new list, instead of modifying the existing list (it reuses the existing list in the background for performance reasons). This is because the default List in Scala is immutable, meaning it cannot be modified (the default implementation is immutable, but you can use a mutable implementation from the scala.collection.mutable package). Although this may seem strange, in conjunction with functional programming, it makes parallel programming (programming for multiple processors) easier as we’ll see in Chapter 10.
JavaScript Arrays
As mentioned earlier, JavaScript uses Array1 instead of List. Also, since JavaScript is not strictly typed, an Array can always hold objects of any type.
Sets
Set<E> is much like List<E>, but each value or object can only have one instance in the Set , whereas in previous collections, there can be repeats.
To preserve insertion order, you can use a LinkedHashSet<E> which uses a doubly linked list to store the order of the elements in addition to a hash table to preserve uniqueness.
TreeSet will magically always be sorted in the proper order.
Okay, it’s not really magic. The object to be sorted must implement the Comparable interface, but you haven’t learned about interfaces yet (interfaces are covered in Chapter 8).
Maps
containsKey(Object key): Returns true, if this map contains a mapping for the specified key
containsValue(Object value): Returns true, if this map maps one or more keys to the specified value
keySet(): Returns a Set view of the keys contained in this map
putAll(Map m): Copies all of the mappings from the specified map to this map
remove(Object key): Removes the mapping for a key from this map, if it is present
Groovy Maps
Scala Maps
As with List and Set, Scala’s default Map is also immutable.
JavaScript Maps
You could then use either of the following to access map values: map.Smaug or map["Smaug"].
Summary
Arrays: Collections of data with a fixed size
Lists: An expandable collection of objects or values
Sets: An expandable collection of unique objects or values
Maps: A dictionary-like collection