The Collections Framework A Brief Introduction Collections A
The Collections Framework A Brief Introduction
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 – Sorted. Set: 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 – Sorted. Map: like a Map, but order is important
The Collections framework Collection Set Map List Sorted. Map Sorted. Set • There are two groups: Collections of single elements, and Maps containing key/value pairs • These are all interfaces, but there are classes implementing each interface
Some implementations Collection Map Set Hash. Set Sorted. Set Tree. Set List Array. List Hashtable Linked. List Vector Hash. Map Sorted. Map Tree. Map Stack • Each class (in green) implements one or more interfaces (in white), hence implements all the methods declared in those interfaces • We will look at the interface methods
Collections are ADTs • The interfaces of the Collections framework describe the functionality of a data structure but not its implementation • 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
The Collection interface • Some simple Collection methods: – – – int size( ) boolean is. Empty( ) boolean contains(Object e) boolean add(Object e)* boolean remove(Object e)* Iterator iterator( ) • Some “bulk” Collection methods: – – – boolean contains. All(Collection c) boolean add. All(Collection c)* boolean remove. All(Collection c)* boolean retain. All(Collection c)* void clear( ) * Returns true if the Collection was changed, false otherwise
The Set interface • A Set is a Collection that does not contain duplicate (“equal”) elements – “Equal” is defined by whatever equals(Object) method applies to the elements of the collection – You need to be sure you have a working equals method – For a Hash. Set, you also need a working hash. Code method • The methods of Set are the same as those of Collection , but their effects may vary – Example: If you add an object to a set, and the set already contains that object (as defined by equals ), it will not be added
The Sorted. Set interface • A Sorted. Set keeps its elements in increasing order • Therefore, it must be possible to sort the elements! • This means: – The elements must be objects of a type that implement the Comparable interface (alternatively, you need to supply a Comparator when you create the Sorted. Set) – Elements must be mutually comparable (e. g. you can’t compare a String to a Button) – The ordering must be consistent with equals • Additional methods include first() and last()
The List interface • List implements all the methods defined by Collection, and adds a few of its own: – – – – Object get(int index) Object set(int index, Object element) void add(int index, Object element) Object remove(int index) boolean add. All(int index, Collection c) int index. Of(Object o) int last. Index. Of(Object o) • In addition, List supplies a List. Iterator that can iterate through the elements forwards or backwards
The Map interface • A Map is an object that maps keys to values • A map cannot contain duplicate keys – Depending on the implementation, null may or may not be an allowable key • Each key can map to at most one value • Examples: dictionary, phone book, etc.
Map: Basic operations • Object put(Object key, Object value) – Returns the old value for this key, or null if no old value • • • Object get(Object key) Object remove(Object key) boolean contains. Key(Object key) boolean contains. Value(Object value) int size( ) boolean is. Empty( )
Additional Map methods • void put. All(Map t) – Copies one Map into another (adding to whatever is already there) – Example: new. Map. put. All(old. Map) • void clear() – Example: old. Map. clear() • public Set key. Set( ) • public Collection values( )
A simple map: Hashtable • To create a Hashtable, use: import java. util. *; Hashtable = new Hashtable(); • To put things into a Hashtable, use: table. put(key, value); • To retrieve a value from a Hashtable, use: value = (type)table. get(key);
The Sorted. Map interface • A hash table keeps elements in an (apparently) random order • Sometimes you want the keys of a map to be in sorted order (e. g. phone book, dictionary) • A map can be implemented with a hash table, but it doesn’t have to be • The Sorted. Map interface implements the Map interface and provides additional methods • For efficiency, you want an implementation that keeps its elements in some kind of order
Requirements for Sorted. Map • A Sorted. Map keeps its elements in the order of increasing key values • Therefore, it must be possible to sort the keys! • This means: – The keys must be objects of a type that implement the Comparable interface (or the Sorted. Map must be created with a Comparator) – Keys must be mutually comparable (e. g. you can’t compare a String to a Button) – The ordering must be consistent with equals
Some Sorted. Map Methods • Comparator comparator() – Returns the comparator associated with this sorted map, or null if it uses its keys' natural ordering. • Object first. Key() – Returns the first (lowest) key currently in this sorted map. • Object last. Key() – Returns the last (highest) key currently in this sorted map. • There are some other methods, but they are not particularly useful • What is more important than these methods is that the Iterator for a Sorted. Map returns elements in sorted order
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 iterator { // java. lang. util boolean has. Next(); // 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. has. Next()) { System. out. println(iter. next()); } Results: one two three
Using a Tree. Set Collection numerals = new Tree. Set(); numerals. add("one"); numerals. add("two"); numerals. add("three"); Iterator iter = numerals. iterator(); while (iter. has. Next()) { System. out. println(iter. next()); } Results: one three two
Constructors • Although constructors cannot be defined in an interface, every Java-supplied class that implements Collection has a constructor that takes any other Collection as an argument – Example: Vector my. Vector = new Vector(my. Tree. Set); • Similarly, most classes that implement the Map interface have a constructor that takes any other Map as an argument – Example: Tree. Map my. Map = new Tree. Map(my. Hashtable); • Thus, it is very easy to convert from one kind of Collection or Map to another
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 void sort(Object[] a) int binary. Search(Object[] a, Object key) boolean equals(Object[] a, Object[] a 2) void fill(Object[] a, Object val, int from, int to) – static List as. List(Object[] a) • Although I’ve shown these methods for arrays of Objects, there are corresponding methods for arrays of each of the primitive types
The End
- Slides: 24