cs 3331 1 collection and input/output classes cs 3331 fall 2009
TRANSCRIPT
![Page 1: CS 3331 1 Collection and Input/Output Classes CS 3331 Fall 2009](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649dc65503460f94ab9db5/html5/thumbnails/1.jpg)
CS 3331 1
Collection and Input/Output Classes
CS 3331
Fall 2009
![Page 2: CS 3331 1 Collection and Input/Output Classes CS 3331 Fall 2009](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649dc65503460f94ab9db5/html5/thumbnails/2.jpg)
2CS 3331
Outline
Collection classes Collection interfaces Implementation classes Iterators Ordering
Input/output classes
![Page 3: CS 3331 1 Collection and Input/Output Classes CS 3331 Fall 2009](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649dc65503460f94ab9db5/html5/thumbnails/3.jpg)
3CS 3331
Collection Classes in Java Major types of collections
Sets, bags, lists, maps Defined in the java.util package
Interfaces of collections
![Page 4: CS 3331 1 Collection and Input/Output Classes CS 3331 Fall 2009](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649dc65503460f94ab9db5/html5/thumbnails/4.jpg)
4CS 3331
Collection InterfaceMethod Description
add(o) Add a new elementaddAll(c) Add all elements of c
remove(o) Remove an element removeAll(c) Remove all elements found in cretainAll(c) Retain only elements found in cclear() Remove all elements
contains(o) Membership testing containsAll(c) Membership testing
isEmpty() Whether it is empty size() The number of elements
iterator() Return an iterator
![Page 5: CS 3331 1 Collection and Input/Output Classes CS 3331 Fall 2009](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649dc65503460f94ab9db5/html5/thumbnails/5.jpg)
5CS 3331
Set InterfaceMethod Description
add(o) Add an element if not already presentaddAll(c) Add each elements of c if not present
![Page 6: CS 3331 1 Collection and Input/Output Classes CS 3331 Fall 2009](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649dc65503460f94ab9db5/html5/thumbnails/6.jpg)
6CS 3331
Exercise Define a set-intersection method.
public static Set union(Set x, Set y) { Set s = new HashSet(); // HashSet will be discussed later. s.addAll(x); s.addAll(y); return s;}public static Set intersection(Set x, Set y) { // WRITE YOUR CODE HERE
}
![Page 7: CS 3331 1 Collection and Input/Output Classes CS 3331 Fall 2009](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649dc65503460f94ab9db5/html5/thumbnails/7.jpg)
7CS 3331
List InterfaceMethod Description
add(i, o) Insert o at the i-th positionadd(o) Append o at the endaddAll(i, c) Insert all elements of c starting at the i-th positionaddAll(c) Append all elements of c at the end
remove(i) Remove i-th elementremove(o) Remove the first occurrence of o set(i, o) Replace i-th element with o
get(i) Return i-th elementindexOf(o) Return the index of the first occurrence of olastIndexOf(o) Return the index of the last occurrence of olistIteratpr() Return a list iteratorlistIterator(i) Return a list iterator for the sublist starting from isubList(i, j) Retrun a sublist between index iand j
![Page 8: CS 3331 1 Collection and Input/Output Classes CS 3331 Fall 2009](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649dc65503460f94ab9db5/html5/thumbnails/8.jpg)
8CS 3331
Map InterfaceMethod Description
put(k,v) Associate v with k
remove(k) Remove the mapping for k clear() Remove all mappings
get(k) The value associated with k
containsKey(k) Whether contains a mapping for k containsValue(v) Whether contains a mapping to v
size() The number of pairs isEmpty() Whether it is empty
….
![Page 9: CS 3331 1 Collection and Input/Output Classes CS 3331 Fall 2009](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649dc65503460f94ab9db5/html5/thumbnails/9.jpg)
9CS 3331
Map Interface (Cont.)
Method Description
entrySet() Set of key-value pairs keySet() Set of keys values() The collection of values
k1k2k3...kn
v1v2v3...vn
keySet() values()
entrySet()
![Page 10: CS 3331 1 Collection and Input/Output Classes CS 3331 Fall 2009](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649dc65503460f94ab9db5/html5/thumbnails/10.jpg)
10CS 3331
Outline
Collection classes Collection interfaces Implementation classes Iterators Ordering
Input/output classes
![Page 11: CS 3331 1 Collection and Input/Output Classes CS 3331 Fall 2009](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649dc65503460f94ab9db5/html5/thumbnails/11.jpg)
11CS 3331
Implementation of Collections
Why different implementations?Bounded vs. unboundedTime and space complexity of operations
Sets
Class Interface Description
HashSet Set Hash table LinkedHashSet Set Hash table & DLLTreeSet SortedSet Balanced binary tree
![Page 12: CS 3331 1 Collection and Input/Output Classes CS 3331 Fall 2009](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649dc65503460f94ab9db5/html5/thumbnails/12.jpg)
12CS 3331
Implementation (Cont.)
Lists
Class Interface Description
ArrayList List Resizable array LinkedList List Doubly linked listVector List Legacy of JDK 1.0
![Page 13: CS 3331 1 Collection and Input/Output Classes CS 3331 Fall 2009](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649dc65503460f94ab9db5/html5/thumbnails/13.jpg)
13CS 3331
Implementation (Cont.)
Maps
Class Interface Description
HashMap Map Hash table IdentityHashMap Map Hash table with identity comparisonLinkedHashMap Map Hash table and DLLTreeMap SortedMap Balanced binary treeHashtable Map Legacy of JDK 1.0
![Page 14: CS 3331 1 Collection and Input/Output Classes CS 3331 Fall 2009](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649dc65503460f94ab9db5/html5/thumbnails/14.jpg)
14CS 3331
Example (JDK 1.5 or above)
Counting word frequency
public static void frequence(String[] words) { Map<String, Integer> map = new LinkedHashMap<String, Integer>(); for (String w: words) { if (!map.containsKey(w)) { map.put(w, 1); } else { map.put(w, 1 + map.get(w)); } } for (String k: map.keySet()) { System.out.println(k + “:\t” + map.get(k)); }}
![Page 15: CS 3331 1 Collection and Input/Output Classes CS 3331 Fall 2009](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649dc65503460f94ab9db5/html5/thumbnails/15.jpg)
15CS 3331
Example (JDK 1.4 or below)
Counting word frequency
public static void frequence(String[] words) { Map map = new LinkedHashMap(); for (int i = 0; i < words.length; i++) { if (!map.containsKey(words[i])) { map.put(words[i], new Integer(1)); } else { map.put(words[i], new Integer(1 + ((Integer) map.get(words[i])).intValue()); } } for (Iterator i = map.keySet().iterator(); i.hasNext(); ) { String word = (String) i.next(); System.out.println(word + “:\t” + map.get(word)); }}
![Page 16: CS 3331 1 Collection and Input/Output Classes CS 3331 Fall 2009](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649dc65503460f94ab9db5/html5/thumbnails/16.jpg)
16CS 3331
Exercise Write a method that counts the number of different
words
/** Returns the number of different words in the array words. */public static int numOfDifferentWords(String[] words) { // WRITE YOUR CODE HERE
}
![Page 17: CS 3331 1 Collection and Input/Output Classes CS 3331 Fall 2009](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649dc65503460f94ab9db5/html5/thumbnails/17.jpg)
17CS 3331
Iterators of Collections
Iterator ListIterator
Method Description
add(o) Insert o in the current positionremove() Remove the last elementset(o) Replace the current element with o
hasNext() More element in the forward? hasPrevious() More element in the reverse?
next() Return the next element nextIndex() Return the next index
previous() Return the previous element previousIndex() Return the previous index
![Page 18: CS 3331 1 Collection and Input/Output Classes CS 3331 Fall 2009](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649dc65503460f94ab9db5/html5/thumbnails/18.jpg)
18CS 3331
Group Work: Set Implementation
Work in group of two or three to write class ArraySet that implements the Set interface. You should use an array to store the elements.
Method Description
add(o) Add a new elementaddAll(c) Add all elements of c
remove(o) Remove an element removeAll(c) Remove all elements found in cretainAll(c) Retain only elements found in cclear() Remove all elements
contains(o) Membership testing containsAll(c) Membership testing
isEmpty() Whether it is empty size() The number of elements
iterator() Return an iterator
![Page 19: CS 3331 1 Collection and Input/Output Classes CS 3331 Fall 2009](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649dc65503460f94ab9db5/html5/thumbnails/19.jpg)
19CS 3331
Outline
Collection classes Collection interfaces Implementation classes Iterators Ordering
Input/output classes
![Page 20: CS 3331 1 Collection and Input/Output Classes CS 3331 Fall 2009](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649dc65503460f94ab9db5/html5/thumbnails/20.jpg)
20CS 3331
Ordering and Sorting
Partial order (or order)Binary relation that is transitiveTotal order if a < b and b < a implies a = b
How to define order on objects?Natural order by implementing the
Comparable interfaceArbitrary order by comparators (classes
implementing the Comparator interface)
![Page 21: CS 3331 1 Collection and Input/Output Classes CS 3331 Fall 2009](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649dc65503460f94ab9db5/html5/thumbnails/21.jpg)
21CS 3331
Comparable Interface
Method compareTo: Result < 0, if the receiver precedes o Result = 0, if neither the receiver precedes o, nor o precedes the receiver Result > 0, if o precedes the receiver
Properties (or constraints) a.compareTo(b) > 0 implies that b.compareTo(a) < 0 a.compareTo(b) < 0 implies that b.compareTo(a) > 0 a.compareTo(b) = 0 implies that b.compareTo(a) = 0; Consistent with the definition of equals, i.e., a.equals(b) is true iff a.compareTo(b) is 0
public interface Comparable { int compareTo(Object o);}
![Page 22: CS 3331 1 Collection and Input/Output Classes CS 3331 Fall 2009](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649dc65503460f94ab9db5/html5/thumbnails/22.jpg)
22CS 3331
Exercise Define a natural order for the Person class based on
the person’s name. (Hint: the String class implements the Comparable interface.)
public class Person implements Comparable { private /*@ non_null @*/ String name;
public int compareTo(Object other) { // YOUR CODE HERE …
}}
![Page 23: CS 3331 1 Collection and Input/Output Classes CS 3331 Fall 2009](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649dc65503460f94ab9db5/html5/thumbnails/23.jpg)
23CS 3331
Comparator Interface
Method compare: Result < 0, if o1 precedes o2 Result = 0, if neither o1 precedes o2, nor o2 precedes o1 Result > 0, if o2 precedes o1
Properties (or constraints) c.compare(a,b) > 0 implies that c.compare(b,a) < 0 c.compare(a,b) < 0 implies that c.compare(b,a) > 0 c.compare(a,b) = 0 implies that c.compare(a,b) = 0; Consistent with equals, i.e., c.compare(a,b) is 0 iff a.equals(b) and b.equals(a)
public interface Comparator { int compare (Object o1, Object o2);}
![Page 24: CS 3331 1 Collection and Input/Output Classes CS 3331 Fall 2009](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649dc65503460f94ab9db5/html5/thumbnails/24.jpg)
24CS 3331
Exercise Define a total ordering for the Person class based on
the person’s SSN.
Public class Person { //@ ensures \result > 0; public int getSSN() { /*… */ } // other declarations …}
public class PersonComparator implements Comparator { public int compare(Object p1, Object p2) { // YOUR CODE HERE …
}}
![Page 25: CS 3331 1 Collection and Input/Output Classes CS 3331 Fall 2009](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649dc65503460f94ab9db5/html5/thumbnails/25.jpg)
25CS 3331
Sorted Collections
InterfacesSortedSet and SortedMap
ImplementationsTreeSet and TreeMap
ExampleSortedSet s1 = new TreeSet();
SortedSet s2 = new TreeSet(new PersonComparator());
SortedMap m1 = new TreeMap();
SortedMap m2 = new TreeMap(new PersonComparator());
![Page 26: CS 3331 1 Collection and Input/Output Classes CS 3331 Fall 2009](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649dc65503460f94ab9db5/html5/thumbnails/26.jpg)
26CS 3331
SortedSet InterfaceMethod Description
comparator() Return the comparator
first() Return the first (lowest) elementlast() Return the last (highest) element
headSet(o) Return elements less than otailSet(o) Return elements greater than or equal to osubSet(o1,o2) Return elements between o1 and o2
![Page 27: CS 3331 1 Collection and Input/Output Classes CS 3331 Fall 2009](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649dc65503460f94ab9db5/html5/thumbnails/27.jpg)
27CS 3331
Exercise Write a method that, given an array of Person objects, returns a
sorted array of the argument array. Assume that the Person class has a natural order defined.
public static Person[] sort(/*@ non_null @*/ Person[] persons) { // YOUR CODE HERE …
}
![Page 28: CS 3331 1 Collection and Input/Output Classes CS 3331 Fall 2009](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649dc65503460f94ab9db5/html5/thumbnails/28.jpg)
28CS 3331
SortedMap InterfaceMethod Description
comparator() Return the comparator
firstKey() Return the first (lowest) keylastKey() Return the last (highest) key
headMap(k) Return maplets less than ktailMap(k) Return maplets greater than or equal to ksubMap(k1,k2) Return maplets between k1 and k2
![Page 29: CS 3331 1 Collection and Input/Output Classes CS 3331 Fall 2009](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649dc65503460f94ab9db5/html5/thumbnails/29.jpg)
29CS 3331
Outline
Collection classes Collection interfaces Implementation classes Iterators Ordering
Input/output classes
![Page 30: CS 3331 1 Collection and Input/Output Classes CS 3331 Fall 2009](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649dc65503460f94ab9db5/html5/thumbnails/30.jpg)
30CS 3331
Input/Output Classes
Two types of input/outputStream I/O
Sequential reading and writing Opened for reading or writing, but not both Byte streams vs. character streams
Random access I/O Non-sequential reading and writing Can be opened for both reading and writing
![Page 31: CS 3331 1 Collection and Input/Output Classes CS 3331 Fall 2009](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649dc65503460f94ab9db5/html5/thumbnails/31.jpg)
31CS 3331
Byte Streams
![Page 32: CS 3331 1 Collection and Input/Output Classes CS 3331 Fall 2009](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649dc65503460f94ab9db5/html5/thumbnails/32.jpg)
32CS 3331
Character Streams
![Page 33: CS 3331 1 Collection and Input/Output Classes CS 3331 Fall 2009](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649dc65503460f94ab9db5/html5/thumbnails/33.jpg)
33CS 3331
Example Usage
Reading lines from file
String fileName = …;BufferedReader reader = new BufferedReader( new InputStreamReader(new FileInputStream(fileName)));String line;while ((line = reader.readLine()) != null) { // do something with line …}
![Page 34: CS 3331 1 Collection and Input/Output Classes CS 3331 Fall 2009](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649dc65503460f94ab9db5/html5/thumbnails/34.jpg)
34CS 3331
Decorator Pattern To add additional responsibility or capability to an object
dynamically