Presentation is loading. Please wait.

Presentation is loading. Please wait.

UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 17 Advanced Java Concepts Data Structure Support.

Similar presentations


Presentation on theme: "UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 17 Advanced Java Concepts Data Structure Support."— Presentation transcript:

1 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

2 Homework Status 1 Fri, 9/8 9/15, 9/18 2Fri, 9/15 Fri, 9/22 3Fri, 9/22 Fri, 9/29 4Fri, 10/6Fri, 10/13 5Fri, 10/13Fri, 10/20 6Fri, 10/20 Fri, 10/27 HW# Assigned Due Graded Submitted Pending

3 Overview ä Collections Framework ä Iterator Interface ä Comparator Interface ä Map Interface ä Collection Algorithms

4 Java Collections Framework ä Similar to C++ Standard Template Library (STL) ä C++ container -> Java collection ä Unifies previous Java data structure classes ä e.g.: Vector, Stack, Hashtable ä Goals: high-performance, interoperability, extensibility ä Algorithms: operate on collections ä provided as static class methods ä Iterator interface: to step through a collection ä Collection view of a Map (stores key/value pairs) Manipulates Objects

5 “Core” Collection Interfaces Collection List Set SortedSet group of objects ordered list of objects unordered group of unique objects ordered group of unique objects [in java.util]

6 boolean add(Object obj) boolean addAll(Collection c) void clear( ) boolean contains(Object obj) boolean containsAll(Collection c) boolean equals(Object obj) int hashCode( ) Collection Interface Methods boolean isEmpty( ) Iterator iterator( ) boolean remove(Object obj) boolean removeAll(Collection c) boolean retainAll(Collection c) int size( ) Object[ ] toArray( ) Object[ ] toArray(Object array[ ]) Exceptional Notes: UnsupportedOperationExceptionUnsupportedOperationException ClassCastExceptionClassCastException

7 void add(int index, Object obj) boolean addAll(int index, Collection c) Object get(int index) int indexOf(Object obj) int lastIndexOf(Object obj) List Interface Methods ListIterator listIterator( ) ListIterator listIterator(int index) Object remove(int index) Object set(int index, Object obj) List subList(int start, int end) Position in list uses 0-based index.

8 Comparator comparator( ) Object first( ) SortedSet headSet(Object end) Object last( ) SortedSet subSet(Object start, Object end) SortedSet tailSet(Object start) Set & Sorted Set Interfaces Set does not define any additional methods of its own. SortedSet Methods

9 Collection Classes Collection ListSet SortedSet Note: Here we use to denote “implements” AbstractCollection AbstractList AbstractSequentialList LinkedList [in java.util] ArrayList AbstractSet HashSetTreeSet stores values in sorted order

10 ArrayList Class ä Variable-length array of object references ä legacy class Vector also supports dynamic array sizing ä Constructors ä ArrayList( ) ArrayList(Collection c) ä ArrayList(int capacity)

11 LinkedList Class ä Constructors ä LinkedList( ) ä LinkedList(Collection c) ä Other Methods: ä void addFirst(Object obj) ä void addLast(Object obj) ä Object getFirst( ) ä Object getLast( ) ä Object removeFirst( ) ä Object removeLast( )

12 HashSet Class ä Constructors ä HashSet( ) ä HashSet(Collection c) ä HashSet(int capacity) ä HashSet(int capacity, float fillRatio)

13 TreeSet Class ä Constructors ä TreeSet( ) ä TreeSet(Collection c) ä TreeSet(Comparator comp) ä TreeSet(SortedSet ss) stores values in sorted order

14 Iterator Interface Iterator ListIterator cycle through collection get/remove Objects bi-directional traversal bi-directional traversal modify Objects modify Objects boolean hasNext( ) Object next( ) void remove( ) boolean add(Object obj) boolean hasNext( ) boolean hasPrevious( ) Object next( ) int nextIndex( ) Object previous( ) int previousIndex( ) void remove( ) void set(Object obj)

15 Comparator Interface Comparator defines meaning of “sorted order” int compare( Object obj1, Object obj2) boolean equals(Object obj)

16 Map Interface ä Map: Object that stores key/value pairs ä stores associations ä keys, values are Objects ä key is unique (value need not be unique) Map SortedMap stores key/value pairs Map.Entry represents a key/value pair stores key/value pairs in ascending (key) order

17 void clear( ) boolean containsKey(Object k) boolean containsValue(Object v) Set entrySet( ) boolean equals(Object obj) Object get(Object k) int hashCode( ) Map Interface Methods Exceptional Notes: UnsupportedOperationExceptionUnsupportedOperationException NoSuchElementExceptionNoSuchElementException ClassCastExceptionClassCastException NullPointerExceptionNullPointerException boolean isEmpty( ) Set keySet( ) Object put(Object k, Object v) void putAll(Map m) Object remove(Object k) int size( ) Collection values( )

18 Comparator comparator( ) Object firstKey( ) SortedMap headMap(Object end) Object lastKey( ) SortedMap subMap(Object start, Object end) SortedMap tailMap(Object start) SortedMap Interface Methods Exceptional Notes: NoSuchElementExceptionNoSuchElementException ClassCastExceptionClassCastException NullPointerExceptionNullPointerException

19 boolean equals(Object obj) Object getKey( ) Object getValue( ) int hashCode( ) Object setValue(Object v) Map.Entry Interface Methods

20 Map Classes Note: Here we use to denote “implements” [in java.util] Map AbstractMap TreeMapHashMap WeakHashMap garbage collection for unused keys stores values in sorted order

21 HashMap Class ä Constructors ä HashMap( ) ä HashMap(Map m) ä HashMap(int capacity) ä HashMap(int capacity, float fillRatio) Note: Does not guarantee order of items!

22 TreeMap Class ä Constructors ä TreeMap( ) ä TreeMap(Comparator comp) ä TreeMap(Map m) ä TreeMap(SortedMap sm) Allows rapid retrieval stores values in sorted order

23 Collection Algorithms static int binarySearch(List list, Object value, Comparator c) static int binarySearch(List list, Object value) static void copy(List list1, List list2) static Enumeration enumeration(Collection c) static void fill(List list, Object obj) static Object max(Collection c, Comparator comp) static Object max(Collection c) static Object min(Collection c, Comparator comp) static Object min(Collection c) static List nCopies(int num, Object obj) static void reverse( List list) static Comparator reverseOrder( )

24 Collection Algorithms (continued) static void shuffle(List list, Random r) static void shuffle(List list) static Set singleton(Object obj) static void sort(List list, Comparator comp) static void sort(List list) static Collection synchronizedCollection(Collection c) static List synchronizedList(List list) static Map synchronizedMap(Map m) static Set synchronizedSet(Set s) static SortedMap synchronizedSortedMap(SortedMap sm) static SortedSet synchronizedSortedSet(SortedSet ss)

25 Collection Algorithms (continued) Static Collection unmodifiableCollection(Collection c) Static List unmodifiableList(List list) Static Map unmodifiableMap(Map m) Static Set unmodifiableSet(Set s) Static SortedMap unmodifiableSortedMap(SortedMap sm) Static SortedSet unmodifiableSortedSet(SortedSet ss)


Download ppt "UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 17 Advanced Java Concepts Data Structure Support."

Similar presentations


Ads by Google