180 likes | 355 Views
IAT 265. Java Collections. Data Structures. With a collection of data, we often want to do many things Organize Iterate Add new Delete old Search. Data Structures. Some are built to enable fast searching Some enable fast insertion/deletion What LnkList Tree HashTable
E N D
IAT 265 Java Collections IAT 265
Data Structures • With a collection of data, we often want to do many things • Organize • Iterate • Add new • Delete old • Search IAT 265
Data Structures • Some are built to enable fast searching • Some enable fast insertion/deletion • What LnkList Tree HashTable • Store Light Less light Medium • Iterate simple moderatedifficult • Add O(1) O( lgN ) O(1) • Delete O(1) O( lgN + ) O(1) • Search O(n) O(lgN) O(1) IAT 265
Hash Table • An array in which items are not stored consecutively - their place of storage is calculated using the key and a hash function • Hashed key: the result of applying a hash function to a key • Keys and entries are scattered throughout the array key entry 4 hash function array index Key 10 123 IAT 265
Hashing • insert: compute location, insert TableNode; O(1) • find: compute location, retrieve entry; O(1) • remove: compute location, set it to null; O(1) key entry 4 10 123 IAT 265
7 9 8 6 4 3 2 5 1 Hashing example • 10 stock details, 10 table positions key entry • Stock numbers between 0 and 1000 85 85, apples 0 • Use hash function: stock no. / 100 • What if we now insert stock no. 350? • Position 3 is occupied: there is a collision 323 323, guava 462 462, pears • Collision resolution strategy: insert in the next free position (linear probing) 350 350, oranges • Given a stock number, we find stock by using the hash function again, and use the collision resolution strategy if necessary 912 912, papaya IAT 265
Hashing performance • The hash function • Ideally, it should distribute keys and entries evenly throughout the table • It should minimize collisions, where the position given by the hash function is already occupied • The collision resolution strategy • Separate chaining: chain together several keys/entries in each position • Open addressing: store the key/entry in a different position • The size of the table • Too big will waste memory; too small will increase collisions and may eventually force rehashing (copying into a larger table) • Should be appropriate for the hash function used – and a prime number is best IAT 265
Applications of Hashing • Compilers use hash tables to keep track of declared variables • A hash table can be used for on-line spelling checkers — if misspelling detection (rather than correction) is important, an entire dictionary can be hashed and words checked in constant time • Hash functions can be used to quickly check for inequality — if two elements hash to different values they must be different • Storing sparse data IAT 265
When to use hashing? • Good if: • Need many searches in a reasonably stable table • Not So Good if • Many insertions and deletions, • If table traversals are needed • Need things in sorted order • More data than available memory • Use a tree and store leaves on disk IAT 265
Java Collections • A Java Collection class is a class that stores and organizes data • Uses one of the algorithms mentioned in previous lectures IAT 265
Java Collections Framework • A unified architecture for representing and manipulating collections: • Interfaces: The means by which you access a collection class • Independent of implementation details • Implementations: The machinery behind the interface that does the work • Concrete implementations • Algorithms: Algorithms that run on collection classes: Searching and sorting • Reusable Functionality IAT 265
Java Collections Framework • Why? • Reduces programming effort • Increases program speed and quality • Interoperable: You and I can pass data in collections between our code IAT 265
Interfaces • Collection • A group of objects (elements) • This is a generic idea, there are more detailed Collection types that you actually use • Set • A collection with no duplicate elements • SortedSet • A Set in ascending order • (Typically alphabetical or numeric order) IAT 265
Interfaces • List • An ordered collection • May contain duplicates • Can access by index (like ArrayList) • Queue • A collection that holds items in some order • Typically FIFO (First in/First Out) IAT 265
Interfaces • Map • An collection that maps keys to values • Think phone book • May contain duplicates • Can access by index (like ArrayList) • SortedMap • A Map in sorted order of keys IAT 265
Using a List Declare: ArrayListaList = new ArrayList(6); • Methods you use: SomeClass item, newItem, existingItem ; item = (SomeClass)aList.get( i ); aList.add( newItem ); aList.remove( existingItem ); IAT 265
Using a HashMap HashMaphm = new HashMap(); hm.put( "Ava", "555-8976" ); hm.put( "Mary", "555-1238" ); hm.put( "Joe", "555-2121" ); String phone = (String)hm.get( "Mary" ); Iterator I = hm.entrySet().iterator(); while( I.hasNext() ) { Map.Entryme = (Map.Entry)I.next(); print( me.getKey() + " is " ); println( me.getValue() ); } IAT 265
Using a TreeMap TreeMaptm = new TreeMap (); tm.put( "Ava", "555-8976" ); tm.put( "Mary", "555-1238" ); tm.put( "Joe", "555-2121" ); String phone = (String)tm.get( "Mary" ); Iterator I = tm.entrySet().iterator(); while( I.hasNext() ) { Map.Entry me = (Map.Entry)I.next(); print(me.getKey() + " is "); println(me.getValue()); } IAT 265