Title: Some of Java's Collection Framework
1Some of Java's Collection Framework
2Java's Collection Framework
- Collection framework
- Unified architecture for representing and
manipulating collections - Java's collection framework contains
- Interfaces (ADTs) specification not
implementation - Concrete implementations as classes
- Polymorphic Algorithms to search, sort, find,
shuffle, ... - The algorithms are polymorphic
- the same method can be used on many different
implementations of the appropriate collection
interface. In essence, algorithms are reusable
functionality.
3ADTs specifies as interfaces in java.util
Image from the Java Tutorial
4Abstract Data Type
- Abstract data type (ADT) is a specification of
the behaviour (methods) of a type - Specifies method names to add, remove, find
- Specifies if elements are unique, indexed,
accessible from only one location, mapped,... - An ADT shows no implementation
- no structure to store elements, no implementations
5Collection Classes
- A collection class the can be instantiated
- implements an interface as a Java class
- implements all methods of the interface
- selects appropriate data structures
- A few of Java's concrete collection classes
- StackltEgt
- ArrayListltEgt, LinkedListltEgt
- LinkedBlockingQueueltEgt, ArrayBlockingQueueltEgt
- HashSetltEgt, TreeSetltEgt
- TreeMapltK,Vgt, HashMapltK,Vgt public Collection
values()
6Common Functionality
- Collection classes often have methods for
- Adding objects
- Removing an object
- Finding a reference to a particular object find
- can then send messages to the object still in the
collection
7ListltEgt, an ADT written as a Java interface
- ListltEgt a collection with a first element, a
last element, distinct predecessors and
successors - The user of this interface has precise control
over where in the list each element is inserted - duplicates that "equals" each other are allowed
- The List interface is implemented by these three
collection classes - ArrayListltEgt
- LinkedListltEgt
- VectorltEgt
8- import java.util. // For List, ArrayList,
Linked ... - import static org.junit.Assert.
- import org.junit.Test
- public class ThreeClassesImplementList
- _at_Test
- public void showThreeImplementationsOfList()
- // Interface name List
- // Three classes that implement the List
interface - ListltStringgt bigList new ArrayListltStringgt()
- ListltStringgt littleList new
LinkedListltStringgt() - ListltStringgt sharedList new
VectorltStringgt() - // All three have an add method
- bigList.add("in array list")
- littleList.add("in linked list")
- sharedList.add("in vector")
9Iterators
- Iterators provide a general way to traverse all
elements in a collection - ArrayListltStringgt list new
ArrayListltStringgt() - list.add("1-FiRsT")
- list.add("2-SeCoND")
- list.add("3-ThIrD")
-
- IteratorltStringgt itr list.iterator()
- while (itr.hasNext())
- System.out.println(itr.next().toLowerCase())
-
Output 1-first 2-second 3-third
10Enhanced for Loop
- The for loop has been enhanced to iterate over
collections - General form
- for (Type element collection)
- element is the next thing visited each
iteration -
- for (String str list)
- System.out.println(str " ")
11Can't add the wrong type
- Java 5 generics checks the type at compile time
- See errors early--a good thing
- "type safe" because you can't add different types
- ArrayListltGregorianCalendargt dates
- new ArrayListltGregorianCalendargt()
- dates.add(new GregorianCalendar()) // Okay
- dates.add("String not a GregorianCalendar") //
Error - ArrayListltIntegergt ints new ArrayListltIntegergt()
- ints.add(1) // Okay. Same as add(new
Integer(1)) - ints.add("Pat not an int")) // Error
12TreeSet implements Set
- SetltEgt An interface for collections with no
duplicates. More formally, sets contain no pair
of elements e1 and e2 such that e1.equals(e2) - TreeSetltEgt This class implements the Set
interface, backed by a balanced binary search
tree. This class guarantees that the sorted set
will be in ascending element order, sorted
according to the natural order of the elements as
defined by ComparableltTgt
13Set and SortedSet
- The SetltEgt interface
- add, addAll, remove, size, but no get!
- Two classes that implement SetltEgt
- TreeSet values stored in order, O(log n)
- HashSet values in a hash table, no order, O(1)
- SortedSet extends Set by adding methods E
first(), SortedSetltEgt tailSet(E fromElement), - SortedSetltEgt headSet(E fromElement), E
last(), SortedSetltEgt subSet(E fromElement,
E toElement)
14TreeSet elements are in order
- SetltStringgt names new TreeSetltStringgt()
- names.add("Sandeep")
- names.add("Chris")
- names.add("Kim")
- names.add("Chris") // not added
- names.add("Devon")
- for (String name names)
- System.out.println(name)
- Output?
- Change to HashSet
15Hash Tables
- A Map is often implemented with a hash table
- Hash tables provide virtually direct access to
objects based on a key (String or Integer) - key could be your SID, your telephone number,
social security number, account number, - The direct access is made possible by converting
a String or Integer key to an array index
16Hashing
- A key such as "555-1234" results in an integer
index from 0 to an array upper bound index - Elements can be found, inserted, and removed
using the integer index as an array index - A hash table uses the same "address calculator"
to (insert), get (find), and remove - Could convert a String key to 4 like this
- "able" 97 98 108 101 404
- array index 404 array capacity
17Hash Function
- Ideally, every key has a unique hash value
- Then the hash value could be used as an array
index, however .... - Cannot rely on every key "hashing" to a unique
array index - can get "close"
- need a way to handle "collisions"
- "abc" may hash to the same integer as "cba"
18Hash function works something like this
Convert a String key into an integer that will be
in the range of 0 through the maximum capacity-1
Assume the array
capacity is 9997
hash(key)
AAAAAAAA
8482
1273
zzzzzzzz
hash(key)
Domain "!" .. "zzzzzzzz"
Range 0 ... 9996
19Hash Function
- What if the ASCII value of individual chars of
the string key added up to a number from ("A") 65
to possibly 488 ("zzzz") 4 chars max - If the array has size 309, mod the sum
- 390 TABLE_SIZE 81
- 394 TABLE_SIZE 85
- 404 TABLE_SIZE 95
- Keys converted to array indices could be
81 85 95
"abba" "abcd" "able"
20A "too simple" hash function
- // Return an int in the range of 0..TABLE_SIZE-1
- public int hash(String key)
- int result 0
- int n key.length()
- for (int j 0 j lt n j)
- result key.charAt(j) // add up the chars
- return result TABLE_SIZE
-
- public static final int TABLE_SIZE 309
- _at_Test
- public void testHashFunction()
- assertEquals(81, hash("abba"))
- assertEquals(81, hash("baab"))
- assertEquals(85, hash("abcd"))
- assertEquals(86, hash("abce"))
- assertEquals(308, hash("IKLT"))
- assertEquals(308, hash("KLMP"))
21Collisions
- A good hash method
- executes quickly
- distributes keys equitably (see String hashCode)
- But you still have to handle collisions when two
keys have the same hash value - the hash method is not guaranteed to return a
unique integer for each key - example simple hash method with "baab" and
"abba" - There are several ways to handle collisions
- Consider separate chaining hashing
22An Array of LinkedList Objects
0
321
365
1
2
23Insert is Easy
- public class MyHashTable
- private final static int TABLE_SIZE 10
- private LinkedList table
- public MyHashTable()
- table new LinkedListTABLE_SIZE
- for (int j 0 j lt TABLE_SIZE j)
- tablej new LinkedList()
-
- public void put(String key, Comparable x)
- int pos hash(key)
- // Place the element in the correct list
- tablepos.add(x)
-
24Insert Six Objects
- MyHashTableltString, BankAccountgt h
- new MyHashTableltString,
BankAccountgt() - BankAccount a1 new BankAccount("abba",
100.00) - BankAccount a2 new BankAccount("abcd",
200.00) - BankAccount a3 new BankAccount("abce",
300.00) - BankAccount a4 new BankAccount("baab",
400.00) - BankAccount a5 new BankAccount("KLMP",
500.00) - BankAccount a6 new BankAccount("IKLT",
600.00) - // Insert BankAccount objects using ID as the
key - h.put(a1.getID(), a1)
- h.put(a2.getID(), a2)
- h.put(a3.getID(), a3)
- h.put(a4.getID(), a4)
- h.put(a5.getID(), a5)
- h.put(a6.getID(), a6)
- h.display()
25The Map Interface (ADT)
- Map describes a type that stores a collection of
elements that consists of a key and a value - A Map associates (maps) a key the it's value
- The keys must be unique
- the values need not be unique
- put destroys one with same key
26Map Operations
- Java's HashMapltK, Vgt
- public V put(K key, V value)
- associates key to value and stores mapping
- public V get(Object key)
- associates the value to which key is mapped or
null - public boolean containsKey(Object key)
- returns true if the Map already uses the key
- public V remove(Object key)
- Returns previous value associated with specified
key, or null if there was no mapping for key. - CollectionltVgt values()
- get a collection you can iterate over
27Code Demo Rick Put in a file named
HashMapDemo.java
- Add some mappings to a HashMap and iterate over
all elements with CollectionltVgt values() and all
keys with SetltKgt keySet()
28Algorithms
- Java has polymorphic algorithms to provide
functionality for different types of collections - Sorting (e.g. sort)
- Shuffling (e.g. shuffle)
- Routine Data Manipulation (e.g. reverse, addAll)
- Searching (e.g. binarySearch)
- Composition (e.g. frequency)
- Finding Extreme Values (e.g. max)
- Demo a few with ArrayList