Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sets and Maps Computer Science 4 Mr. Gerb Reference: Objective: Understand the two basic applications of searching.

Similar presentations


Presentation on theme: "Sets and Maps Computer Science 4 Mr. Gerb Reference: Objective: Understand the two basic applications of searching."— Presentation transcript:

1 Sets and Maps Computer Science 4 Mr. Gerb Reference: Objective: Understand the two basic applications of searching.

2 Searching The majority of data structures we will study in this course are for the purpose of searching. –Involves storing information about a piece of data in the data structure –Must be able to retrieve the information later There are two major applications for searching: Sets and Maps

3 Sets and Maps Sets –The simplest application of searching –Place an item into a data structure –Ask the question about an item, “is it in the data structure?” Maps –Store a pair, a key and information about the key in a data structure –Ask if a key is in the data structure (basically, a set) –Retrieve information stored about a key

4 Examples Set Example: Dictionary for a spell checker –Want to ask whether a word is in the dictionary –Want to add a word to the dictionary Map Example: Students’ names and addresses –Given a student’s name, want to be able to get his/her address –Want to add a new student with address –Want to remove a student (for drop-out or xfer) –Want to change a student’s address –Want to see if a student is enrolled

5 Operations on Set ADT void clear() - Empty the set. boolean add(Object obj) - Add to the set, returns whether it is a new item boolean contains(Object obj) - In the set? boolean isEmpty() - Nothing in the set? boolean remove(Object obj) - Remove from the set. Returns true if it was in the set. int size() - Number of items in the set. Iterator iterator() - Iterate over all set members Note that Sets are templated. Can follow them with the type of item they stored in angle brackets (<>).

6 Operations on Map ADT clear, isEmpty, and size operations as in set. Object put(Object key, Object value) - Add key/value mapping. Return previous value mapped to key or null if none. Object get(Object key) - Return value key maps to. Object remove(Object key) - Remove object from map. Return mapped value (or null if none). boolean containsKey(Object key) - Key in map? Iterator Like Sets, Maps are templated. Can follow them with a pair of types in angle brackets.

7 Equals In order to retrieve an item, must have the concept of when two items are equal –E.g. is “George W. Bush” the same item as “George H. W. Bush” –Is “USA” the same as “U.S.A.”? Maps and sets use the equals() method –Defined for any kind of object –Can specialize it if necessary When adding an item to a set or map, checks if it is equal to an item already there. –If so, replace (map) or do nothing (set) –If not, add the new member/mapping.

8 Set Example: States bordering MD Set tomd=new Set (); boolean b; b=tomd.add(“VA”); b=tomd.add(“WV”); b=tomd.add(“PA”); b=tomd.add(“DE”) b=tomd.add(“OH”); print(tomd.contains(“VA”)); //True print(tomd.contains(“NJ”)); //False tomd.remove(“OH”);//OOPS! Not Ohio print(tomd.size());//4 tomd.clear(); print(tomd.size());//0

9 Map Example: State Capitals Map caps=new Map (); Object ob; ob=caps.put(“MD”,”Annapolis”); ob=caps.put(“PA”,”Harrisburg”); ob=caps.put(“NY”,”New York City”); print(caps.get(“PA”));//Harrisburg print(caps.get(“WV”));//null, not in map ob=caps.put(“NY”,”Albany”); print(ob);//New York City (old value) print(caps.containsKey(“MD”)); //True print(caps.size()); //3 ob=caps.remove(“PA”); print(ob); //Harrisburg

10 Summary Searching involves retrieving information about an object Two main applications –Sets - Does this belong –Map - What information is stored with this? Need to be able to tell if two object are equal –All objects in Java implement the equals() method.


Download ppt "Sets and Maps Computer Science 4 Mr. Gerb Reference: Objective: Understand the two basic applications of searching."

Similar presentations


Ads by Google