chapter 6 the collections api. simple container/ iterator simple container shape [] v = new...
TRANSCRIPT
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
1. Implement a class MyContainer according to the UML specification given in the right.
2. Create following objects.1. Circle c1 = new Circle(10.0);
2. Circle c2 = new Circle(5.0);
3. Rectangle r1 = new Rectangle(5,9);
4. Rectangle r2 = new Rectangle(3,10);
3. How do you add these objects into your container?
MyContainer
- shapes : Shape []
+add(Shape): void
+remove(Shape); void
+isEmpty() : boolean
+clear() : void
+size() : int
Collection Interface
Major methods of Collection interface
public interface Collection {1. int size();2. boolean isEmpty();3. boolean contains(Object o);4. Object[] toArray();5. Object[] toArray(Object a[]);6. boolean add(Object o);7. boolean remove(Object o);8. void clear();9. 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 interfacepublic interface List extends Collection {
1. Iterator iterator();2. Object get(int index);3. Object set(int index, Object element);4. void add(int index, Object element);5. Object remove(int index);6. int indexOf(Object o);7. int lastIndexOf(Object o);8. List subList(int fromIndex, int toIndex);9. ……………}
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
LinkedListStackArrayListHashSetLinkedHashSetTreeSetVector
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) {1. LinkedList ll = new LinkedList();2. ll.add("Hello 1");3. ll.add("World 2"); 4. show(ll);5. ll.addFirst("Start");6. ll.addLast("Last");7. ll.add(0,”0-th element”);8. show(ll); }
Implementing LinkedList.
1. class Node {2. Object data;3. Node next;4. public String toString() {
5. return data.toString();
6. }7. }
Implementing LinkedList.
1. class MyLinkedList {2. Node head, // head of the linked list 3. tail, // tail of the linked list4. current; // current node of the linked list5. public MyLinkedList() {6. // create sentinel nodes, head and tail7. head = new Node();8. tail = new Node();9. head.next = tail;10. tail.next = tail;11. current = head;12. }
Implementing LinkedList
1. public void add(Node n1) {2. n1.next = current.next;3. current.next = n1 ;4. current = n1 ;5. }6. public void addFirst(Node n1) {7. n1.next = head.next;8. head.next = n1;9. }10. public Node getFirst() {11. return head;12. }13. // How would you implement the following method?14. public Node getLast() {…………..}
Linked List
How to implement the
iterator(), or listIterator()
Method?
Implementing LinkedList
1. public MyIterator iterator()
2. {
3. return new MyIterator(head);
4. }
Implementing LinkedList
1. class MyIterator {2. Node current ;3. MyIterator(Node head)4. { this.current = head ; }5. boolean hasNext() {6. boolean flag = false ;7. Node next = current.next ;8. if ( !next.next.equals(next) ) flag = true;9. return flag;10. }11. Node next() {12. current = current.next ;13. return current ;14. }15.}
Stack
Implements Collection and ListMajor methods of Stack
push()
pop()
peek()
empty()
Using Stack visitation 1
1. Stack mystack = new Stack(); 2. 3. mystack.push(new Integer(1));4. mystack.push(new Double(2.0));5. mystack.push(new String("Hello")); 6. mystack.push(new String(“Ivey”));
7. System.out.println(mystack.pop());8. System.out.println(mystack.pop());9. System.out.println(mystack.pop());
Using Stack visitation 2
1. Stack mystack = new Stack(); 2. 3. mystack.push(new Integer(1));4. mystack.push(new Double(2.0));5. mystack.push(new String("Hello"));6. mystack.push(new String(“Ivey”));7. 8. Iterator itr = mystack.iterator();9. while(itr.hasNext())10. System.out.println(itr.next());
Using Stack visitation 3
1. Stack mystack = new Stack(); 2. 3. mystack.push(new Integer(1));4. mystack.push(new Double(2.0));5. mystack.push(new String("Hello"));6. mystack.push(new String(“Ivey”));7. 8. ListIterator litr = mystack.listIterator();9. System.out.println(litr.next());10. System.out.println(litr.previous());
Implementation of Stack 1 Array Version
1. class MyStack{2. int currentIndex ;3. int maxSize = 100;
4. Object [] objectlist = new Object[maxSize];5. MyStack()6. { currentIndex = 0 ; }
7. void push(Object obj)8. {9. objectlist[currentIndex] = obj ;10. currentIndex++;11. }12. // implements all the methods of Collection interface13. …………..14. …………..15. }
Implementation of Stack 2 Linked List Version
1. class MyStack{2. int currentIndex ;3. int maxSize = 100;
4. LinkedList objectlist ;5. MyStack()
6. {objectlist = new LinkedList() }
7. void push(Object obj)8. {9. objectlist.addLast(obj) ;10. }11. // implements all the methods of Collection interface12. …………..13. …………..14. }
Implementation of Stack 2
How to implement the
iterator(), or listIterator()
Method?