UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

25
UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 17 Lecture 17 Advanced Java Concepts Advanced Java Concepts Data Structure Support Data Structure Support [ [ Java 2: The Complete Reference: Java 2: The Complete Reference: Chapter 15] Chapter 15] [ [ Deitel Deitel : Chapters 22, 23, 24] : Chapters 22, 23, 24] Mon. 10/23/00 Mon. 10/23/00

description

UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000. Lecture 17 Advanced Java Concepts Data Structure Support [ Java 2: The Complete Reference: Chapter 15] [ Deitel : Chapters 22, 23, 24] Mon. 10/23/00. Homework Status. HW# Assigned Due. - PowerPoint PPT Presentation

Transcript of UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

Page 1: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

UMass Lowell Computer Science 91.460

Java and Distributed Computing

Prof. Karen Daniels Fall, 2000

UMass Lowell Computer Science 91.460

Java and Distributed Computing

Prof. Karen Daniels Fall, 2000

Lecture 17Lecture 17Advanced Java ConceptsAdvanced Java Concepts

Data Structure SupportData Structure Support[[Java 2: The Complete Reference: Java 2: The Complete Reference: Chapter 15]Chapter 15]

[[DeitelDeitel: Chapters 22, 23, 24]: Chapters 22, 23, 24]

Mon. 10/23/00Mon. 10/23/00

Page 2: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

Homework StatusHomework Status

11 Fri, 9/8 9/15, 9/18 Fri, 9/8 9/15, 9/18

22 Fri, 9/15 Fri, 9/22 Fri, 9/15 Fri, 9/22

33 Fri, 9/22Fri, 9/22 Fri, 9/29 Fri, 9/29

44 Fri, 10/6Fri, 10/6 Fri, 10/13Fri, 10/13

55 Fri, 10/13Fri, 10/13 Fri, 10/20Fri, 10/20

66 Fri, 10/20 Fri, 10/27Fri, 10/20 Fri, 10/27

HW#HW# AssignedAssigned DueDue

GradedGraded

SubmittedSubmitted

PendingPending

Page 3: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

OverviewOverview

Collections FrameworkCollections Framework Iterator InterfaceIterator Interface Comparator InterfaceComparator Interface Map InterfaceMap Interface Collection AlgorithmsCollection Algorithms

Page 4: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

Java Collections FrameworkJava Collections Framework

Similar to C++ Standard Template Library (STL)Similar to C++ Standard Template Library (STL) C++ container -> Java collectionC++ container -> Java collection

Unifies previous Java data structure classesUnifies previous Java data structure classes e.g.: Vector, Stack, Hashtablee.g.: Vector, Stack, Hashtable

Goals: Goals: high-performance, interoperability, extensibilityhigh-performance, interoperability, extensibility

Algorithms: operate on collectionsAlgorithms: operate on collections provided as static class methodsprovided as static class methods

Iterator interface: to step through a collectionIterator interface: to step through a collection Collection view of a Map Collection view of a Map (stores key/value pairs) (stores key/value pairs)

Manipulates Manipulates ObjectsObjects

Page 5: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

“Core” Collection Interfaces “Core” Collection Interfaces

CollectionCollection

ListList SetSet

SortedSetSortedSet

group of objectsgroup of objects

ordered list of objectsordered list of objects

unordered group of unordered group of uniqueunique objects objects

ordered group of ordered group of uniqueunique objects objects

[in java.util][in java.util]

Page 6: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

boolean add(Object boolean add(Object objobj))

boolean addAll(Collection boolean addAll(Collection cc))

void clear( )void clear( )

boolean contains(Object boolean contains(Object objobj))

boolean containsAll(Collection boolean containsAll(Collection cc))

boolean equals(Object boolean equals(Object objobj))

int hashCode( )int hashCode( )

Collection Interface MethodsCollection Interface Methods

boolean isEmpty( )boolean isEmpty( )

Iterator iterator( )Iterator iterator( )

boolean remove(Object boolean remove(Object objobj))

boolean removeAll(Collection boolean removeAll(Collection cc))

boolean retainAll(Collection boolean retainAll(Collection cc) )

int size( )int size( )

Object[ ] toArray( )Object[ ] toArray( )

Object[ ] toArray(Object Object[ ] toArray(Object arrayarray[ ])[ ])

Exceptional Notes:Exceptional Notes:

•UnsupportedOperationExceptionUnsupportedOperationException

•ClassCastExceptionClassCastException

Page 7: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

void add(int index, Object void add(int index, Object objobj))

boolean addAll(int boolean addAll(int indexindex, , Collection Collection cc))

Object get(int Object get(int indexindex))

int indexOf(Object int indexOf(Object objobj))

int lastIndexOf(Objectint lastIndexOf(Object obj obj))

List Interface MethodsList Interface Methods

ListIterator listIterator( )ListIterator listIterator( )

ListIterator listIterator(int ListIterator listIterator(int indexindex))

Object remove(int Object remove(int indexindex))

Object set(int Object set(int indexindex, Object , Object objobj))

List subList(int List subList(int startstart, int , int endend))

Position in list uses 0-based index.Position in list uses 0-based index.

Page 8: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

Comparator comparator( )Comparator comparator( )

Object first( )Object first( )

SortedSet headSet(Object SortedSet headSet(Object endend))

Object last( )Object last( )

SortedSet subSet(Object SortedSet subSet(Object startstart, Object , Object endend))

SortedSet tailSet(Object SortedSet tailSet(Object startstart))

Set & Sorted Set InterfacesSet & Sorted Set Interfaces

SetSet does not define any additional methods of its own. does not define any additional methods of its own.

SortedSet MethodsSortedSet Methods

Page 9: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

Collection Classes Collection Classes

CollectionCollection

ListList SetSet

SortedSetSortedSet

NoteNote: Here we use to denote “implements”: Here we use to denote “implements”

AbstractCollectionAbstractCollection

AbstractListAbstractList

AbstractSequentialListAbstractSequentialList

LinkedListLinkedList

[in java.util][in java.util]

ArrayListArrayList

AbstractSetAbstractSet

HashSetHashSet TreeSetTreeSet

stores values stores values

in sorted orderin sorted order

Page 10: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

ArrayList ClassArrayList Class

Variable-length array of object referencesVariable-length array of object references legacy class Vector also supports dynamic array sizinglegacy class Vector also supports dynamic array sizing

ConstructorsConstructors ArrayList( )ArrayList( )

ArrayList(Collection ArrayList(Collection cc)) ArrayList(int ArrayList(int capacitycapacity))

Page 11: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

LinkedList ClassLinkedList Class

ConstructorsConstructors LinkedList( )LinkedList( ) LinkedList(Collection LinkedList(Collection cc))

Other Methods:Other Methods: void addFirst(Object void addFirst(Object objobj)) void addLast(Object void addLast(Object objobj)) Object getFirst( )Object getFirst( ) Object getLast( )Object getLast( ) Object removeFirst( )Object removeFirst( ) Object removeLast( )Object removeLast( )

Page 12: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

HashSet ClassHashSet Class

ConstructorsConstructors HashSet( )HashSet( ) HashSet(Collection HashSet(Collection cc)) HashSet(int HashSet(int capacitycapacity)) HashSet(int HashSet(int capacity, capacity, floatfloat fillRatio fillRatio))

Page 13: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

TreeSet ClassTreeSet Class

ConstructorsConstructors TreeSet( )TreeSet( ) TreeSet(Collection TreeSet(Collection cc)) TreeSet(Comparator TreeSet(Comparator compcomp)) TreeSet(SortedSet TreeSet(SortedSet ssss))

stores values in sorted orderstores values in sorted order

Page 14: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

Iterator InterfaceIterator Interface

IteratorIterator

ListIteratorListIterator

cycle through collection get/remove Objectscycle through collection get/remove Objects

• bi-directional traversalbi-directional traversal

• modify Objectsmodify Objects

boolean hasNext( )boolean hasNext( )

Object next( )Object next( )

void remove( )void remove( )

boolean add(Object boolean add(Object objobj))

boolean hasNext( )boolean hasNext( )

boolean hasPrevious( )boolean hasPrevious( )

Object next( )Object next( )

int nextIndex( )int nextIndex( )Object previous( )Object previous( )int previousIndex( )int previousIndex( )void remove( )void remove( )void set(Object void set(Object obj)obj)

Page 15: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

Comparator InterfaceComparator Interface

ComparatorComparator defines meaning of “sorted order”defines meaning of “sorted order”

int compare( Object int compare( Object obj1obj1, Object , Object obj2obj2))

boolean equals(Object boolean equals(Object objobj))

Page 16: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

Map InterfaceMap Interface

Map: Object that stores key/value pairsMap: Object that stores key/value pairs stores associationsstores associations keys, values are Objectskeys, values are Objects key is unique (value need not be unique)key is unique (value need not be unique)

MapMap

SortedMapSortedMap

stores key/value pairsstores key/value pairs

Map.EntryMap.Entry represents a key/value pairrepresents a key/value pair

stores key/value pairs in stores key/value pairs in ascending (key) orderascending (key) order

Page 17: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

void clear( )void clear( )

boolean containsKey(Object boolean containsKey(Object kk))

boolean containsValue(Object boolean containsValue(Object vv) )

Set entrySet( )Set entrySet( )

boolean equals(Object boolean equals(Object objobj))

Object Object getget(Object (Object kk))

int hashCode( )int hashCode( )

Map Interface MethodsMap Interface Methods

Exceptional Notes:Exceptional Notes:•UnsupportedOperationExceptionUnsupportedOperationException•NoSuchElementExceptionNoSuchElementException•ClassCastExceptionClassCastException•NullPointerExceptionNullPointerException

boolean isEmpty( )boolean isEmpty( )

Set keySet( )Set keySet( )

Object Object putput(Object (Object kk, Object , Object vv))

void putAll(Map void putAll(Map mm))

Object remove(Object Object remove(Object kk))

int size( )int size( )

Collection values( )Collection values( )

Page 18: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

Comparator comparator( )Comparator comparator( )

Object firstKey( )Object firstKey( )

SortedMap headMap(Object SortedMap headMap(Object endend) )

Object lastKey( )Object lastKey( )

SortedMap subMap(Object SortedMap subMap(Object startstart, Object , Object endend) )

SortedMap tailMap(Object SortedMap tailMap(Object startstart))

SortedMap Interface MethodsSortedMap Interface Methods

Exceptional Notes:Exceptional Notes:•NoSuchElementExceptionNoSuchElementException•ClassCastExceptionClassCastException•NullPointerExceptionNullPointerException

Page 19: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

boolean equals(Object boolean equals(Object objobj) )

Object getKey( )Object getKey( )

Object getValue( )Object getValue( )

int hashCode( ) int hashCode( )

Object setValue(Object Object setValue(Object vv))

Map.Entry Interface MethodsMap.Entry Interface Methods

Page 20: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

Map Classes Map Classes

NoteNote: Here we use to denote “implements”: Here we use to denote “implements”

[in java.util][in java.util]MapMap

AbstractMapAbstractMap

TreeMapTreeMap HashMapHashMap

WeakHashMapWeakHashMap

garbage collection for unused keysgarbage collection for unused keys

stores values stores values

in sorted orderin sorted order

Page 21: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

HashMap ClassHashMap Class

ConstructorsConstructors HashMap( )HashMap( ) HashMap(Map HashMap(Map mm)) HashMap(int HashMap(int capacitycapacity)) HashMap(int HashMap(int capacity, capacity, floatfloat fillRatio fillRatio))

NoteNote: Does not guarantee order of items!: Does not guarantee order of items!

Page 22: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

TreeMap ClassTreeMap Class

ConstructorsConstructors TreeMap( )TreeMap( ) TreeMap(Comparator TreeMap(Comparator compcomp)) TreeMap(Map TreeMap(Map mm)) TreeMap(SortedMap TreeMap(SortedMap smsm))

Allows rapid retrievalAllows rapid retrieval

stores values in sorted orderstores values in sorted order

Page 23: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

Collection AlgorithmsCollection Algorithms

static int binarySearch(List static int binarySearch(List listlist, Object , Object valuevalue, Comparator , Comparator cc))static int binarySearch(List static int binarySearch(List listlist, Object, Object value value))static void copy(List static void copy(List list1list1, List , List list2list2))static Enumeration enumeration(Collection static Enumeration enumeration(Collection cc))static void fill(List static void fill(List listlist, Object , Object objobj))static Object max(Collection static Object max(Collection cc, Comparator , Comparator compcomp))static Object max(Collection static Object max(Collection cc))static Object min(Collection static Object min(Collection cc, Comparator , Comparator compcomp))static Object min(Collection static Object min(Collection cc))static List nCopies(int static List nCopies(int numnum, Object , Object objobj))static void reverse( List static void reverse( List listlist))static Comparator reverseOrder( )static Comparator reverseOrder( )

Page 24: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

Collection Algorithms (continued)Collection Algorithms (continued)

static void shuffle(List static void shuffle(List listlist, Random, Random r r))static void shuffle(List static void shuffle(List listlist))static Set singleton(Object static Set singleton(Object objobj))static void sort(List static void sort(List listlist, Comparator , Comparator compcomp))static void sort(List static void sort(List listlist))static Collection synchronizedCollection(Collection static Collection synchronizedCollection(Collection cc))static List synchronizedList(List static List synchronizedList(List listlist))static Map synchronizedMap(Map static Map synchronizedMap(Map mm))static Set synchronizedSet(Set static Set synchronizedSet(Set ss))static SortedMap synchronizedSortedMap(SortedMap static SortedMap synchronizedSortedMap(SortedMap smsm))static SortedSet synchronizedSortedSet(SortedSet static SortedSet synchronizedSortedSet(SortedSet ssss))

Page 25: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

Collection Algorithms (continued)Collection Algorithms (continued)

Static Collection unmodifiableCollection(Collection Static Collection unmodifiableCollection(Collection cc))

Static List unmodifiableList(List Static List unmodifiableList(List listlist))

Static Map unmodifiableMap(Map Static Map unmodifiableMap(Map mm))

Static Set unmodifiableSet(Set Static Set unmodifiableSet(Set ss))

Static SortedMap unmodifiableSortedMap(SortedMap sStatic SortedMap unmodifiableSortedMap(SortedMap smm))

Static SortedSet unmodifiableSortedSet(SortedSet Static SortedSet unmodifiableSortedSet(SortedSet ssss))