A Glimpse of Collections

Download Report

Transcript A Glimpse of Collections

Collections
A First Glimpse
Collections
• A collection is a structured group of objects
– An array is a kind of collection
– A Vector is a kind of collection
– A linked list is a kind of collection
• Java 1.2 introduced the Collections Framework
and provided many great implementations
– Vectors have been redefined to implement Collection
– Trees, linked lists, stacks, hash tables, and other classes
are implementations of Collection
– Arrays do not implement the Collection interfaces
Types of Collection
• Java supplies several types of Collection:
– Set: cannot contain duplicate elements, order is not
important
– SortedSet: like a Set, but order is important
– List: may contain duplicate elements, order is
important
• Java also supplies some “collection-like” things:
– Map: a “dictionary” that associates keys with values,
order is not important
– SortedMap: like a Map, but order is important
Collections are ADTs
• I’m not going to cover Collections just yet,
but I want to use them as an example
• Collections are one of the best-designed
parts of Java, because
– They are elegant: they combine maximum
power with maximum simplicity
– They are uniform: when you know how to use
one, you almost know how to use them all
– You can easily convert from one to another
Uniformity through interfaces
• Much of the elegance of the Collections Framework
arises from the intelligent use of interfaces
• For example, the Collection interface specifies
(among many other operations):
–
–
–
–
–
–
boolean add(Object o)
boolean isEmpty()
boolean remove()
int size()
Object[] toArray()
Iterator iterator()
Vectors
• The class Vector has been retrofitted to
implement the Collection interface
• Vector supplies add(Object) and
iterator() methods (among others)
• Let’s look at creating a Vector and
iterating through the elements
The Iterator interface
interface iterator { // java.lang.util
boolean hasNext();
// Returns true if the iteration has more
// elements.
Object next();
// Returns the next element in the
// interation.
void remove();
// Removes from the underlying collection
// the last element returned by the
// iterator (optional operation).
Using a Vector
Collection numerals = new Vector();
numerals .add("one");
numerals .add("two");
numerals .add("three");
Iterator iter = numerals.iterator();
while (iter.hasNext()) {
System.out.println(iter.next());
}
Results:
one
two
three
Using a TreeSet
Collection numerals = new TreeSet();
numerals .add("one");
numerals .add("two");
numerals .add("three");
Iterator iter = numerals.iterator();
while (iter.hasNext()) {
System.out.println(iter.next());
}
Results:
one
three
two
Conversions
• Vector v = new Vector(numerals);
• TreeSet ts = new TreeSet(v);
Back to arrays
• Arrays are not part of the new Collections Framework,
but they haven’t been ignored
• Java 1.2 introduced the new Arrays class, with some
useful operations, for example:
–
–
–
–
–
static
static
static
static
static
void sort(Object[] a)
int binarySearch(Object[] a, Object key)
boolean equals(Object[] a, Object[] a2)
void fill(Object[] a, Object val)
void fill(Object[] a, Object val,
int from, int to)
– static List asList(Object[] a)
What to remember
• We haven’t really begun to study the
Collections framework yet, but—
– You should learn to use the Iterator interface
– You should examine and learn more about the
Arrays package
The End