Presentation is loading. Please wait.

Presentation is loading. Please wait.

1.0tCopyright © 1998 Purple Technology, Inc. 1 Java Collections Framework Authored by Alex Chaffee Copyright © 1998 Purple Technology, Inc. All rights.

Similar presentations


Presentation on theme: "1.0tCopyright © 1998 Purple Technology, Inc. 1 Java Collections Framework Authored by Alex Chaffee Copyright © 1998 Purple Technology, Inc. All rights."— Presentation transcript:

1 1.0tCopyright © 1998 Purple Technology, Inc. 1 Java Collections Framework Authored by Alex Chaffee Copyright © 1998 Purple Technology, Inc. All rights reserved. Licensed to Advanced Web Technologies Modified by Habib Rostami.Lecture 9

2 Java Collections Framework 1.0tCopyright © 1998 Purple Technology, Inc.2 Interface-based design Separate interface from implementationSeparate interface from implementation Built in to Java languageBuilt in to Java language PolymorphismPolymorphism –List l = new LinkedList(); –Calling l.add() invokes method of class LinkedList

3 Java Collections Framework 1.0tCopyright © 1998 Purple Technology, Inc.3 Collections Framework Interoperability between unrelated APIsInteroperability between unrelated APIs Reduces the effort required to learn APIsReduces the effort required to learn APIs Reduces the effort required to design and implement APIsReduces the effort required to design and implement APIs Fosters software reuseFosters software reuse

4 Java Collections Framework 1.0tCopyright © 1998 Purple Technology, Inc.4 Project Goals Small APISmall API –Number of interfaces –Number of methods per interface –Low "conceptual weight" Builds on existing Java collections (Vector, Hashtable)Builds on existing Java collections (Vector, Hashtable) Interconvertible with Java arraysInterconvertible with Java arrays

5 Java Collections Framework 1.0tCopyright © 1998 Purple Technology, Inc.5 Interface-Based Design –interface List {…} –class LinkedList implements List {…} –… –List l = new LinkedList(); –l.add( new Date() ); –Date d = (Date)l.get(0);

6 Java Collections Framework 1.0tCopyright © 1998 Purple Technology, Inc.6 Overview: Core Interfaces CollectionCollection SetSet ListList MapMap SortedSetSortedSet SortedMapSortedMap

7 Java Collections Framework 1.0tCopyright © 1998 Purple Technology, Inc.7 Overview: Utilities Utility InterfacesUtility Interfaces –Comparator –Iterator Utility ClassesUtility Classes –Collections –Arrays

8 Java Collections Framework 1.0tCopyright © 1998 Purple Technology, Inc.8 Collection A group of objectsA group of objects Major methods:Major methods: –int size(); –boolean isEmpty(); –boolean contains(Object); –Iterator iterator(); –Object[] toArray(); –boolean add(Object); –boolean remove(Object); –void clear();

9 Java Collections Framework 1.0tCopyright © 1998 Purple Technology, Inc.9 Set interface Set extends Collectioninterface Set extends Collection An unordered collection of objectsAn unordered collection of objects No duplicate elementsNo duplicate elements Same methods as CollectionSame methods as Collection –Semantics are different, so different interface needed for design Implemented by:Implemented by: –HashSet, TreeSet

10 Java Collections Framework 1.0tCopyright © 1998 Purple Technology, Inc.10 List interface List extends Collectioninterface List extends Collection An ordered collection of objectsAn ordered collection of objects Duplicates allowedDuplicates allowed

11 Java Collections Framework 1.0tCopyright © 1998 Purple Technology, Inc.11 List Details Major additional methods:Major additional methods: –Object get(int); –Object set(int, Object); –int indexOf(Object); –int lastIndexOf(Object); –void add(int, Object); –Object remove(int); –List subList(int, int); add() inserts remove() deletes Implemented by:Implemented by: –ArrayList, LinkedList, Vector

12 Java Collections Framework 1.0tCopyright © 1998 Purple Technology, Inc.12 Map interface Map (does not extend Collection)interface Map (does not extend Collection) An object that maps keys to valuesAn object that maps keys to values Each key can have at most one valueEach key can have at most one value Replaces java.util.Dictionary interfaceReplaces java.util.Dictionary interface Ordering may be provided by implementation class, but not guaranteedOrdering may be provided by implementation class, but not guaranteed

13 Java Collections Framework 1.0tCopyright © 1998 Purple Technology, Inc.13 Map Details Major methods:Major methods: –int size(); –boolean isEmpty(); –boolean containsKey(Object); –boolean containsValue(Object); –Object get(Object); –Object put(Object, Object); –Object remove(Object); –void putAll(Map); –void clear(); Implemented by:Implemented by: –HashMap, Hashtable, ….

14 Java Collections Framework 1.0tCopyright © 1998 Purple Technology, Inc.14 Accessing all members of Map MethodsMethods –Set keySet(); –Collection values(); –Set entrySet(); Map.EntryMap.Entry –Object that contains a key-value pair getKey(), getValue()

15 Java Collections Framework 1.0tCopyright © 1998 Purple Technology, Inc.15 Iterator Represents a loopRepresents a loop Created by Collection.iterator()Created by Collection.iterator() Similar to EnumerationSimilar to Enumeration –Improved method names –Allows a remove() operation on the current item

16 Java Collections Framework 1.0tCopyright © 1998 Purple Technology, Inc.16 Iterator Methods boolean hasNext()boolean hasNext() –Returns true if the iteration has more elements Object next()Object next() –Returns next element in the iteration void remove()void remove() –Removes the current element from the underlying Collection

17 Java Collections Framework 1.0tCopyright © 1998 Purple Technology, Inc.17 ListIterator interface ListIterator extends Iteratorinterface ListIterator extends Iterator Created by List.listIterator()Created by List.listIterator() Adds methods toAdds methods to –traverse the List in either direction –modify the List during iteration Methods added:Methods added: –hasPrevious(), previous() –nextIndex(), previousIndex() –set(Object), add(Object)

18 Java Collections Framework 1.0tCopyright © 1998 Purple Technology, Inc.18 Set Implementations HashSetHashSet –a Set backed by a hash table TreeSetTreeSet –A balanced binary tree implementation –Imposes an ordering on its elements

19 Java Collections Framework 1.0tCopyright © 1998 Purple Technology, Inc.19 List Implementations ArrayListArrayList –a resizable-array implementation like Vector LinkedListLinkedList –a doubly-linked list implementation –May provide better performance than ArrayList if elements frequently inserted/deleted within the List –For queues and double-ended queues (deques) VectorVector –a resizable-array implementation of a List

20 Java Collections Framework 1.0tCopyright © 1998 Purple Technology, Inc.20 Map Implementations HashMapHashMap –A hash table implementation of Map –Like Hashtable, but supports null keys & values TreeMapTreeMap –A balanced binary tree implementation –Imposes an ordering on its elements HashtableHashtable –Synchronized hash table implementation of Map interface, with additional "legacy" methods.

21 Java Collections Framework 1.0tCopyright © 1998 Purple Technology, Inc.21 Sorting Collections.sort() static methodCollections.sort() static method SortedSet, SortedMap interfacesSortedSet, SortedMap interfaces –Collections that keep their elements sorted –Iterators are guaranteed to traverse in sorted order Ordered Collection ImplementationsOrdered Collection Implementations –TreeSet, TreeMap

22 Java Collections Framework 1.0tCopyright © 1998 Purple Technology, Inc.22 Sorting (cont.) Comparable interfaceComparable interface –Must be implemented by all elements in SortedSet –Must be implemented by all keys in SortedMap –Method: int compareTo(Object o) –Defines "natural order" for that object class Comparator interfaceComparator interface –Defines a function that compares two objects –Can design custom ordering scheme –Method: int compare(Object o1, Object o2)

23 Java Collections Framework 1.0tCopyright © 1998 Purple Technology, Inc.23 Sorting (cont.) Total vs. Partial OrderingTotal vs. Partial Ordering –Technical, changes behavior per object class Sorting ArraysSorting Arrays –Use Arrays.sort(Object[]) –Equivalent methods for all primitive types Arrays.sort(int[]), etc.

24 Java Collections Framework 1.0tCopyright © 1998 Purple Technology, Inc.24 Unsupported Operations An implementation class may elect not to support a particular method of the interfaceAn implementation class may elect not to support a particular method of the interface UnsupportedOperationException is a runtime (unchecked) exceptionUnsupportedOperationException is a runtime (unchecked) exception

25 Java Collections Framework 1.0tCopyright © 1998 Purple Technology, Inc.25 Utility Classes Collections classCollections class Static methods:Static methods: –sort(List) –binarySearch(List, Object) –reverse(List) –shuffle(List) –fill(List, Object) –copy(List dest, List src) –min(Collection) –max(Collection) –synchronizedX, unmodifiableX factory methods

26 Java Collections Framework 1.0tCopyright © 1998 Purple Technology, Inc.26 Utility Classes Arrays classArrays class Static methods that act on Java arrays:Static methods that act on Java arrays: –sort –binarySearch –equals –fill –asList - returns an ArrayList composed of this array's contents


Download ppt "1.0tCopyright © 1998 Purple Technology, Inc. 1 Java Collections Framework Authored by Alex Chaffee Copyright © 1998 Purple Technology, Inc. All rights."

Similar presentations


Ads by Google