1 / 27

Chapter 6

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.

jacqui
Download Presentation

Chapter 6

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 6 The Collections API

  2. 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]);

  3. 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?

  4. 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(); }

  5. Iterator Interface • Member methods of Iterator Interface public interface Iterator { boolean hasNext(); Object next(); void remove(); }

  6. 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());

  7. 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); • …………… }

  8. 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); }

  9. 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()); }

  10. 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()); }

  11. Classes Implementing Collection • LinkedList • Stack • ArrayList • HashSet • LinkedHashSet • TreeSet • Vector

  12. 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();

  13. 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.

  14. 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); }

  15. Implementing LinkedList. • class Node { • Object data; • Node next; • public String toString() { • return data.toString(); • } • }

  16. 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; • }

  17. 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() {…………..}

  18. Linked List How to implement the iterator(), or listIterator() Method?

  19. Implementing LinkedList • public MyIterator iterator() • { • return new MyIterator(head); • }

  20. 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 ; • } • }

  21. Stack • Implements Collection and List • Major methods of Stack push() pop() peek() empty()

  22. 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());

  23. 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());

  24. 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());

  25. 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 • ………….. • ………….. • }

  26. 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 • ………….. • ………….. • }

  27. Implementation of Stack 2 How to implement the iterator(), or listIterator() Method?

More Related