lecture24-aug7 - Lecture 24 Announcements Assignment 4 due...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Lecture 24 Announcements z Assignment 4 – due today z Assignment 5 – out today z Exam 2 – in one week z Today’s topics Finish maps Finish maps Introduction to Graphs (Chapter 12) z Representation techniques z Traversals and searching z Common applications TreeMaps z Another Implementation of Map: TreeMap : a (balanced) BST storing entries – they can also be produced in ascending order by an inorder traversal Map example public class FamousBirthYear { public static void main(String[] args) { Map<String, Integer> m = new TreeMap <String, Integer>(); m.put("Newton", new Integer(1642)); m.put("Darwin", new Integer(1809)); m.put(”Krasner", new Integer(1947)); System.out.println(m); } } Output: Darwin = 1809, Krasner = 1947, Newton = 1642 TreeMap Constructors public class TreeMap<K,V> extends AbstractMap<K,V> implements SortedMap<K,V>, Cloneable, Serializable Constructor Summary z TreeMap() Constructs a new, empty map, sorted according to the keys' natural order z TreeMap(Comparator<? super K> c) TreeMap(Comparator<? super K> c) Constructs a new, empty map, sorted according to the given comparator z TreeMap(Map<? extends K,? extends V> m) Constructs a new map containing the same mappings as the given map, sorted according to the keys' natural order z TreeMap(SortedMap<K,? extends V> m) Constructs a new map containing the same mappings as the given SortedMap, sorted according to the same ordering Bank Accounts Map Example class AccountBalancesMapDemo { public static void main(String args[ ]) { Map<String, Double> acct = new TreeMap // Put elements into the map. acct.put("John Doe", new Double(3434.34)); acct.put("Tom Smith", new Double(123.22)); acct.put("Jane Baker", new Double(1378.00)); acct.put("Todd Hall", new Double(99.22)); acct.put("Ralph Smith", new Double(19.08)); // Get a set of the entries. a map for bank account balances by name (new TComp()); Set<Map.Entry<String, Double>> set = acct.entrySet(); // Display the elements. for(Map.Entry<String, Double> me : set) { System.out.print(me.getKey() + ": "); System.out.println(me.getValue()); } System.out.println(); // Deposit 1000 into John Doe's account. double balance = acct.get("John Doe"); acct.put("John Doe", balance + 1000); System.out.println("John Doe's new balance: " + acct.get("John Doe")); } } Output would be: Jane Baker: 1378.00 John Doe: 3434.34 Ralph Smith: 19.08 Todd Hall: 99.22 Tom Smith: 123.22 John Doe's new balance: 4434.34 Bank Accounts Map Ex. import java.util.*; // Compare last whole words in two strings, and if needed the first ones. // the strings are assummed to be of the form: firstname lastname class TComp implements Comparator <String> { public int compare (String a, String b) { int i, j, k; String aStr, bStr; aStr = a; bSt b bStr = b; // Find index of beginning of last name. i = aStr.lastIndexOf(' ') + 1; j = bStr.lastIndexOf(' ') + 1; k = aStr.substring(i).compareTo(bStr.substring(j)); if(k==0) // then the last names match, check entire name return aStr.compareTo(bStr); else return k; } }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Bank Accounts Map Example class AccountBalancesMapDemo { public static void main(String args[ ]) { Map<String, Double> acct = new TreeMap <String, Double> (new TComp()); // Put elements into the map.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/15/2009 for the course EE 322C taught by Professor Nettles during the Summer '08 term at University of Texas at Austin.

Page1 / 6

lecture24-aug7 - Lecture 24 Announcements Assignment 4 due...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online