CISC 3115
Modern Programming Techniques
Lecture 10
The Java Collection Framework (JCF)

Framework

Good definitions from Chat: In this context we have:

Collection

Some (Future) CISC 3130 Material

The different implementations of the various collections are usually primarily concerned with access/modification time

Interface Inheritance and Implementation, Class Inheritance

Remember:

Constructs that Support Common Behavior

Typical Common Behaviors

Here are some fundamental Java interfaces:

The Java Collections Framework

From the Oracle Java Tutorial

The Collection Hierarchy

Common Behavior -- The Polymorphic Receiver

If we program to the interface (or at the superclass level), the receiver can be an object of any implementing (sub) class
Object obj = …
…
Set set = new TreeSet();	// An implementing class of the Set interfce
set.add(obj);				// collections take Object as their element type
…
set = new HashSet();		// another implement class of Set
set.add(obj);
…
Collection coll = set;		// Set is a subinterfce of Collection
coll.add(obj);
…
coll = new ArrayList();		// ArrayList is an implementing class of List which is a subinterfcace of COllection
coll.add(obj);
…
coll = new Stack();			// Stack is a subclass of Vector which is an implementing class of 		
coll.add(obj);
…
…

void myAdd(Collection c, Object obj) {c.add(obj);}	// the receiver c could be any Collection object sent to themyAdd
…
…
myAdd(set, obj);
myAdd(coll, obj);

Common Behavior -- The Polymorphic Argument

As we've seen above, a related notion is that an implementing class object can be passed to any reference variable that is a legitimate upcast candidate (superclass, interface that it implements)
add(Object obj) {...}
void addAll(Collection colla;)		// defined in the Collection interface

The Map Interface

The Collection Hierarchy in Detail

(Iterable)

Collection

Iterator List Set Map SortedSet SortedMap ArrayList/Vector
Vector v = new Vector();
Scanner scanner = new Scanner(new File("...));
while (scanner.hasNextLine()
	v.add(scanner.nextLine());
		
Iterator iter = v.iterator();
while (iter.hasNext()) {
	String s = (String)iter.next();		// Have to state that it's a String
	System.err.println(s);					//  or do whatever you want with the string
}
	
Stack Queue HashSet
HashSet hs = new HashSet();
String line = br.readLine(); // assume BufferedReader br
while (line != null) {
	hs.add(line);					// could add ANY object to hs
	line = br.readLine();
}
		
Iterator iter = hs.iterator();			
while (iter.hasNext()) {					// No particular order
	String s = (String)iter.next();		// Have to state that it's a String
	System.err.println(s);					//  or do whatever you want with the string
}
	
HashMap
HashMap hm = new HashMap();
Employee employee = Employee.read(br); // assume BufferedReader br
while (employee != null) {
	hm.put(employee.getSSNum(), employee));	// could use ANY key or value for tm
	employee = Employee.read(br);
}
		
Set keys = hm.keySet();						// Get the set of keys
Iterator iter = keys.iterator();			
while (iter.hasNext()) {					// No particular order
	String sSNum = (String)iter.next();	// Have to state that it's a String
	Employee e = (Employee)tm.get(sSNum);		
	System.err.println(e);					//  or do whatever you want with the Employee object
}
	
TreeSet
TreeSet ts = new TreeSet();
String line = br.readLine(); // assume BufferedReader br
while (line != null) {
	ts.add(line);					// could add ANY object to ts
	line = br.readLine();
}
		
Iterator iter = ts.iterator();			
while (iter.hasNext()) {					// Natural order of String (alphabetical order)
	String s = (String)iter.next();		// Have to state that it's a String
	System.err.println(s);					//  or do whatever you want with the string
}
	
TreeMap
TreeMap tm = new TreeMap();
Employee employee = Employee.read(br); // assume BufferedReader br
while (employee != null) {
	tm.put(employee.getSSNum, employee));	// could use ANY key or value for tm
	line = br.readLine();
}

Set keys = tm.keySet();						// Get the set of keys
Iterator iter = keys.iterator();			
while (iter.hasNext()) {					// Natural order of the key -- a String (alphabetical order)
	String sSNum = (String)iter.next();	// Have to state that it's a String
	Employee e = (Employee)tm.get(sSNum);		
	System.err.println(e);					//  or do whatever you want with the Employee object
}
	
LinkedList Deque AbstractCollection

AbstractList

AbstractMap

AbstractQueue

AbstractSet

Autoboxing and Generics

Overview

As of Java 1.5 (Java 5), two highly useful (and long-awaited) features made their debut

Generics

Enhanced for loop

Random r = new Random();

Vector<Integer> v = new Vector<Integer>();
for (int i = 0; i < 100; i++)
	v.add(new Integer(r.nextInt(200)));

for (Integer i : v)
	System.out.println(i);
Map<String, Integer> concordance = new TreeMap<String, Integer>();
…
…
for (String s : concordance.keySet())
	System.out.println(s + ": " + concordance.get(s));

Autoboxing

Code for this Lecture