Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Collections Framework COMP53 Oct 24, 2007. Collections Framework A unified architecture for representing and manipulating collections Allows collections.

Similar presentations


Presentation on theme: "Java Collections Framework COMP53 Oct 24, 2007. Collections Framework A unified architecture for representing and manipulating collections Allows collections."— Presentation transcript:

1 Java Collections Framework COMP53 Oct 24, 2007

2 Collections Framework A unified architecture for representing and manipulating collections Allows collections to be manipulated independently of the details of their representation. Reduces programming effort while increasing performance. Reduces effort in designing and learning new APIs, and fosters software reuse. Based on six collection interfaces and algorithms to manipulate them. http://java.sun.com/j2se/1.4.2/docs/guide/collections/index.html

3 Core Collection Interfaces These are the interfaces that define the operations that are expected for various kinds of collections. This is an inheritance hierarchy. Collection SetListQueue Map SortedMap SortedSet

4 Core Collections Collection: the root of the collection hierarchy. A collection represents a group of objects known as its elements. Set: a collection that cannot contain duplicate elements. This interface models the mathematical set abstraction. List: an ordered collection (sometimes called a sequence). Lists can contain duplicate elements. Queue: a collection used to hold multiple elements prior to processing. Map: an object that maps keys to values. A Map cannot contain duplicate keys; each key can map to at most one value. SortedSet and SortedMap: Same functionality as Set and Map, but elements are ordered.

5 Core Collection Interfaces These are the interfaces that define the operations that are expected for various kinds of collections. This is an inheritance hierarchy. Collection SetListQueue Map SortedMap SortedSet For the moment, we’ll focus on the collections we know. We’ll return to the others later.

6 The Collection Interface public interface Collection extends Iterable { // Basic operations int size(); boolean isEmpty(); boolean contains(Object element); boolean containsAll(Collection c); Iterator iterator(); These operations are required. All collections must implement them.

7 The Collection Interface public interface Collection extends Iterable { boolean add(E element); boolean remove(Object element); boolean addAll(Collection c); boolean removeAll(Collection c); boolean retainAll(Collection c); void clear(); These operations are optional – collections may leave them unimplemented. Unimplemented methods must throw UnsupportedOperationException.

8 The Collection Interface public interface Collection extends Iterable { // Array operations Object[] toArray(); T[] toArray(T[] a); These operations are also required. Every collection must be convertible to an array.

9 The List Interface public interface List extends Collection { // Positional access E get(int index); E set(int index, E element); boolean add(E element); void add(int index, E element); E remove(int index); boolean addAll(int index, Collection c); get() is required, others are optional.

10 The List Interface public interface List extends Collection { // Search int indexOf(Object o); int lastIndexOf(Object o); // Iteration ListIterator listIterator(); ListIterator listIterator(int index); // Range-view List subList(int from, int to); These operations are required.

11 The Queue Interface public interface Queue extends Collection { boolean add(E element); // from Collection E remove(); E element(); boolean offer(E e); E poll(); E peek(); } First three throw exceptions on failure. Last three return false or null on failure.

12 Stack and Queue Operations JCFSTACK ADTQUEUE ADT add offer pushenqueue remove poll popdequeue element peek topfront

13 Legacy Collections These collections from older versions of Java are still available: Vector – old-style List HashTable – old-style Map

14 List Implementations ArrayList and LinkedList both implement the List interface – as defined in sections 3.1,3.2 and previous lecture Most of the time, use ArrayList, which offers constant-time positional access and is just plain fast. If you frequently add elements to the beginning of the list or iterate to delete elements from list’s interior, consider using LinkedList.

15


Download ppt "Java Collections Framework COMP53 Oct 24, 2007. Collections Framework A unified architecture for representing and manipulating collections Allows collections."

Similar presentations


Ads by Google