100 likes | 221 Views
159.234 OOP Lecture 33. Java Abstract Data Types java.util package. Java Advanced Data Structures. java.util package Use of abstract data types Linked List example HashTable example Like the C++ STL - only more powerful and more extensive. java.util package
E N D
159.234 OOP Lecture 33 • Java Abstract Data Types • java.util package OO Programming
Java Advanced Data Structures • java.util package • Use of abstract data types • Linked List example • HashTable example • Like the C++ STL - only more powerful and more extensive OO Programming
java.util package A Java library containing useful functions for storing dynamic arrays and for storing arrays with efficient access Unlike C++ STL generics, the Java containers can contain any Object - (all objects in Java descend from java.lang.Object) java.util.Vector is a variable-sized array. Vector has a default size. Can store any object type When it reaches its capacity, the size is incremented by a set amount Accessor methods exist to search/add/delete elements. java.util.HashTable implements efficient storage Stores elements according to their hashCode (a property of java.lang.Object inherited by all objects) OO Programming
public class LinkedList { public interface Linkable { public Linkable getNext(); // Returns next element in the list public void setNext(Linkable node); // Sets the next element in the list } Linkable head; // head of the linked list public synchronized Linkable getHead(){ return head; } public synchronized void insertAtHead(Linkable node) { node.setNext(head); head = node; } public synchronized void insertAtTail(Linkable node) { if (head == null) head = node; else { Linkable p, q; for(p = head; (q = p.getNext()) != null; p = q); p.setNext(node); } } public synchronized Linkable removeFromHead() { Linkable node = head; if (node != null) { head = node.getNext(); node.setNext(null); } return node; } public synchronized Linkable removeFromTail() { if (head == null) return null; Linkable p = head, q = null, next = head.getNext(); if (next == null) { head = null; return p; } while((next = p.getNext()) != null) { q = p; p = next; } q.setNext(null); return p; } public synchronized void remove(Linkable node) { if (head == null) return; if (node.equals(head)) { head = head.getNext(); return; } Linkable p = head, q = null; while((q = p.getNext()) != null) { if (node.equals(q)) { p.setNext(q.getNext()); return; } p = q; } } OO Programming
public static class Test { static class LinkableInteger implements Linkable { int i; // The data contained in the node Linkable next; // A reference to the next node in the list public LinkableInteger(int i) { this.i = i; } // Constructor method public Linkable getNext() { return next; } // Part of Linkable public void setNext(Linkable node) { next = node; }// Part of Linkable public String toString() { return i + ""; } // For easy printing public boolean equals(Object o) { // For comparison if (this == o) return true; if (!(o instanceof LinkableInteger)) return false; if (((LinkableInteger)o).i == this.i) return true; return false; } } /** * The test program. Insert some nodes, remove some nodes, then * print out all elements in the list. It should print out the * numbers 4, 6, 3, 1, and 5 **/ public static void main(String[] args) { LinkedList ll = new LinkedList(); // Create a list ll.insertAtHead(new LinkableInteger(1)); // Insert some stuff ll.insertAtHead(new LinkableInteger(2)); ll.insertAtHead(new LinkableInteger(3)); ll.insertAtHead(new LinkableInteger(4)); ll.insertAtTail(new LinkableInteger(5)); // Insert some more stuff ll.insertAtTail(new LinkableInteger(6)); System.out.println(ll.removeFromHead()); // Remove and print a node System.out.println(ll.removeFromTail()); // Remove and print another ll.remove(new LinkableInteger(2)); // Remove another one // Now print out the contents of the list. for(Linkable l = ll.getHead(); l != null; l = l.getNext()) System.out.println(l); } } } Linked List Example linkable objects example note filename mangling done for inner classes > java LinkedList$Test 4 6 3 1 5 > OO Programming
HashTable Example • hashtable worked example code • note use of HashObject as data structure • note use of dedicated exception handler • integrated test method • order of tests important to avoid nullPointerException • use java.util.HashTable for real applications • evolution of the language - deprecation - see example OO Programming
// // Hash table Example Program // class HashObject{ // data structure for holding list of key/value pairs String key; Object data; HashObject next; } class NoSuchKeyException extends Exception{ // exception handler public NoSuchKeyException( String s ){ super( s ); } public NoSuchKeyException(){ super(); } } public class HashTable{ // working class plus the test main method private HashObject table[]; private int size; private int rehashSize; private int capacityIncrement; private int count; public HashTable(){ table = new HashObject[23]; size = table.length; rehashSize = 4; capacityIncrement = 2; count = 0; } public void put( String key, Object data ){ HashObject obj = new HashObject(); obj.key = key; obj.data = data; obj.next = null; bucketAdd( table, getBucket(hashCode(key)), obj); count++; if( count > size * rehashSize ){ rehash(); } } public Object get( String key ) throws NoSuchKeyException { HashObject place = table[ getBucket( hashCode(key) ) ]; while( place != null && place.key.compareTo(key) != 0 ){ // note order place = place.next; } if( place == null ){ throw new NoSuchKeyException( key ); } return place.data; } public int hashCode( String key ){ int value = 0; byte keyBytes[] = new byte[key.length()]; key.getBytes( 0, key.length(), keyBytes, 0 ); for( int i = 0; i<keyBytes.length; i++){ value += (int)keyBytes[i]; } return value; } OO Programming
private int getBucket( int hash ){ return hash % size; } private void bucketAdd( HashObject table[], int bucket, HashObject obj ){ obj.next = table[ bucket ]; table[bucket] = obj; } private void rehash(){ int newSize = size * capacityIncrement; HashObject newTable[]; HashObject tmp, obj; if( newSize % 2 == 0 ){ newSize++; } newTable = new HashObject[newSize]; for(int i = 0; i< size; i++){ tmp = table[i]; while( tmp != null ){ obj = new HashObject(); obj.key = tmp.key; obj.data = tmp.data; obj.next = null; bucketAdd( newTable, hashCode(tmp.key) % newSize, obj ); tmp = tmp.next; } } size = newSize; table = newTable; } public static void main( String args[] ){ // main method for test code HashTable table = new HashTable(); for(int i = 0; i<5; i++){ table.put("STRING " + i, new Integer(i) ); } for(int i=10; i>=0; i--){ try{ System.out.println("KEY = STRING " + i + " VALUE = " + table.get("STRING " + i ) ); } catch( NoSuchKeyException e ){ System.out.println(" Key Not Found: " + e.getMessage() ); } } } } // end of class HashTable >javac -deprecation HashTable.java HashTable.java:61: Note: The method void getBytes(int, int, byte[], int) in class java.lang.String has been deprecated. key.getBytes( 0, key.length(), keyBytes, 0 ); ^ Note: HashTable.java uses or overrides a deprecated API. Please consult the documentation for a better alternative. 1 warning >java HashTable Key Not Found: STRING 10 Key Not Found: STRING 9 Key Not Found: STRING 8 Key Not Found: STRING 7 Key Not Found: STRING 6 Key Not Found: STRING 5 KEY = STRING 4 VALUE = 4 KEY = STRING 3 VALUE = 3 KEY = STRING 2 VALUE = 2 KEY = STRING 1 VALUE = 1 KEY = STRING 0 VALUE = 0 > OO Programming
8. Exercises • Browse the java.util package • Compare the methods for the various container classes • Consider which you would use to implement a Graph consisting of nodes and edges • How would you implement a Directed Graph where edges have a direction? OO Programming
Java comes with a rich library of utilities and container classes The containers can hold any Object The API is growing Track changes on the Java/Sun web sites Summary OO Programming