Java Collections Framework: Interfaces

15
Unit 29 1 Java Collections Framework: Interfaces Introduction to the Java Collections Framework (JCF) The Comparator Interface Revisited The Collection Interface The List Interface The Iterator Interface The ListIterator Interface

description

Java Collections Framework: Interfaces. Introduction to the Java Collections Framework (JCF) The Comparator Interface Revisited The Collection Interface The List Interface The Iterator Interface The ListIterator Interface. The Java Collections Framework. - PowerPoint PPT Presentation

Transcript of Java Collections Framework: Interfaces

Page 1: Java Collections Framework: Interfaces

Unit 29 1

Java Collections Framework: Interfaces

• Introduction to the Java Collections Framework (JCF)

• The Comparator Interface Revisited

• The Collection Interface

• The List Interface

• The Iterator Interface

• The ListIterator Interface

Page 2: Java Collections Framework: Interfaces

Unit 29 2

The Java Collections Framework

• A collection is a group of objects.• The Java Collections Framework is a set of important utility classes

and interfaces in the java.util package for working with collections.• The Collections Framework consists of three parts:

– Interfaces: the abstract data types that the framework supports.• E.g., java.util.Comparator, java.util.Collection, java.util.Iterator

– Implementations: concrete versions of these interfaces.• E.g., java.util.ArrayList and java.util.LinkedList.

– Algorithms: predefined actions defined on the interfaces or their implementations. E..g., Collections.sort(), Collections.binarySearch()

Page 3: Java Collections Framework: Interfaces

Unit 29 3

Why Develop the JCF?

• An array is a common data structure suitable in many situations• However, arrays are not always good for some of the following:

– It requires size information for creation– Inserting an element may lead to moving other elements around– Deleting an element may lead to moving other elements around

• Other data structures, like linked list and trees, are better for some situations

• The JCF provides efficient implementations for these common data structures

• Using the JCF interfaces, these data structures can be manipulated in a uniform way

Page 4: Java Collections Framework: Interfaces

Unit 29 4

Some Benefits of the JCF

• Benefits– It reduces programming effort: by providing useful data structures. Programmer

can focus on problem at hand– Improve program speed and quality: by providing high-performance, high-quality

implementations for the most common data structures– Allows interoperability among unrelated APIs: If a network API provides a

Collection of node names, and a GUI toolkit expects a Collection of column headings, they will interoperate seamlessly even though they were written independently

– It fosters software reuse

• Concerns– JCF data structures work only with objects, not primitive types– A Collection can contain incompatible types at the same time– JFC methods are generic; must always downcast from Object to our types

Page 5: Java Collections Framework: Interfaces

Unit 29 5

Collections Framework’s Interfaces

Object <<interface>> Comparable

Collections AbstractCollection

AbstractList

AbstractSequentialList

LinkedList

ArrayList

<<interface>> Collection

<<interface>> Comparator

<<interface>> Iterator

<<interface>> List

<<interface>> ListIterator

java.lang

Page 6: Java Collections Framework: Interfaces

Unit 29 6

The Comparator Interface Revisited

• Recall that the Comparator interface is used when

– The objects to be compared do not support natural ordering or

– When we desire a different ordering than the natural

• The interface:

public interface Comparator { public abstract int compare(Object object1, Object object2); public abstract boolean equals(Object obj);}

Page 7: Java Collections Framework: Interfaces

Unit 29 7

Example 1: The Comparator Interface 1 import java.util.*; 2 class MyComparator implements Comparator { 3 public int compare(Object obj1, Object obj2) { 4 int i1 = ((Integer)obj1).intValue(); 5 int i2 = ((Integer)obj2).intValue(); 6 return Math.abs(i2) - Math.abs(i1); 7 } 8 } 9 public class TestCollections {10 public static void main(String args[]) {11 ArrayList array = new ArrayList();12 array.add(new Integer(-200));13 array.add(new Integer(100));14 array.add(new Integer(400));15 array.add(new Integer(-300));16 Collections.sort(array);17 System.out.println("Natural ordering: " + array);18 Collections.sort(array, new MyComparator());19 System.out.println("My own ordering : " + array);20 }21 }

Page 8: Java Collections Framework: Interfaces

Unit 29 8

The Collection Interface

public interface Collection{ public abstract boolean add(Object object); public abstract boolean addAll(Collection collection); public abstract void clear(); public abstract boolean remove(Object object); public abstract boolean removeAll(Collection collection); public abstract boolean retainAll(Collection collection); public abstract boolean isEmpty(); public abstract boolean contains(Object object); public abstract boolean containsAll(Collection collection); public abstract equals(Object object); public abstract int size(); public abstract Iterator iterator(); public abstract int hashCode(); public abstract Object[] toArray(); public abstract Object[] toArray(Object[] array); }

Page 9: Java Collections Framework: Interfaces

Unit 29 9

The List Interface

public abstract void add(int index, Object object); public abstract Object get(int index); public abstract int indexOf(Object object); public abstract int lastIndexOf(Object object); public abstract Object remove(int index); public abstract Object set(int index, Object object); public abstract ListIterator listIterator(); public abstract ListIterator listIterator(int index); public abstract List subList(int fromIndex, int toIndex);

• The List interface represents an ordered collection of objects.

• Each element in a list has an index, or position. The indexes range from 0 to size() – 1.

• List extends Collection. It has the following additional methods to those it inherits and

overrides:

Page 10: Java Collections Framework: Interfaces

Unit 29 10

The Iterator Interface

public interface Iterator{ public abstract boolean hasNext(); public abstract Object next(); public abstract void remove();}

• To visit all the elements of a collection object c, code such as such as the

following may be used:

Iterator iter = c.iterator();while(iter.hasNext( )){ Object obj = iter.next(); process(obj);}

• Note: When next() is invoked, the iterator jumps over the next element, and it

returns a reference to the object that it just passed.

Page 11: Java Collections Framework: Interfaces

Unit 29 11

The Iterator Interface (cont’d)

Iterator iter = c.iterator( );iter.next(); // skip over the first elementiter.remove(); // remove the first element

• The remove() method of an iterator removes the element whose reference was returned by the last call to

next(). For example, the following code removes the first element in a collection c:

Iterator iter = c.iterator( ); iter.remove();

• It is illegal to call the remove() method of an iterator if it was not preceded by a

call to next(). For example, the following is invalid:

Page 12: Java Collections Framework: Interfaces

Unit 29 12

Example 2: The Iterator Interface 1 import java.util.*; 2 public class TestIterator { 3 public static void main(String[] args) { 4 //LinkedList list = new LinkedList(); 5 ArrayList list = new ArrayList(); 6 7 for(int i = 0; i < 6; i++) 8 list.add(new Integer(i)); 910 Iterator iter = list.iterator();1112 while (iter.hasNext()){13 Integer myInt = (Integer)iter.next();14 System.out.print(myInt+" ");15 }16 System.out.println();17 }18 }

Page 13: Java Collections Framework: Interfaces

Unit 29 13

The ListIterator Interface

public abstract boolean hasPrevious(); public abstract int nextIndex(); public abstract Object previous(); public abstract int previousIndex(); public abstract add(Object object); public abstract void set(Object object);

• The ListIterator interface extends Iterator to allow bi-directional traversal of a list, and the modification of a list.

• It has the following additional methods to those it inherits and overrides:

Page 14: Java Collections Framework: Interfaces

Unit 29 14

Example 3: The ListIterator Interface 1 import java.util.*; 2 public class TestListIterator { 3 public static void main(String[] args) { 4 //LinkedList list = new LinkedList(); 5 ArrayList list = new ArrayList(); 6 7 ListIterator iter2, iter1 = list.listIterator(); 8 9 for(int i = 0; i < 6; i++)10 iter1.add(new Integer(i));1112 iter2 = list.listIterator();13 iter2.next(); // skip the first element14 iter2.add(new Integer(25)); // add immediately after the first1516 System.out.println(list);17 }18 }

Page 15: Java Collections Framework: Interfaces

Unit 29 15

Example 4: The ListIterator Interface 1 import java.util.*; 2 public class TestListIterator2 { 3 public static void main(String[] args) { 4 ArrayList list = new ArrayList(); 5 int bonus = 1; 6 7 for(int i = 0; i < 6; i++) 8 list.add(new Integer(i)); 910 ListIterator iter = list.listIterator();1112 System.out.println("List before: " + list);1314 while (iter.hasNext()){15 Integer myInt = (Integer)iter.next();16 iter.set(new Integer(myInt.intValue()+bonus));17 }18 System.out.println("List after : " + list);19 }20 }