- Forward


An Introduction to Collections
in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Review
Back SMYC Forward
  • An Array:
    • A fixed-size collection of homogeneous objects that are indexed by an integer
  • Limitations of Arrays:
    • Fixed size
    • Must be indexed by an integer
When These Limitations Arise
Back SMYC Forward
  • Fixed Size:
    • When we don't know even the maximum number of elements in the collection beforehand
  • Integer Index:
    • When the indices aren't numbers (e.g., names)
    • When the "important" integers are sparse (e.g., phone numbers)
Beyond Arrays
Back SMYC Forward
  • Collection:
    • An object that "groups" multiple elements into a single unit
  • Examples in Java:
    • List
    • Map
UML
Back SMYC Forward
  • Aggregation:
    • An object of class A "has-an" object of class B if B is a part of A.
  • Obvious Applications to Collections:
    • aggregation-collection
  • Less Obvious Applications:
    • aggregation-university
The java.util.ArrayList Class
Back SMYC Forward
  • Purpose:
    • A "growable array" with an integer index
  • Important Methods:
    • void add(int index, Object element)
    • Object get(int index)
    • int size()
The ArrayList Class (cont.)
Back SMYC Forward

An Example

javaexamples/collections/basics/TextFormatter.java
 
The java.util.ArrayList Class (cont.)
Back SMYC Forward
  • Some Convenient Methods:
    • void add(Object element)
    • void clear()
  • Some Methods to Avoid For Now:
    • boolean contains(int index, Object element)
    • int indexOf(Object element)
The java.util.HashMap Class
Back SMYC Forward
  • Purpose:
    • A "growable array" that can use an object as an index/key
  • Important Methods:
    • Object get(Object key)
    • Object put(Object key, Object element)
    • int size()
  • What Does put() Return:
    • The previous value associated with the key (or null) if there was no such value
The HashMap Class (cont.)
Back SMYC Forward

An Example

javaexamples/collections/basics/CallerID.java
 
An Important Observation
Back SMYC Forward
  • Parameters of add() (in ArrayList) and put() (in HashMap):
    • A Object
  • Implication:
    • An ArrayList and a HashMap can only contain objects
    • They can't contain an int, double, etc...
Wrapper Classes
Back SMYC Forward
  • Integer:
    • Constructor: Integer(int value)
    • Accessor: int intValue()
  • Double:
    • Constructor: Double(double value)
    • Accessor: double doubleValue()
Wrapper Classes (cont.)
Back SMYC Forward

An Example

javaexamples/collections/basics/StatisticsCalculator.java
 
Wrapper Classes (cont.)
Back SMYC Forward
  • Some Observations:
    • Wrapper classes are inconvenient
    • It seems like the process could be handled by the compiler
  • The Solution:
    • Autoboxing - the Java compiler will automatically box and unbox in many situations
Using the Value as the Key in a HashMap
Back SMYC Forward
  • The Motivation:
    • They key is normally an Object that uniquely identifies the value Object
    • Sometimes the value Object does not have an attribute (or attributes) that can be used for this purpose
  • Some Observations:
    • The Java interpreter has a way of uniquely identifying each object - its address
    • Each Object also has a Object.hashCode() method that can be used for this purpose (which can be based on the address but needn't be)
Using the Value as the Key (cont.)
Back SMYC Forward
  • The Result:
    • Sometimes the key and the value are the same
  • An Example:
    • A "paint" application in which you add a bunch of different shapes to the canvas
  • What To Do?
    • You can use a HashMap, but you can also use a HashSet
Using the Value as the Key (cont.)
Back SMYC Forward

An Example Using a HashSet

javaexamples/collections/basics/ShapeCanvas.java (Fragment: 0)
 
The Iterator Pattern - Motivation
Back SMYC Forward
  • Computers vs. Calculators:
    • Computers can perform the same operation many times
  • Programming Languages:
    • Harness this power using loops
  • A Problem:
    • Traditional looping requires an understanding of the structure of the collection/aggregate
Iterator - Looping Over an Array
Back SMYC Forward
javaexamples/iterator/ArrayExample.java (Fragment: 1)
 
Iterator - Looping Over an ArrayList
Back SMYC Forward
javaexamples/iterator/ArrayListExample.java (Fragment: 1)
 
Iterator - Looping Over a HashMap
Back SMYC Forward

We don't know all of the keys unless we keep them in another collection

Iterator - Some Observations
Back SMYC Forward
  • Applications often "loop" over the same collection/aggregate object in many classes and methods
  • Changing from one colelction/aggregate to another is, as a result, very inconvenient
  • The Iterator design pattern enables us to access the elements of an aggregate object while hiding its internal structure
Iterator - Important Operations
Back SMYC Forward
  • Reset its "pointer" (or cursor) to the first element.
  • Determine if there are any more elements in the sequence.
  • Move its "pointer" to the next element.
  • Retrieve the "current" element.
The Iterator Pattern in UML
Back SMYC Forward
iterator
Iterator: Examples in Java
Back SMYC Forward
  • The Iterator:
    • Iterator
  • Some Aggregates:
    • ArrayList
    • HashSet
Iterator: An Example of the Benefits
Back SMYC Forward
javaexamples/collections/basics/IteratorExample.java
 
There's Always More to Learn
Back -