Professional Documents
Culture Documents
05 Maps
05 Maps
• Maps
• Iteration
1
Maps
• Collection of data, but not of single values:
• Map = set of pairs of keys to values
• Constrained access: get values via keys.
• Lots of implementations, most common is HashMap.
put(“Lady Greystoke”, Kathleen Kirkham)
get(“Jane Porter”)
put(“Lady Greystoke”, Anne)
“Jane Porter” ⇒ Enid Markey
2
Maps
• When declaring and constructing, must specify two types:
• Type of the key, and type of the value
private Map<String, String> movieCast; // Could be say <String,
Integer>
:
movieCast = new HashMap<String, String>();
• Central operations:
• get(key), ⇒ the value associated with key
• put(key, value), ⇒ sets value to be associated with key
(and returns the old value, if any)
• remove(key), ⇒ removes key and associated value
(and returns the old value, if any)
• containsKey(key),
• size()
• entrySet ⇒ a Set of Map.Entry’s that you can iterate along.
3
Example: movieCast
private Map<String, String> movieCast;
:
public void lookup(){
String name = askName(“Character to look up");
if (movieCast.containsKey(name))
textArea.setText(name +" : "+movieCast.get(name));
else
textArea.setText("No entry for "+ name);
}
public void update(){
String name = askName(“Character to update");
String old = movieCast.put(name, askName(“Actor who played
"+name));
if (old==null)
textArea.setText(name +" added as "+movieCast.get(name));
else
textArea.setText(name +" : "+old+" to "+movieCast.get(name));
}
4
Example: movieCast
public void readMovieCast(){
try {
movieCast = new HashMap<String,String>();
File filename = new File("movieCast.txt");
if(filename.exists()){
Scanner sc = new
Scanner(filename).useDelimiter(Pattern.compile(",|\n"));
while (sc.hasNext()){
movieCast.put(sc.next(), sc.next());
}
sc.close();
}
}
catch(IOException ex) { … }
}
5
Iteration & “for each” loop
• How does the “for each” loop work:
for (Face face : crowd) {
face.render(canvas);
}
for (Map.Entry<String,Integer> entry : movieCast.entrySet()){
textArea.append(entry.getKey()+” as ”+entry.getValue());
}
John
“Jane Porter” ⇒ Enid Markey
6
Why Iterators
• Program cannot get inside the Collection object
• “for each” loop uses Iterators
Program
hasNext
Iterator Collection
for (Face f : crowd){
next Data
}
7
Iterators
• Operations on Iterators:
• hasNext() : returns true iff there is another value to get
• next() : returns the next value
8
Iterators and Collections
• Every Collection can construct an Iterator:
• iterator() ⇒ an Iterator over the values
• (Maps ⇒ Sets ⇒ an Iterator over keys/values/Map.Entrys )
is equivalent to:
• The compiler translates the “for each” loop into the iterator
loop.
9
Iterating through a Map
• How do you iterate through a Map? (eg, to print it out)
• A Map isn’t just a collection of items!
⇒ could iterate through the collection of keys
⇒ could iterate through the collection of values
⇒ could iterate through the collection of pairs
• Java Map allows all three!
keySet returns a Set of Keys
• for (String name : movieCast.keySet()){….
• for (String actor : movieCast.values()){….
valueSet returns a Set of values
• for (Map.Entry<String, String> entry :
movieCast.entrySet()){….
entrySet returns a Set of ‘pairs’
… entry.getKey() …
… entry.getValue()…
15
Iterable and “for each”
16
Create your own Iterable
public class NumCreator implements Iterator<Integer>,
Iterable<Integer>{
private int lastNum = 1,
private int numbersToGo = 20; // how many numbers yet to deliver;
public Iterator<Integer> iterator(){
return this;
}
public boolean hasNext(){
return (numbersToGo > 0);
}
public Integer next(){
lastNum = (lastNum * 13) % 97 + 1;
numbersToGo--;
return lastNum;
}
• What else?
• Next we will start looking at Sorting Collections
18