CSE 214 – Computer Science II Maps Source: http://contrailscience.com/wp-content/uploads/usa-flightpaths750.jpg
Maps An abstract data structure for storing (key, value) data pairs called (name, value) on Web maps may also be called dictionaries, associative arrays, etc. What’s a key? a unique identifier for accessing a piece of data like an ID What’s a value? the data
You have already built a type of map PresidentsBST is a Map Each president has a unique ID president number ID provides a natural sorting/ordering Only difference: we didn’t call it a map we didn’t adhere to a specific map interface
Game Programmers Love Maps Why? To store commonly used assets Example: 3D Model of a dragon suppose we need to render 3 or more identical dragons same geometry, same textures, same animations, etc.
What do we do? Construct Dragon object for each one Instead: will keep track of location, animation state, damage, etc. don’t load dragon artwork 3 times Instead: load dragon artwork once store it in a dictionary (map) Optimized for fast access (log N) give map key of dragon to all Dragon objects At render time: retrieve 3D dragon model from dictionary using ID render the 3 models
The java.util.Map interface Map methods: V get(K key, V value) V put(K key, V value) V remove(Object key) Colletions<V> values() – to help with getting an Iterator Manages Map.Entry objects
The java.util.Map.Entry Interface A Map.Entry has: 2 variables Object: key Object: value accessors for getting key & value mutator for setting value
Ex: HashMap vs. TreeMap Both are maps Both classes implement Map both have the same methods Underlying data structures are different HashMap is a hash table TreeMap is a red-black tree a self-balancing binary search tree
So what are we using maps for? Storing our Airports a lookup dictionary Why? because we have a lot of data fast way of getting this info
Our updated AirportGraph public class AirportGraph { // THESE ARE THE NODES IN THE GRAPH public TreeMap<String,Airport> vertices = new TreeMap<String,Airport>(); // THESE ARE THE CONNECTIONS BETWEEN THE NODES // FOR EACH AIRPORT CODE, WE HAVE A SORTED Vector // OF THE ADJACENT AIRPORT CODES public TreeMap<String,Vector<String>> edges = new TreeMap<String,Vector<String>>();
How might we find a shortest path? This is a bonus part of the assignment We can do this by remembering shortest paths to other nodes Let’s do another example
Depth First Search vs. Breadth First Search Again, let’s do an example