270 likes | 343 Views
Chapter 6. The Collections API. Simple Container/ Iterator. Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++) System.out.println(v[i]);. Custom Container.
E N D
Chapter 6 The Collections API
Simple Container/ Iterator • Simple Container Shape [] v = new Shape[10]; • Simple Iterator For( int i=0 ; i< v.length ; i++) System.out.println(v[i]);
Custom Container • Implement a class MyContainer according to the UML specification given in the right. • Create following objects. • Circle c1 = new Circle(10.0); • Circle c2 = new Circle(5.0); • Rectangle r1 = new Rectangle(5,9); • Rectangle r2 = new Rectangle(3,10); • How do you add these objects into your container?
Collection Interface • Major methods of Collection interface public interface Collection { • int size(); • boolean isEmpty(); • boolean contains(Object o); • Object[] toArray(); • Object[] toArray(Object a[]); • boolean add(Object o); • boolean remove(Object o); • void clear(); • Iterator iterator(); }
Iterator Interface • Member methods of Iterator Interface public interface Iterator { boolean hasNext(); Object next(); void remove(); }
Using Iterator Interface • Array Iteration Object [] v = new Object[10]; while( i< v.length ) System.out.println(v[i++]); • Container Iteration Collection c ; Iterator itr =c.iterator(); while( itr.hasNext() ) System.out.println(itr.next());
Extending CollectionList Interface • List has precise control over where in the list each element is inserted. • Major methods of List interface public interface List extends Collection { • Iterator iterator(); • Object get(int index); • Object set(int index, Object element); • void add(int index, Object element); • Object remove(int index); • int indexOf(Object o); • int lastIndexOf(Object o); • List subList(int fromIndex, int toIndex); • …………… }
ListIterator • Member methods of ListIterator interface public interface ListIterator extends Iterator { boolean hasNext(); Object next(); boolean hasPrevious(); Object previous(); int nextIndex(); int previousIndex(); void remove(); void set(Object o); void add(Object o); }
Difference between Iterator & ListIterator • ListIterator allows bidirectional movement. • Case 1: public static void show(Collection col) { Iterator itr = col.iterator(); while( itr.hasNext() ) System.out.println(itr.next()); }
Difference between Iterator & ListIterator • ListIterator allows bidirectional movement. • Case 2: public static void showBackward(List ll) { ListIterator itr = ll.listIterator(); while( itr.hasNext()) // go to the last itr.next(); while( itr.hasPrevious() ) // print backward System.out.println(itr.previous()); }
Classes Implementing Collection • LinkedList • Stack • ArrayList • HashSet • LinkedHashSet • TreeSet • Vector
Linked List • Implements Collection and List • Major methods of Stack add(Object); add(int index, Object); addFirst(Object); addLast(Object); Object getFirst(); Object getLast(); Object get(int index); ListIterator listIterator();
Linked List • Provides methods to get, remove and insert an element at the beginning and end of the list. • Can be used as a stack, queue, or double-ended queue (deque). • Java LinkedList class is in fact a doubly linked list.
Using Linked List? public static void main(String [] args) { • LinkedList ll = new LinkedList(); • ll.add("Hello 1"); • ll.add("World 2"); • show(ll); • ll.addFirst("Start"); • ll.addLast("Last"); • ll.add(0,”0-th element”); • show(ll); }
Implementing LinkedList. • class Node { • Object data; • Node next; • public String toString() { • return data.toString(); • } • }
Implementing LinkedList. • class MyLinkedList { • Node head, // head of the linked list • tail, // tail of the linked list • current; // current node of the linked list • public MyLinkedList() { • // create sentinel nodes, head and tail • head = new Node(); • tail = new Node(); • head.next = tail; • tail.next = tail; • current = head; • }
Implementing LinkedList • public void add(Node n1) { • n1.next = current.next; • current.next = n1 ; • current = n1 ; • } • public void addFirst(Node n1) { • n1.next = head.next; • head.next = n1; • } • public Node getFirst() { • return head; • } • // How would you implement the following method? • public Node getLast() {…………..}
Linked List How to implement the iterator(), or listIterator() Method?
Implementing LinkedList • public MyIterator iterator() • { • return new MyIterator(head); • }
Implementing LinkedList • class MyIterator { • Node current ; • MyIterator(Node head) • { this.current = head ; } • boolean hasNext() { • boolean flag = false ; • Node next = current.next ; • if ( !next.next.equals(next) ) flag = true; • return flag; • } • Node next() { • current = current.next ; • return current ; • } • }
Stack • Implements Collection and List • Major methods of Stack push() pop() peek() empty()
Using Stack visitation 1 • Stack mystack = new Stack(); • mystack.push(new Integer(1)); • mystack.push(new Double(2.0)); • mystack.push(new String("Hello")); • mystack.push(new String(“Ivey”)); • System.out.println(mystack.pop()); • System.out.println(mystack.pop()); • System.out.println(mystack.pop());
Using Stack visitation 2 • Stack mystack = new Stack(); • mystack.push(new Integer(1)); • mystack.push(new Double(2.0)); • mystack.push(new String("Hello")); • mystack.push(new String(“Ivey”)); • Iterator itr = mystack.iterator(); • while(itr.hasNext()) • System.out.println(itr.next());
Using Stack visitation 3 • Stack mystack = new Stack(); • mystack.push(new Integer(1)); • mystack.push(new Double(2.0)); • mystack.push(new String("Hello")); • mystack.push(new String(“Ivey”)); • ListIterator litr = mystack.listIterator(); • System.out.println(litr.next()); • System.out.println(litr.previous());
Implementation of Stack 1 • Array Version • class MyStack{ • int currentIndex ; • int maxSize = 100; • Object [] objectlist = new Object[maxSize]; • MyStack() • { currentIndex = 0 ; } • void push(Object obj) • { • objectlist[currentIndex] = obj ; • currentIndex++; • } • // implements all the methods of Collection interface • ………….. • ………….. • }
Implementation of Stack 2 • Linked List Version • class MyStack{ • int currentIndex ; • int maxSize = 100; • LinkedList objectlist ; • MyStack() • {objectlist = new LinkedList() } • void push(Object obj) • { • objectlist.addLast(obj) ; • } • // implements all the methods of Collection interface • ………….. • ………….. • }
Implementation of Stack 2 How to implement the iterator(), or listIterator() Method?