190 likes | 540 Views
Containers. Contents. Description of Container objects (Partial) Taxonomy of the Collection classes in java.util ArrayList LinkedList Stack Queue Binary Search Tree HashMap. Containers. Think of Containers as objects where you hold other objects. .
E N D
Containers Contents • Description of Container objects • (Partial) Taxonomy of the Collection classes in java.util • ArrayList • LinkedList • Stack • Queue • Binary Search Tree • HashMap
Containers Think of Containers as objects where you hold other objects. • You want your containers to be generic – hold any thing derived from class Object • Containers will require methods that enable you to “put things in”, “take things out”, “find and/or retrieve objects in the container”, and “describe the state of the container” – empty, full, number of contents, etc.. • A container will use an object called an Iterator to traverse its contents.
Containers All containers, whether they are part of the standard library (in java.util) or whether they are container classes created by an application programmer, are built up around either an array or a collection of node objects that can be linked together.
Containers Containers in standard usage include • ArrayList (found in java.util) • LinkedList (found in java.util) • Stack • Queue • Tree • Binary Search Tree (implemented as TreeSet in java.util) • Hash Table (implemented as HashMap in java.util)
Containers Some of these containers are part of the standard java library found in java.util. They are derived from the (abstract) Collection interface provided in this package. We will use these library classes in various exercises during the course of the semester, and we will also construct similar classes for ourselves. We will also add container classes such as Stack and Queue to our personal library.
interface Iterator Collection Concrete class Color code List Set ListIterator HashSet TreeSet ArrayList LinkedList Containers produces produces A simplified taxonomy of the Container classes in java.util
Containers ArrayList An ArrayList augments an array in the following ways: • It encapsulates the array with an attribute size that is used to determine the number of items currently stored in the array and to indicate the next available location to add an item. • It encapsulates data (a handle to the array and its size) with the operations that can be performed upon ArrayList objects. – The client programmer can add or remove objects from any valid location in the list without having to personally shift items to maintain the continuity of the underlying array. • It automatically expands when an insertion is attempted in a full ArrayList. But! It holds only objects. Primitive types must be wrapped in their Wrapper class before being placed in an ArrayList, and objects removed or retrieved from an ArrayList must be down cast to retain the full functionality of members of their class.
int size A 0 1 2 3 4 5 6 Object [ ] buffer Containers ArrayList Note! Both the ArrayList object and the array are dynamically allocated (using the new operation) 0 The client program will declare and construct an ArrayList object ArrayList A; A = new ArrayList(7);
data next p 7 Containers LinkedList A linked list is a structure that is formed by chaining together a set of objects called nodes or links. A Node object has (at least) two fields: • A field holding a (handle to a) data element (of type Object) • A field for holding a handle to another Node object class Node { public Object data; public Node next; } Node p = new Node( new Integer(7));
int size 3 Node first 0 Containers LinkedList These Node objects are linked together to form a structure for holding a sequence of objects. L handle to LinkedList object Node objects LinkedList object
Containers LinkedList Both ArrayList and LinkedList implement the same List interface. In an ArrayList of size N, up to N elements have to be shifted one position to the left or right in order to insert or remove an object in general position. (Average # shifts = N/2) In an ArrayList, locating a position by its index is done in constant time independent of the size of the list. In a LinkedList, inserts and removes are performed by splicing the affected links. The cost (in time or # of operations) to perform an insert or remove in a LinkedList is independent of the size of the list. However, in a LinkedList locating a Node at a given position can only be done by traversing through the links to the indicated index. In a list of size N the average number of Nodes visited = N/2.
x 3 s Node front 0 int size 1 2 3 Node top Containers Stack A Stack is a last-in-first-out (LIFO) List. It is a list with this additional constraint and can be implemented using either an array or a linked list. A stack has operations: void push(Object), Object pop( ), and Object top( ) Stack s = new Stack( ); s.push(new Integer(1)); Object x = s.pop( ); s.push(new Integer(2)); s.pusn(new Integer(3)); 0 0 0 3 1 2 2 0
x Containers Queue A Queue is a first-in-first-out (FIFO) list. Queue has methods void enqueue(Object), Object dequeue( ), Object front() Queue q = new Queue( ); Frog x = (Frog)q.dequeue( ); q.enqueue(new Frog( )); q.enqueue(new Frog( )); q.enqueue(new Frog()); q.enqueue(new Frog( ));
Containers Stacks and Queues The previous slide shows that a queue grows at the back and loses objects at the front. If a standard array structure is used to implement a queue, every object in the queue will have to be shifted forward after each dequeue( ) operation. Stacks and Queues are constrained lists. They should not provide all of the method calls in the List interface. The programmer constructing a Stack or Queue should NOT inherit from either LinkedList or ArrayList, but either of these containers can be used inside of a Stack or a Queue (composition)
Attribute is an ArrayList object Implement method by calling an ArrayList method Containers Stacks and Queues An example of composition publicclass Stack { private ArrayList myList; piblic Stack( ) { myList = new ArrayList(10); } publicvoid push(Object x) { myList.add(x); } public Object pop( ) { return myList.remove(myList.size( ) - 1); } //other stack operations implemented similarly }
TreeNode root tree Containers Binary Search Tree A binary tree is a container consisting of TreeNode objects that have three attributes – one holding data, the other 2 handles to TreeNodes. class TreeNode { public Object data; public TreeNode left; public treeNode right; //other methods } 0 0 0 BinaryTree tree = new BinaryTree( ); 0 0 TreeNode objects
Containers Binary Search Tree A binary search tree is a binary tree that consists of a root and a left and right sub-tree (either or both are possibly null) with the properties that: • All of the keys in the left subtree are less than the key of the root. • All of the keys in the right subtree are greater than the key of the root • The left and right subtree are themselves binary search trees. All newly inserted TreeNodes in a binary search tree will be located at a leaf. If a binary tree of size N treeNodes is reasonably balanced, the length of the path from the root to any leaf will be proportional to log(N).
Containers HashMap A HashMap is structure for rapidly storing and retrieving objects, but it cannot provide an ordered listing of its contents like the other containers are able to do. A Hash code is a function that maps a key field of the data object into an integer value in the range of indices of the table. A good hash function will distribute the keys uniformly throughout the table indices. The HashMap holds its objects in one of the chains or linked lists attached to each table entry. If the number of objects held is less than or equal to the size of the table, the length of each chain should be close to 1. Note! The HashMap is derived from an interface Map and is not the same thing as HashSet depicted in the taxonomy of java.util
Let be an object that we want to insert into the HashMap hm. 0 1 2 3 4 5 6 7 8 9 10 hm 0 0 0 0 Containers HashMap Step 1. Find the hashCode( ) of the object % 11. Step 2. Put the object inside of a Node Step 3. Add the node to the chain at index 2 of the table. 2