Baseball Stats Data Model Example

view source
ERROR: Install Java plugin to see this applet.

What are Data Structures

Deepak's Case

Maps as a Model of Mathematical Functions

Recall that a function in mathematics is a mapping from parameters to values. Consider the function
ƒ(x) = x / 2
Suppose for the sake of this example that we only allow whole numbers as parameters of ƒ, but we're OK with the fact that the values are non-whole numbers.

A listing of parameters and values of this function would be...
Parameter Value
The Map data type is an attempt to "capture" (read: represent) this notion of a function within a program, where we define the function as a listing of its parameter/value pairs.

Working with Maps

In general, we can create a Map in Java by doing the following...
   Map<ParameterType, ValueType> myMap = new HashMap<ParameterType, ValueType>();
Notice that the type before the declaration is Map, but after the new keyword the type is HashMap. Forget about why this is, just know that you have to do it.

Once you have the above we can add a parameter/value pair to our map by doing the following...
   myMap.add( parameter1, value1 );
   myMap.add( parameter2, value2 );
And we can retrieve the value of the map (read: function) at a particular parameter by simply doing...
   ValueType v = myMap.get(parameter);

A Concrete Example

Suppose we wanted to create a Map to mimic the function above
ƒ(x) = x / 2
We could do this in Java by the following...
   // create the map
   Map<int, double> myMap = new HashMap<int, double>();
   // add some parameter/value pairs
   myMap.add( 0, 0.0 );
   myMap.add( 1, 0.5 );
   myMap.add( 2, 1.0 );
   myMap.add( 3, 1.5 );
   myMap.add( 4, 2.0 );
Then, to determine what the value of ƒ(3) is, we could do the following
   double value = myMap.get(3);
(value would be set to 1.5)

How Map's are used in this code

Whenever we're working with large amounts of data, as Deepak is, we have to figure out a way to represent that day. In this case, Map's prove usefull....

Here's what we have to represent... To see how this makes things a bit easier, take a look at the code and notice that we don't have many variables, and we don't have to manually relate each team with a set of players (at least not how we would have to do it with arrays). The map does this for us.