Title: Unit I: Collections Framework
1Unit I Collections Framework
- Topics to be covered
- Introduction to Application Frameworks
- The Collections Framework
2Application Frameworks
- A set of cooperating classes that represent
reusable design of software systems in a
particular application domain - Consists of a set of abstract classes and
interfaces that are parts of semicomplete
applications that can be specialized to produce
custom applications - Often prescribes a set of conventions for
extending the abstract classes, implementing the
interfaces and allowing their instances to
interact with one another.
3Application Frameworks
- Goal
- To support the reuse of designs and
implementations - Examples
- GUI frameworks such as the Abstract Windows
Toolkit (AWT) and Swing - The collections framework
- The input/output framework
- Distributed computing frameworks such as the Java
Remote Method Invocation (RMI) and the Common
Object Request Broker Architecture (CORBA)
4Application Frameworks
- Characteristics
- Extensibility extend abstract classes
implement interfaces - Inversion of control framework not application
driven - Design patterns as building blocks
- Design patterns are schematic descriptions of
reusable designs are language independent - Frameworks are compilable programs, written in a
specific programming language contain abstract
classes interfaces
5Application Frameworks
- Design Requirements
- Completeness so that developers can select
classes appropriate to their needs - Adaptability platform dependent aspects
identified for substitutions - Efficiency in compilation, execution and ease of
use - Safety includes compiler being able to enforce
assumptions about the behaviour of a class
(type-checking) - Simplicity of use
- Extensibility for addition of new classes
6Collections Framework
- A collection (container) is an object that
contains other objects (elements) - The Collections framework is a set of interfaces
and classes that support storing retrieving
objects in collections of varying data
structures, algorithms and time-space
complexities - Classification of Collections (Abstract
Collections, Abstract Data Types) - Bags (multisets)
- Sets
- Lists (sequences)
- Maps (functions, dictionaries, associative
arrays)
7Collections Framework
- Bags
- Unordered collection of elements
- May contain duplicates
- Represented by the Collection interface
- Sets
- Unordered collection of elements
- No duplicates
- Represented by the Set interface
- Sorted sets are represented by the SortedSet
interface
8Collections Framework
- Lists
- Ordered collection, indexed sequentially starting
from 0 - Duplicate elements allowed
- Represented by the List interface
- Maps
- Unordered collection of key-value pairs
- Keys are unique (each key can only map to one
value) - Represented by the Map interface
- Sorted maps elements sorted by keys
- Represented by the SortedMap interface
9Java Collection Framework
10Methods of the Collection Interface
- add(o) Add a new element
- clear() Remove all elements
- contains(o) Membership checking.
- IsEmpty() Whether it is empty
- iterator() Return an iterator
- remove(o) Remove an element
- size() The number of elements
- Parameter o is of type Object
11Methods of the Set Interface
- Extends the Collection interface so all methods
of the Collection interface are inherited - However, some of the methods are overridden
- For example, add (o) adds the element o to this
set if it is not already present
12Methods of the List Interface
- Overrides some methods of the Collection
interface - Also introduces some new methods
- add(i,o) Insert o at position i
- add(o) Append o to the end
- get(i) Return the i-th element
- remove(i) Remove the i-th element
- remove(o) Remove the element o
- set(i,o) Replace the i-th element with o
- Parameter i is an integer representing an index
13Methods of the Map Interface
- Does not extend the Collection interface
- clear() Remove all mappings
- containsKey(k) Whether contains a mapping for k
- containsValue(v) Whether contains a mapping to v
- SetentrySet() Set of key-value pairs
- get(k) The value associated with k
- isEmpty() Whether it is empty
- keySet() Set of keys
- put(k,v) Associate v with k
- remove(k) Remove the mapping for k
- size() The number of pairs
- values() The collection of values
- Parameter k (v) is of type Object and represents
a key (value)
14Implementation of Collections
- Implementations of these collections are defined
as classes that implement the abstract collection
interfaces - Each of the abstract collections can be
implemented with various data structures and
algorithms (concrete collections) - Implementations will vary
- Bounded or unbounded
- Time and space complexity of operations such as
searching, inserting, deleting and iteration
15Implementation of Collections
- Different implementations of an abstract
collection will all implement the same interface - Since each concrete collection is a class that
implements the interface of an abstract
collection, different implementations of the same
abstract collection are interchangeable - An array implementation of a list can be switched
to a linked list and not affect the clients - But concrete implementations determine
performance, memory consumption, etc.
16Implementations for Set
- HashSet
- Stores the elements in a hash table
- In hashing, each item is associated with a hash
code - The code is based on some property of the item
- It is computed by a hash function (Java provides
a standard hashcode method for any object) - The hash code is used to locate the item
- Requires that the equals() hashCode methods be
properly defined in the class of the elements - Efficient in insertion membership checking
- LinkedHashSet
- Hash table and doubly linked list data structure
- Iteration order is predictable
17Implementations for Set
- TreeSet
- Stores the elements in a balanced binary tree
- A binary tree is a tree in which each node has at
most two children - Elements are ordered in their natural order or in
a user-defined order - Less efficient than HashSet in insertion
membership checking
18Implementations for List
- ArrayList
- Uses an array to store the elements
- Incurs a performance penalty if list exceeds the
size of the array - Wastes space if a large portion of the array is
unused - More efficient than LinkedList for methods
involving indices get(), set() - LinkedList
- Uses a doubly linked list to store the elements
19Implementations for Map
- HashMap
- Stores the entries in a hash table
- Efficient in insertion (put()) mapping (get())
- But iteration order is unpredictable determined
by the hash function of the keys - LinkedHashMap
- Iteration order is predictable usually is the
insertion order - IdentityHashMap
- Comparison in keys is based on identity not
equality - TreeMap
- Uses a balanced binary tree
- Entries ordered in their natural order or in a
user-defined order - Less efficient than HashMap for insertion and
mapping
20Iterate Through Collections
- Provides a uniform way to iterate through
different concrete collections - Two iterator interfaces
- Iterator for traversal in a forward direction
- ListIterator for traversal in both directions
21Iterate Through Collections
- The Iterator interface
- interface Iterator
- boolean hasNext()
- Object next()
- void remove()
-
- Usually the elements returned by the iterator
must be downcast to their actual classes before
they can be manipulated - The iterator() method defined in the Collection
interface - Iterator iterator()
22Using Set
Set set new HashSet() // instantiate a
concrete set // ... set.add(obj) // insert an
element // ... int n set.size() // get size
// ... if (set.contains(obj)) ... // check
membership // iterate through the set Iterator
iter set.iterator() while (iter.hasNext())
Object e iter.next() // downcast e // ...
23Using Map
Map map new HashMap() // instantiate a
concrete map // ... map.put(key, val) //
insert a key-value pair // ... // get the value
associated with key Object val map.get(key)
map.remove(key) // remove a key-value pair //
... if (map.containsValue(val)) ... if
(map.containsKey(kay)) ... Set keys
map.keySet() // get the set of keys // iterate
through the set of keys Iterator iter
keys.iterator() while (iter.hasNext()) Key
key (Key) iter.next() // ...
24Counting Different Words
- This example demonstrates the use of a set
- It reads some text and counts both the total
number of words and the number of different words - To count the number of different words in text,
we must remember the words that have already
occurred - Use a Set each word occurs only once in the set
even if it occurs multiple times in the text - Reminder of the StringTokenizer class
- Breaks a string into pieces (tokens) based on a
set of delimiters - public StringTokenizer(String arg, String delims)
creates a new instance of StringTokenizer with
the string initialized to arg and using the
specified delimiters
25Counting Different Words
public class CountWords static public void
main(String args) Set words new
HashSet() BufferedReader in new
BufferedReader( new
InputStreamReader(System.in)) String delim
" \t\n.,?!-/()\"\'" String line
int count 0
26Counting Different Words
try while ((line in.readLine()) !
null) StringTokenizer st
new StringTokenizer(line, delim) while
(st.hasMoreTokens()) count
words.add( st.nextToken().toLowerCa
se()) catch
(IOException e) System.out.println("Tota
l number of words "
count) System.out.println("Number of
different words "
words.size())
27Word Frequency
- This example demonstrates iterating through the
entry set of a map - To count the number of occurrences of each word,
we use a map that maps each word to the number of
its occurrences - To print the result, we first obtain the entry
set view of the map - Then we iterate through the entry set and print
each entry
28Word Frequency
public class Count public Count(String word,
int i) this.word word this.i i
public String word public int i
29Word Frequency (cont'd)
public class WordFrequency static public
void main(String args) Map words new
HashMap() String delim "
\t\n.,?!-/()\"\'" BufferedReader in
new BufferedReader( new
InputStreamReader(System.in)) String line,
word Count count
30Word Frequency (cont'd)
(class WordFrequency continued.) try
while ((line in.readLine()) ! null)
StringTokenizer st new
StringTokenizer(line, delim) while
(st.hasMoreTokens()) word
st.nextToken().toLowerCase() count
(Count) words.get(word) if (count
null) words.put(word,
new Count(word, 1)) else
count.i
catch (IOException e)
31Word Frequency (cont'd)
(class WordFrequency continued.) Set set
words.entrySet() Iterator iter
set.iterator() while (iter.hasNext())
Map.Entry entry (Map.Entry)
iter.next() word (String)
entry.getKey() count (Count)
entry.getValue() System.out.println(word
(word.length() lt 8 ? "\t\t" "\t")
count.i)
32Word Frequency Output
Using President Lincoln's The Gettysburg Address
as the input, the output is devotion
2 years 1 civil 1 place
1 gave 2 they
3 struggled 1 ...... men
2 remember 1 who 3 did
1 work 1 rather 2 fathers
1
33Ordering and Sorting
- There are two ways to define orders on objects.
-
- Each class can define a natural order among its
instances by implementing the Comparable
interface (only method is compareTo()). - int compareTo(Object o)
- The result is lt0 if this precedes o, gt0 if o
precedes this, and 0 otherwise
34Ordering and Sorting
- Arbitrary orders among different objects can be
defined by comparators, classes that implement
the Comparator interface (only method is
compare()). - int compare(Object o1, Object o2)
- The result is lt0 if o1 precedes o2, gt0 if o2
precedes o1, or 0 otherwise
35Word Frequency II
- This example demonstrates the use of a sorted map
according to the natural order of its keys - We want to count the number of occurrences of
each word and print the results as a list sorted
according to the alphabetical order of the words - The words are the keys of the map
- The alphabetical order of words is the natural
order of String - Use a TreeMap (by default, maintains its entries
according to the natural order of its keys)
36Word Frequency II
public class WordFrequency2 static public
void main(String args) Map words new
TreeMap() ltsame as WordFrequencygt
37Word Frequency II Output
Using President Lincoln's The Gettysburg Address
as the input, the output is a
7 above 1 add 1 address
1 advanced 1 ago 1 all
1 ...... whether 1 which
2 who 3 will 1 work
1 world 1 years 1
38Word Frequency III
- This example demonstrates the use of a sorted map
according to a user-defined order of the keys - We want to print the results as a list sorted
according to the reverse alphabetical order of
the words - To sort the entries in an order other than the
natural order of the keys, we need to define a
user-defined order - An instance of TreeMap can be instantiated by
specifying the user-defined comparator in the
constructor - Use the comparator for the reverse alphabetical
order of strings - Negate the sign of the result of the compareTo()
method of the String class to obtain the reverse
alphabetical order
39User-Defined Order
Reverse alphabetical order of strings public
class StringComparator implements
Comparator public int compare(Object o1,
Object o2) if (o1 ! null o2 !
null o1 instanceof String
o2 instanceof String) String s1
(String) o1 String s2 (String) o2
return - (s1.compareTo(s2)) else
return 0
40Word Frequency III
public class WordFrequency3 static public
void main(String args) Map words
new TreeMap(new StringComparator()) ltsame as
WordFrequencygt
41Word Frequency III Output
Using President Lincoln's The Gettysburg Address
as the input, the output is years
1 world 1 work 1 will
1 who 3 which 2 whether
1 ...... all 1 ago
1 advanced 1 address 1 add
1 above 1 a 7
42Sorting
public class CountComparator implements
Comparator public int compare(Object o1,
Object o2) if (o1 ! null o2 !
null o1 instanceof Count
o2 instanceof Count) Count c1 (Count)
o1 Count c2 (Count) o2 return
(c2.i - c1.i) else return 0
43Word Frequency IV
- This example demonstrates the use of sorting
utilities to sort collections according to
user-defined orders - We want to print the results as a list sorted by
the frequencies of occurrences of the words - The desired order is not based on keys but on
values - Use a HashMap to store the word counts
- Obtain a collection view of the values contained
in the map after all the words have been counted - Then sort the value collection according to a
user-defined comparator CountComparator
44Word Frequency IV
public class WordFrequency4 static public
void main(String args) ltsmae as
WordFrequencygt List list new
ArrayList(words.values())
Collections.sort(list, new
CountComparator()) Iterator iter
list.iterator() while (iter.hasNext())
count (Count) iter.next() word
count.word System.out.println(word
(word.length() lt 8 ? "\t\t" "\t")
count.i)
45Word Frequency IV Output
Using President Lincoln's The Gettysburg Address
as the input, the output is the
13 that 12 we 10 here
8 to 8 a 7 and
6 ...... consecrate 1 world
1 consecrated 1 remember 1 did
1 work 1 fathers 1