You are on page 1of 37

Tuesday, January 4, 8:46:44 AM

Vladimir Misic vm(at)cs.rit.edu

Introduction to Java Collection Framework

1
Generics – Reading for next week
Tuesday, January 4, 8:46:44 AM

• http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf
Vladimir Misic vm(at)cs.rit.edu

2
What are collections?
Tuesday, January 4, 8:46:44 AM

• Most programs utilise collections of data


– A set of users
– Words in a dictionary
Vladimir Misic vm(at)cs.rit.edu

– A list of students
– A list relating people to email addresses

• Higher-order collections are also useful


– A set of lists
– A list of sets
–…

3
What is Java Collection Framework?
Tuesday, January 4, 8:46:44 AM

• A framework is an extensive set of interfaces,


abstract classes and concrete classes together
with support tools
• Java provides a collection framework: JCF
Vladimir Misic vm(at)cs.rit.edu

• In short, it contains facilities which allow us to


represent data using
– Sets
– Lists
– Maps
• Also contains lots of useful algorithms
– E.g. sorting, searching, manipulation…

4
Reasons for Collections Framework use
Tuesday, January 4, 8:46:44 AM

• Interoperability between unrelated APIs


• Reduces the effort required to learn APIs
• Reduces the effort required to design and
Vladimir Misic vm(at)cs.rit.edu

implement APIs
• Fosters software reuse

5
Key idea
Tuesday, January 4, 8:46:44 AM

• The Java Collection Framework works by


separating interfaces from implementations
• Recall that an interface is a class definition in
Vladimir Misic vm(at)cs.rit.edu

which all methods are abstract


• An interface serves as a specification of what
any class implementing it should provide

6
Interface-based design
Tuesday, January 4, 8:46:44 AM

Separation of interface and implementation

• Extensive use of Polymorphism. Typically:


Vladimir Misic vm(at)cs.rit.edu

– List l = new LinkedList();

– List is interface
– LinkedList is implemented class
– Calling l.add() invokes method of class LinkedList

7
Interface-Based Design
Tuesday, January 4, 8:46:44 AM
Vladimir Misic vm(at)cs.rit.edu

interface List {…}


class LinkedList implements List
{…}

List lst = new LinkedList();
lst.add( new Student() );
Student s = (Student)lst.get(0);

8
Overview: Core Interfaces
Tuesday, January 4, 8:46:44 AM

Collection Map
Vladimir Misic vm(at)cs.rit.edu

SortedMap
Set List

SortedSet

9
Concrete Collections
Tuesday, January 4, 8:46:44 AM

CONCRETE IMPLEMENTS DESCRIPTION


COLLECTION
Vladimir Misic vm(at)cs.rit.edu

HashSet Set hash table


TreeSet SortedSet balanced binary tree
ArrayList List resizable-array
LinkedList List linked list
Vector List resizable-array
HashMap Map hash table
TreeMap SortedMap balanced binary tree
Hashtable Map hash table

10
Overview: Utilities
Tuesday, January 4, 8:46:44 AM

• Utility Interfaces
– Comparator
– Iterator
Vladimir Misic vm(at)cs.rit.edu

• Utility Classes
– Collections
– Arrays

11
Interface Collection
Tuesday, January 4, 8:46:44 AM

• A group of objects
• Major methods:
Vladimir Misic vm(at)cs.rit.edu

– int size();
– boolean isEmpty();
– boolean contains(Object);
– Iterator iterator();
– Object[] toArray();
– boolean add(Object);
– boolean remove(Object);
– void clear();
– ...

12
Interface Collection
Tuesday, January 4, 8:46:44 AM

METHOD NAME METHOD DOES


•add(o) Add a new element
• addAll(c) Add a collection
Remove all elements
Vladimir Misic vm(at)cs.rit.edu

•clear()
•contains(o) Membership checking.
•containsAll(c) Inclusion checking
•isEmpty() Whether it is empty
•iterator() Return an iterator
•remove(o) Remove an element
•removeAll(c) Remove a collection
•retainAll(c) Keep the elements
•size() The number of elements

13
To iterate through collection… use iterator
Tuesday, January 4, 8:46:44 AM

• The Iterator interface:


interface Iterator {
boolean hasNext();
Object next();
Vladimir Misic vm(at)cs.rit.edu

void remove();
}

• The iterator() method defined in the Collection


interface:
Iterator iterator()

• More about iterators, later…

14
Set
Tuesday, January 4, 8:46:44 AM

• interface Set extends Collection


• An unordered collection of objects
• No duplicate elements
Vladimir Misic vm(at)cs.rit.edu

• Same methods as Collection


– Semantics are different, so different interface needed
for design
• Implemented by:
– HashSet, TreeSet

15
An Example
Tuesday, January 4, 8:46:44 AM

/*
* Print out the unique strings in the phrase:
* "It was the best of times, it was the worst of times."
* @author Sean Strout
*/

import java.util.*; // HashSet, StringTokenizer

public class TestHashSet {


public static void main (String args[]) {
Vladimir Misic vm(at)cs.rit.edu

// The phrase
String phrase = "It was the best of times, it was the worst of times.";

// Create a hash set


Set<String> set = new HashSet<String>();

// Extract words from the phrase using a StringTokenizer.


// The characters that separate words in this phrase are
// space, comma and period.
StringTokenizer st = new StringTokenizer(phrase, " ,.");

// Put each word into the set


while (st.hasMoreTokens()) {
% java TestSet
set.add(st.nextToken());
} The set: [the, of, it, It, times, best, worst, was]

// Print out the whole set


The elements: the of it It times best worst was
System.out.println("The set: " + set);

// Display the elements in the set using an Iterator


System.out.print("The elements:");
Iterator iter = set.iterator();
while (iter.hasNext()) {
System.out.print(" " + iter.next());
}

} // main
} // TestHashSet
16
An Example
Tuesday, January 4, 8:46:44 AM

import java.util.*;

public class FindDups {

public static void main(String args[])


{
Set s = new HashSet();
Vladimir Misic vm(at)cs.rit.edu

for (int i=0; i<args.length; i++)


if (!s.add(args[i]))
System.out.println("Duplicate detected: "+args[i]);

System.out.println(s.size()+" distinct words detected: "+s);


}
}

// If you want sorted output, use TreeSet instead of HashSet

% java FindDups a qwe b qwe c qwe


Duplicate detected: qwe
Duplicate detected: qwe
4 distinct words detected: [a, b, c, qwe]

17
List
Tuesday, January 4, 8:46:44 AM

• interface List extends Collection


• An ordered collection of objects
• Duplicates allowed
Vladimir Misic vm(at)cs.rit.edu

18
List Details
Tuesday, January 4, 8:46:44 AM

• Major additional methods:


– Object get(int);
– Object set(int, Object);
– int indexOf(Object);

Vladimir Misic vm(at)cs.rit.edu

int lastIndexOf(Object);
– void add(int, Object);
– Object remove(int);
– List subList(int, int);
• add() inserts
• remove() deletes
• Implemented by:
– ArrayList, LinkedList, Vector

19
An Example
Tuesday, January 4, 8:46:44 AM

// silly example:

import java.util.*;

public class Sort {


public static void main(String args[]) {
List l = Arrays.asList(args);
Collections.sort(l);
Vladimir Misic vm(at)cs.rit.edu

System.out.println(l);
}
}

% java Sort bob adam dave carol


[adam, bob, carol, dave]

20
An Example
Tuesday, January 4, 8:46:44 AM

// silly example using generics:

import java.util.*;

public class Sortgen {


public static void main(String args[]) {
List<String> l = Arrays.asList(args);
Collections.sort(l);
Vladimir Misic vm(at)cs.rit.edu

System.out.println(l);
}
}

% java Sortgen bob adam dave carol


[adam, bob, carol, dave]

21
Map
Tuesday, January 4, 8:46:44 AM

• interface Map (does not extend Collection)


• An object that maps keys to values
• Each key can have at most one value
Vladimir Misic vm(at)cs.rit.edu

• Replaces java.util.Dictionary interface


• Ordering may be provided by implementation
class, but not guaranteed

22
Map Details
Tuesday, January 4, 8:46:44 AM

• Major methods:
– int size();
– boolean isEmpty();
– boolean containsKey(Object);

Vladimir Misic vm(at)cs.rit.edu

boolean containsValue(Object);
– Object get(Object);
– Object put(Object, Object);
– Object remove(Object);
– void putAll(Map);
– void clear();

• Implemented by:
– HashMap, Hashtable, WeakHashMap, Attributes

23
Accessing all members of Map
Tuesday, January 4, 8:46:44 AM

• Methods
– Set keySet();
– Collection values();
– Set entrySet();
• Map.Entry
Vladimir Misic vm(at)cs.rit.edu

– Object that contains a key-value pair


• getKey(), getValue()
• Thread safety
– The collections returned are backed by the map
• When the map changes, the collection changes
– Behavior can easily become undefined
• Be very careful and read the docs closely

24
An Example
Tuesday, January 4, 8:46:44 AM

import java.util.*;

public class MapExample {


public static void main(String args[]) {

Map map = new HashMap();


Integer ONE = new Integer(1);
Vladimir Misic vm(at)cs.rit.edu

for (int i=0, n=args.length; i<n; i++) {


String key = args[i];
Integer frequency = (Integer)map.get(key);

if (frequency == null) {
frequency = ONE;
} else {
int value = frequency.intValue();
frequency = new Integer(value + 1);
}

%java MapExample ko jel ja ko jel ti


map.put(key, frequency);
} {ti=1, ko=2, ja=1, jel=2}
{ja=1, jel=2, ko=2, ti=1}
System.out.println(map);
Map sortedMap = new TreeMap(map);
System.out.println(sortedMap);
}
}

25
An Example
Tuesday, January 4, 8:46:44 AM

import java.util.*;
% java MapExampleVM 3
public class MapExampleVM {
Name: ben
public static void main(String args[]) {
Grade: b
Map map = new HashMap();
Name: chad
String name, grade;
Grade: c
Vladimir Misic vm(at)cs.rit.edu

Scanner sc = new Scanner(System.in);


for(int i=0; i<Integer.parseInt(args[0]); i++) {
System.out.print("Name: "); Name: adam
name = sc.nextLine(); Grade: a
System.out.print("Grade: ");
grade = sc.nextLine();
System.out.println(); {chad=c, ben=b, adam=a}
{adam=a, ben=b, chad=c}
map.put(name, grade);
---------------
}

System.out.println(map); Gimme name, I'l spit grade! Name:


Map sortedMap = new TreeMap(map); daniel
System.out.println(sortedMap);
daniel is not mapped!
System.out.println("---------------");

System.out.println("\nGimme name, I'l spit grade! Name: ");


name = sc.nextLine();
if(map.containsKey(name))
System.out.println(name + "\'s grade is " + map.get(name));
else
System.out.println(name + " is not mapped!");
}
} 26
Iterators
Tuesday, January 4, 8:46:44 AM

• Iterators provide a way of visiting the elements


of a collection one by one
• Already met idea of using an iterator in a loop
Vladimir Misic vm(at)cs.rit.edu

– for (int i=0; i < 10; i++) { . . . }


• The Iterator interface is more abstract and can
be applied to any collection
Interface Iterator {
Object next();
boolean hasNext();
void remove();
}
• Think of an iterator as a cursor that resides
between elements of the collection
27
Iterator methods
Tuesday, January 4, 8:46:44 AM

Object next() Moves the iterator forward one position and


returns the element it has passed over
void remove() This removes the element that has just been
passed over by next()
Vladimir Misic vm(at)cs.rit.edu

boolean Returns true if the iterator can be moved forward


hasNext()

hasNext() next()
true

hasNext()
false
iterator
28
Iterator
Tuesday, January 4, 8:46:44 AM

• Represents a loop
• Created by Collection.iterator()
• Similar to Enumeration
Vladimir Misic vm(at)cs.rit.edu

– Improved method names


– Allows a remove() operation on the current item

29
Iterator Methods
Tuesday, January 4, 8:46:44 AM

• boolean hasNext()
– Returns true if the iteration has more elements
• Object next()
Vladimir Misic vm(at)cs.rit.edu

– Returns next element in the iteration


• void remove()
– Removes the current element from the underlying
Collection

30
ListIterator
Tuesday, January 4, 8:46:44 AM

• interface ListIterator extends Iterator


• Created by List.listIterator()
• Adds methods to
Vladimir Misic vm(at)cs.rit.edu

– traverse the List in either direction


– modify the List during iteration
• Methods added:
– hasPrevious(), previous()
– nextIndex(), previousIndex()
– set(Object), add(Object)

31
Set Implementations
Tuesday, January 4, 8:46:44 AM

• HashSet
– a Set backed by a hash table
• TreeSet
Vladimir Misic vm(at)cs.rit.edu

– A balanced binary tree implementation


– Imposes an ordering on its elements

32
List Implementations
Tuesday, January 4, 8:46:44 AM

• ArrayList
– a resizable-array implementation like Vector
• unsynchronized, and without legacy methods
• LinkedList
Vladimir Misic vm(at)cs.rit.edu

– a doubly-linked list implementation


– May provide better performance than ArrayList
• if elements frequently inserted/deleted within the List
– For queues and double-ended queues (deques)
• Vector
– a synchronized resizable-array implementation of a
List with additional "legacy" methods.

33
Map Implementations
Tuesday, January 4, 8:46:44 AM

• HashMap
– A hash table implementation of Map
– Like Hashtable, but supports null keys & values
Vladimir Misic vm(at)cs.rit.edu

• TreeMap
– A balanced binary tree implementation
– Imposes an ordering on its elements
• Hashtable
– Synchronized hash table implementation of Map
interface, with additional "legacy" methods.

34
WeakHashMap
Tuesday, January 4, 8:46:44 AM

• WeakHashMap
– Special-purpose implementation of Map interface
storing only weak references to its keys
Vladimir Misic vm(at)cs.rit.edu

– Allows key-value pairs to be garbage-collected when


the key is no longer referenced outside of the
WeakHashMap
– Useful for implementing "registry-like" data
structures, where the utility of an entry vanishes
when its key is no longer reachable by any thread.

35
Sorting
Tuesday, January 4, 8:46:44 AM

• Collections.sort() static method


• SortedSet, SortedMap interfaces
– Collections that keep their elements sorted
Vladimir Misic vm(at)cs.rit.edu

– Iterators are guaranteed to traverse in sorted order


• Ordered Collection Implementations
– TreeSet, TreeMap

36
Sorting (cont.)
Tuesday, January 4, 8:46:44 AM

• Comparable interface
– Must be implemented by all elements in SortedSet
Vladimir Misic vm(at)cs.rit.edu

– Must be implemented by all keys in SortedMap


– Method: int compareTo(Object o)
– Defines "natural order" for that object class
• Comparator interface
– Defines a function that compares two objects
– Can design custom ordering scheme
– Method: int compare(Object o1, Object o2)

37

You might also like