Building Java Programs: Chapter 10, 11 Lecture 22: 143 Preview
Building Java Programs: Chapter 10, 11 Lecture 22: 143 Preview
No method to find the index of a given object in an array Could use Arrays.sort and Arrays.binarySearch, but this could be inefficient No method to add/remove from the middle of the list
No method to find the index of a given object in an array Could use Arrays.sort and Arrays.binarySearch, but this could be inefficient No method to add/remove from the middle of the list
ArrayLists
Arrays that dynamically resize themselves to accommodate
ArrayList declaration
Arrays: type[] name = new type[length]; ArrayList: ArrayList<type> name = new ArrayList<type>();
Example:
You can mostly just use primitive types because of autoboxing and unboxing, but you must declare object types such as
ArrayList Methods
Method name add(obj) add(index, obj) contains(obj) Description Adds obj to the end of the list Adds obj at the specified index, shifting higher-index elements to make room Whether the list contains obj
get(i)
indexOf(obj) lastIndexOf(obj) remove(i) remove(obj) set(i, obj) size()
Copyright 2010 by Pearson Education
Cities revisited
Remember our Cities example?
City Seattle State Population Latitude Longitude WA 616627 47621800 -122350326
we just ignored.
plotted were from Why would this have been difficult with standard arrays?
Lets pick a different color for each state, and color all cities in
that state with that color Lets add that color to our legend as well How will we convert a state (String) to a color (3 ints)?
Copyright 2010 by Pearson Education
number representing that object The Random object has a constructor Random(seed)
The seed determines future random numbers
(red, green, and blue) We can use the states hash code to seed a Random object and then generate the red, green, and blue components of a Color.
This guarantees that for a given state, we will always generate
the same color, but different states will likely have different colors
Solution details
Our method converting String to Color
public static Color getColor(String state) {
Collections.sort(states); for (int i = 0; i < states.size(); i++) { String state = states.get(i); g.setColor(getColor(state)); g.drawString(state, x, y); // update x and y }
11
Problems
For large ArrayLists, contains can be inefficient We have to generate the Color from the state What if we wanted to associate an arbitrary Color with each state?
We could make parallel ArrayLists, that store Strings and Colors, but wed get thrown off when we sort the states for the legend We could create a new object type with a String and a Color field, but thats a lot of work (Collections wont be able to sort an ArrayList of an arbitrary type either)
12
Problems
For large ArrayLists, contains can be inefficient We have to generate the Color from the state What if we wanted to associate an arbitrary Color with each state?
We could make parallel ArrayLists, that store Strings and Colors, but wed get thrown off when we sort the states for the legend We could create a new object type with a String and a Color field, but thats a lot of work (Collections wont be able to sort an ArrayList of an arbitrary type either)
13
HashMaps
A data structure that associates keys and values The keys and values can be arbitrary types, but all the keys
must be the same type, and all the values must be the same type. The keys must be unique!
just ints
key value
"foo" 12
"bar" 49
"baz" -2
14
HashMap declaration
HashMap<key_type, value_type> name = new HashMap<key_type, value_type>();
Example:
You can mostly just use primitive types because of autoboxing and unboxing, but you must declare object types such as
HashMap Methods
Method name containsKey(obj) containsValue(obj) get(obj) keyset() put(key, val) remove(obj) Description Whether obj is a key in the map Whether obj is a value in the map Get the value associated with the key obj, null if key is not found Gets the Set of all the keys in the map Adds a key/value pairing to the map Remove the mapping for key obj, and return the value that was associated with it, null if key is not found The number of entries in the map Gets a Collection of all the values in the map
size() values()
16
Cities revisited
Well no longer have to generate a Color from a String We can just associate Strings and Colors and keys as
values in the map Without going into detail, for large data sets, adding, removing, and finding entries in a HashMap is faster than adding, removing, and finding elements in an ArrayList
ArrayList is an ordered list, while HashMap isnt. Maintaining
17
Solution details
Assume we have a HashMap<String, Color> called colors
18
for (String state : new TreeSet<String>(colors.keySet())) { g.setColor(colors.get(state)); g.drawString(state, x, y); // update x and y }
This is called a foreach loop. A TreeSet doesnt have
19