250 likes | 338 Vues
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.
E N D
UMass Lowell Computer Science 91.460Java and Distributed ComputingProf. Karen DanielsFall, 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 1 Fri, 9/8 9/15, 9/18 2 Fri, 9/15 Fri, 9/22 3 Fri, 9/22 Fri, 9/29 4 Fri, 10/6 Fri, 10/13 5 Fri, 10/13 Fri, 10/20 6 Fri, 10/20 Fri, 10/27 Graded Submitted Pending
Overview • Collections Framework • Iterator Interface • Comparator Interface • Map Interface • Collection Algorithms
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
group of objects Collection unordered group of unique objects Set List ordered list of objects ordered group of unique objects SortedSet “Core” Collection Interfaces [in java.util]
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[ ]) boolean add(Object obj) boolean addAll(Collection c) void clear( ) boolean contains(Object obj) boolean containsAll(Collection c) boolean equals(Object obj) int hashCode( ) • Exceptional Notes: • UnsupportedOperationException • ClassCastException
List Interface Methods void add(int index, Object obj) boolean addAll(int index, Collection c) Object get(int index) int indexOf(Object obj) int lastIndexOf(Object obj) 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.
Set & Sorted Set Interfaces Set does not define any additional methods of its own. Comparator comparator( ) Object first( ) SortedSet headSet(Object end) Object last( ) SortedSet subSet(Object start, Object end) SortedSet tailSet(Object start) SortedSet Methods
Collection Classes [in java.util] Collection List Set AbstractCollection SortedSet AbstractList AbstractSet ArrayList AbstractSequentialList HashSet TreeSet LinkedList stores values in sorted order Note: Here we use to denote “implements”
ArrayList Class • Variable-length array of object references • legacy class Vector also supports dynamic array sizing • Constructors • ArrayList( )ArrayList(Collection c) • ArrayList(int capacity)
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( )
HashSet Class • Constructors • HashSet( ) • HashSet(Collection c) • HashSet(int capacity) • HashSet(int capacity, float fillRatio)
TreeSet Class • Constructors • TreeSet( ) • TreeSet(Collection c) • TreeSet(Comparator comp) • TreeSet(SortedSet ss) stores values in sorted order
Iterator Interface Iterator cycle through collection get/remove Objects boolean hasNext( ) Object next( ) void remove( ) • bi-directional traversal • modify Objects ListIterator int nextIndex( ) Object previous( ) int previousIndex( ) void remove( ) void set(Object obj) boolean add(Object obj) boolean hasNext( ) boolean hasPrevious( ) Object next( )
Comparator Interface int compare( Object obj1, Object obj2) boolean equals(Object obj) Comparator defines meaning of “sorted order”
Map Interface • Map: Object that stores key/value pairs • stores associations • keys, values are Objects • key is unique (value need not be unique) stores key/value pairs Map represents a key/value pair Map.Entry stores key/value pairs in ascending (key) order SortedMap
Map Interface Methods boolean isEmpty( ) Set keySet( ) Object put(Object k, Object v) void putAll(Map m) Object remove(Object k) int size( ) Collection values( ) void clear( ) boolean containsKey(Object k) boolean containsValue(Object v) Set entrySet( ) boolean equals(Object obj) Object get(Object k) int hashCode( ) • Exceptional Notes: • UnsupportedOperationException • NoSuchElementException • ClassCastException • NullPointerException
SortedMap Interface Methods Comparator comparator( ) Object firstKey( ) SortedMap headMap(Object end) Object lastKey( ) SortedMap subMap(Object start, Object end) SortedMap tailMap(Object start) • Exceptional Notes: • NoSuchElementException • ClassCastException • NullPointerException
Map.Entry Interface Methods boolean equals(Object obj) Object getKey( ) Object getValue( ) int hashCode( ) Object setValue(Object v)
Map Classes [in java.util] Map AbstractMap TreeMap HashMap stores values in sorted order WeakHashMap garbage collection for unused keys Note: Here we use to denote “implements”
HashMap Class • Constructors • HashMap( ) • HashMap(Map m) • HashMap(int capacity) • HashMap(int capacity, float fillRatio) Note: Does not guarantee order of items!
TreeMap Class • Constructors • TreeMap( ) • TreeMap(Comparator comp) • TreeMap(Map m) • TreeMap(SortedMap sm) stores values in sorted order Allows rapid retrieval
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( )
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)
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)