Using The Java Collections Framework: Ray Toal
Using The Java Collections Framework: Ray Toal
Framework
Ray Toal
Loyola Marymount University and
Ticketmaster Online-CitySearch, Inc.
April 7, 1999
Outline
• Background (Java, Collections, …)
• What is in the Collections Framework
• Organization of the Framework
• Tour of the Collections Framework
• Concluding Remarks
Goals and Objectives
• To present the overall organization and
examples of the use of the Java Collections
Framework so that
– Programmers will be able to start using the
framework right away
– Programmers will be able to get rid of tons of poorly
commented, under-tested, non-standard, collection
classes that defy (large-scale) reuse
What This Talk is About
• What is in the JCF and how the JCF is organized
• Why the JCF looks the way it does, and why it is
better than alternatives
• How to write code (right away) using the JCF
(via examples)
• Helping you to become a better Java
programmer
What This Talk is NOT About
• Introductory Java Programming
• Object Oriented Programming (though the basics
are assumed)
• Absolute details of the classes and interfaces
(we prefer code samples)
• Language Wars
• Swing
Background
Java
• An enormously popular, buzzword-compliant
language for the 1990s and beyond
• Not currently an international standard
• Consists of (1) a base language with the usual
keywords, declarations, expressions and
statements, and (2) a Core API
• Other languages would call the "Core API" a
"standard library"
The Java Core API
• Currently consists of 59 packages (see on-line
documentation for complete list)
• Informally, the Core API covers language
support, utilities, input/output, graphics, graphical
user interfaces, networking, concurrency and
distribution, security, database connectivity
The package java.util
• Has the following stuff:
– Collections!!
– Calendars, Dates, Time zone stuff
– i18n stuff: Locale, ResourceBundle
– Event stuff: EventObject, Observable, Observer...
– Some useful exception classes
– Really miscellaneous stuff: Random, Permission classes,
StringTokenizer
What is a Collection?
• An object that contains other objects
• Four main topologies: Set, Sequence, Hierarchy,
Network (some people like to throw in Dictionary)
• Other metrics: allows duplicates? sorted?
• Usually described by interfaces
• Don't confuse data types with data structures
JCF Overview
Simple Example 1
import java.util.*;
public class SimpleSetDemo {
public static void main(String[] args) {
Set s = new HashSet();
s.add("odin"); s.add("dva"); s.add("tri");
Set t = new TreeSet();
t.add("dva"); t.add("chityri"); t.add("shest");
s.retainAll(t);
for (Iterator it = s.iterator(); it.hasNext();)
System.out.println(it.next());
}
}
Simple Example 2
import java.util.*;
public class SimpleListDemo {
public static void main(String[] args) {
List s = new LinkedList();
s.add("odin"); s.add("dva"); s.add(1, "tri");
List t = new ArrayList();
t.add("dva"); t.add(0, "chityri"); t.set(1, "shest");
s.addAll(t);
for (Iterator it = s.subList(1, 4).iterator(); it.hasNext();)
System.out.println(it.next());
}
}
Simple Example 3
public class ExampleDictionaryProgram {
public static void main(String[] args) {
java.util.HashMap spanishWords = new HashMap();
java.util.HashMap russianWords = new HashMap();
spanishWords.put("red", "rojo");
spanishWords.put("brown", "caf\u00e9");
russianWords.put("black", "\u0447\u0451\u0440\u043d\u044b\u0439");
russianWords.put("white", "\u0431\u0435\u043b\u044b\u0439");
javax.swing.JOptionPane.showMessageDialog(null,
spanishWords.get("red"));
System.exit(0);
}
}
What's in the JCF?
• Collection Interfaces
• Collection Implementations
– general purpose / wrapper / abstract / convenience /
legacy
• Algorithms
– sort, shuffle, search, min, max, copy, fill, ...
• Infrastructure
– iterators, ordering, exceptions
Design Goals
• Small in size and conceptual weight
– don't let number of classes skyrocket by trying to
capture subtleties with distinct classes
– Method in interface IFF it is a basic operation or
there's a compelling reason why someone would
want to override
• Interoperability among reasonable
representations
Tour of the JCF
Collection Interfaces
Collection
«interface»
Map
Set List «interface»
«interface» «interface»
SortedMap
«interface»
SortedSet
«interface»
Collection
public interface Collection {
int size();
boolean isEmpty();
boolean contains(Object element);
boolean add(Object element);
boolean remove(Object element);
Iterator iterator();
Object[] toArray();
Object[] toArray(Object a[]);
}
Set
public interface Set extends Collection {
// intentionally empty.
}
List
public interface List extends Collection {
Object get(int index);
Object set(int index, Object element); // Optional
void add(int index, Object element); // Optional
Object remove(int index); // Optional
boolean addAll(int index, Collection c); // Optional
ListIterator listIterator();
ListIterator listIterator(int index);
Object first();
Object last();
Comparator comparator();
}
SortedMap
public interface SortedMap extends Map {
SortedMap subMap(Object fromKey, Object toKey);
SortedMap headMap(Object toKey);
SortedMap tailMap(Object fromKey);
Object first();
Object last();
Comparator comparator();
}
General Purpose Implementations
Interfaces Implementations