1 / 25

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 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.

sharis
Télécharger la présentation

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

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  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. 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]

  6. 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

  7. 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.

  8. 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

  9. 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”

  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 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( )

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

  16. 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

  17. 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

  18. 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

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

  20. 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”

  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) stores values in sorted order Allows rapid retrieval

  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)

More Related