Java and Distributed Computing Lecture 17 UMass Lowell Computer Science 91.460
Download
Report
Transcript Java and Distributed Computing Lecture 17 UMass Lowell Computer Science 91.460
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
Graded
Submitted
Pending
1
2
3
4
5
6
Fri, 9/8
Fri, 9/15
Fri, 9/22
Fri, 10/6
Fri, 10/13
Fri, 10/20
9/15, 9/18
Fri, 9/22
Fri, 9/29
Fri, 10/13
Fri, 10/20
Fri, 10/27
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
“Core” Collection Interfaces
[in java.util]
group of objects
Collection
List
Set
unordered group of
unique objects
SortedSet
ordered group of
unique objects
ordered list of objects
Collection Interface Methods
boolean add(Object obj)
boolean addAll(Collection c)
void clear( )
boolean contains(Object obj)
boolean containsAll(Collection c)
boolean equals(Object obj)
int hashCode( )
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:
•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.
SortedSet Methods
Comparator comparator( )
Object first( )
SortedSet headSet(Object end)
Object last( )
SortedSet subSet(Object start, Object end)
SortedSet tailSet(Object start)
Collection Classes
[in java.util]
Collection
List
AbstractCollection
Set
SortedSet
AbstractList
AbstractSet
ArrayList
AbstractSequentialList
LinkedList
Note: Here we use
HashSet
to denote “implements”
TreeSet
stores values
in sorted order
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
ListIterator
• modify Objects
boolean add(Object obj)
boolean hasNext( )
boolean hasPrevious( )
Object next( )
int nextIndex( )
Object previous( )
int previousIndex( )
void remove( )
void set(Object obj)
Comparator Interface
Comparator
defines meaning of “sorted order”
int compare( Object obj1, Object obj2)
boolean equals(Object obj)
Map Interface
Map: Object that stores key/value pairs
stores
associations
keys, values are Objects
key is unique (value need not be unique)
Map
Map.Entry
SortedMap
stores key/value pairs
represents a key/value pair
stores key/value pairs in
ascending (key) order
Map Interface Methods
void clear( )
boolean containsKey(Object k)
boolean containsValue(Object v)
Set entrySet( )
boolean equals(Object obj)
Object get(Object k)
int hashCode( )
boolean isEmpty( )
Set keySet( )
Object put(Object k, Object v)
void putAll(Map m)
Object remove(Object k)
int size( )
Collection values( )
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)