Download presentation
Presentation is loading. Please wait.
Published byLindsay Stafford Modified over 9 years ago
1
Nov 22, 2010IAT 2651 Java Collections
2
Nov 22, 2010IAT 2652 Data Structures With a collection of data, we often want to do many things –Organize –Iterate –Add new –Delete old –Search
3
Nov 22, 2010IAT 2653 Data Structures Some are built to enable fast searching Some enable fast insertion/deletion –WhatLnkListTreeHashTable –Store LightLess lightMedium –Iterate simplemoderatedifficult –AddO(1)O( lgN )O(1) –DeleteO(1)O( lgN + )O(1) –SearchO(n)O(lgN)O(1)
4
Nov 22, 2010IAT 2654 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 Hash Table keyentry Key hash function array index 4 10 123
5
Nov 22, 2010IAT 2655 insert: compute location, insert TableNode; O(1) find: compute location, retrieve entry; O(1) remove: compute location, set it to null; O(1) Hashing keyentry 4 10 123
6
Nov 22, 2010IAT 2656 Hashing example 10 stock details, 10 table positions keyentry 0 1 2 3 4 5 6 7 8 9 Stock numbers between 0 and 1000 Use hash function: stock no. / 100 What if we now insert stock no. 350? –Position 3 is occupied: there is a collision Collision resolution strategy: insert in the next free position (linear probing) 85 85, apples 462 462, pears 912 912, papaya 323 323, guava 350 350, oranges Given a stock number, we find stock by using the hash function again, and use the collision resolution strategy if necessary
7
Nov 22, 2010IAT 2657 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
8
Nov 22, 2010IAT 2658 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
9
Nov 22, 2010IAT 2659 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
10
Java Collections A Java Collection class is a class that stores and organizes data –Uses one of the algorithms mentioned in previous lectures Nov 22, 2010IAT 26510
11
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 Nov 22, 2010IAT 26511
12
Java Collections Framework Why? Reduces programming effort Increases program speed and quality Interoperable: You and I can pass data in collections between our code Nov 22, 2010IAT 26512
13
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) Nov 22, 2010IAT 26513
14
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) Nov 22, 2010IAT 26514
15
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 Nov 22, 2010IAT 26515
16
Using a List Declare: ArrayList aList = new ArrayList(6); Methods you use: SomeClass item, newItem, existingItem ; item = (SomeClass)aList.get( i ); aList.add( newItem ); aList.remove( existingItem ); Nov 22, 2010IAT 26516
17
Using a HashMap HashMap hm = 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.Entry me = (Map.Entry)I.next(); print( me.getKey() + " is " ); println( me.getValue() ); } Nov 22, 2010IAT 26517
18
Using a TreeMap TreeMap tm = 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()); } Nov 22, 2010IAT 26518
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.