Entrystring integer entry countsentryset if

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ng, Integer> counts){ String maxWord = null; int maxCount = -1; for (Map.Entry<String, Integer> entry : counts.entrySet() ){ if (entry.getValue() > maxCount){ maxCount = entry.getValue(); maxWord = entry.getKey(); } } return maxWord; } “String” ⇒ 5 “public” ⇒ 1 “counts” ⇒ 2 “Map” ⇒ 2 “findMaxCount” ⇒ 1 Map.Entry<K,V> - getKey() - getValue() 7 Another example of Map private Map<String, String> movieCast; // character → actor : 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 actor =askName(“Actor who played "+name); String old = movieCast.put(name, actor); if (old==null) textArea.setText(" added "+name +" played by " + actor); else textArea.setText(" replaced "+old+" by "+actor+ " for " + name)); 8 Queues • Queues are like/unlike Stacks • Collection of values with an order • Constrained access: • Only remove from the front • Two varieties: • Ordinary queues only add at the back queues: • Priority queues add or remove with a given priority queues: 9 Queues • Used for • Operating Systems, Network Applications, Multi-user Systems • Handling requests/events/jobs that must be done in order • (often called a “buffer” in this context) • Simulation programs • Representing queues in the real world (traffic, customers, deliveries, ….) • Managing events that must happen in the future • Search Algorithms • Computer Games • Artificial Intelligence • Java provides • a Queue interface • several classes: LinkedList, PriorityQueue LinkedList, 10 Queue Operations • offer offer(value) ⇒ boolean • add a value to the queue • (sometimes called “enqueue” ) • poll ⇒ value poll() • remove an...
View Full Document

This note was uploaded on 12/07/2012 for the course COMPUTER S CS10425 taught by Professor Shengweiguan during the Spring '12 term at University of Liverpool.

Ask a homework question - tutors are online