Download presentation
Presentation is loading. Please wait.
1
1 The Collection Interface public interface Collection extends Iterable { boolean add(E e); boolean addAll(Collection c); void clear(); boolean contains(Object o); boolean containsAll(Collection c); boolean equals(Object o); int hashCode(); boolean isEmpty(); Iterator iterator(); boolean remove(Object o); boolean removeAll(Collection c); boolean retainAll(Collection c); int size(); Object[] toArray(); }
2
2 Traversing Collections 4 For-each construct 4 Iterators for (Object o : c){ … } Iterator it = c.iterator(); While (it.hasNext()){ Object o = it.next(); … }
3
3 The List Interface public interface List extends Collection { void add(int index, E element); boolean addAll(int index, Collection c); E get(int index); int indexOf(Object o); int lastIndexOf(Object o); ListIterator listIterator(); ListIterator listIterator(int index); E remove(int index); List subList(int fromIndex, int toIndex); }
4
4 Implementing Classes of List 4 java.util.Vector 4 java.util.LinkedList 4 java.util.Stack
5
5 Example Use of Vector public static Vector removeDuplicates(Vector v){ Vector c = new Vector (); for (E elm : v){ if (!c.contains(elm)){ c.add(elm); } return c; }
6
6 Example Use of LinkedList class LinkedListStack { LinkedList elms; public LinkedListStack() { elms = new LinkedList (); } public void push(E x) { elms.addFirst(x); } public E pop() { if (elms.size()==0){ throw new RuntimeException("Stack underflow"); } else { return elms.removeFirst(); } public void print(){ for (Object elm : elms){ System.out.print(elm+" "); }
7
7 Implementing LinkedList class class Node { E val; Node next; public Node(E x, Node next){ val = x; this.next = next; } public Node(E x){ this(x,null); } public void setNext(Node next){ this.next = next; } public Node getNext(){ return this.next; } public E getVal(){ return val; } public void setVal(E o){ val = o; }
8
8 Implementing LinkedList class class MyLinkedList { Node head; public void addFirst(E x){ head = new Node (x,head); } public void add(E x){ if (head == null){ head = new Node (x); } else { Node pre,curr; pre = curr = head; while (curr != null){ pre = curr; curr = curr.getNext(); } pre.setNext(new Node (x)); }
9
9 Implementing LinkedList class class MyLinkedListIterator implements Iterator { Node curNode; public MyLinkedListIterator(MyLinkedList l){ curNode = l.head; } public E next(){ E val; if (curNode!=null){ val = curNode.getVal(); curNode = curNode.getNext(); return val; } return null; } …
10
10 The Set Interface and Classes 4 The interface 4 The implementing classes –HashSet, LinkedHashSet, TreeSet,… public interface Set extends Collection { … }
11
11 The Map Interface and Classes 4 The interface 4 Implementing classes –Hashtable,HashMap,LinkedHashMap,… public interface Map { void clear(); boolean containsKey(Object key); boolean containsValue(Object value); V get(Object key); boolean isEmpty(); Set keySet(); V put(K key, V value); int size(); Collection values(); … }
12
12 Example Use of Hashtable Hashtable numbers = new Hashtable (); numbers.put("one", 1); numbers.put("two", 2); numbers.put("three", 3); System.out.println("two = " + numbers.get("two"));
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.