University of Limerick1 Collections The Collection Framework
University of Limerick2 Objectives u This lecture will enable students to –compare an appreciate a second framework which has some variation to the Swing GUI framework –understand how the Collection Framework is used to representing and manipulating collections –Gain a better understanding of the usage of interface for hiding implementation detail
University of Limerick3 The Collection Framework u The collections framework is a unified architecture for representing and manipulating collections, allowing them to be manipulated independently of the details of their representation u It reduces programming effort while increasing performance u It allows for interoperability among unrelated APIs, reduces effort in designing and learning new APIs, and fosters software reuse u The framework is based on six collection interfaces. It includes implementations of these interfaces, and algorithms to manipulate them
University of Limerick4 Collections in practice u In practice a collection is an object that groups together other objects in a single unit u Collections are used to store, retrieve and manipulate data, and to transmit data from one method to another.
University of Limerick5 Collection Framework u All collection types work with objects rather than primitives u There is a general class called Collection, containing methods that any type of collection should have –inserting, retrieving, removing elements –iterating through a collection –converting to an array –getting the size
University of Limerick6 Collections u Three general types of collections represented as interfaces: –List –Map –Set u The type that is used depends on several factors –how the data is structured –what the needs of the programmer are
University of Limerick7 Collections Java provides two implementations of each interface –HashSet –TreeSet –ArrayList –LinkedList –HashMap –TreeMap u The implementation that is used depends mostly on performance issues
University of Limerick8 Interfaces V’s implementation
University of Limerick9 List u An ordered collection, also known as a sequence –elements indexed sequentially –elements can be retrieved by index –user has control over where to insert elements –can contain duplicate elements u Implementations include LinkedList, ArrayList, (Vector, Stack Java 1.1)
University of Limerick10 List Example // N.B. List has 2 possible implementations List aList = new ArrayList(); System.out.println("empty: " + List.isEmpty()); aList.add("It’s the"); aList.add(”end of the world"); aList.add(”as we know it!"); System.out.println(aList); Object o = aList.remove(aList.size() - 1); aList.add(”Collections are cool!!!!"); System.out.println(aList); aList.clear(); System.out.println("empty: " + aList.isEmpty());
University of Limerick11 Map u Maps keys to values u Keys must be unique –values do not have to be unique u Each key can map to at most a single value –if another value is added with that key, the previous one is overwritten u Implementations include Hashtable, HashMap, TreeMap
University of Limerick12 Map Example // N.B. Map has 2 possible implementations Map tm = new TreeMap(); //first param is key, second is element tm.put(new Integer(8), "eight"); tm.put(new Integer(3), "three"); tm.put(new Integer(20), "twenty"); tm.put(new Integer(5), "five"); tm.put(new Integer(1), "eight"); tm.put(new Integer(20), "another twenty"); System.out.println(tm);
University of Limerick13 Set u A collection that can’t contain duplicate elements –can contain at most one null element –not ordered, no guarantees that elements will be retrieved in the same order added u Implementations include HashSet and TreeSet
University of Limerick14 Set Example // N.B. Set has 2 possible implementations Set string = new HashSet(); hs.add("I"); hs.add("think"); hs.add("therefore"); hs.add("IBM"); System.out.println(string);
University of Limerick15 Questions u Given the choices List, Map or Set, determine which collection type would be appropriate for each of the following examples: –a deck of cards –a dictionary –a line of customers at a bakery where each has to take a number
University of Limerick16 Iterator u The collections use Iterators –similar to Enumeration, but: »method names improved »allows removal of elements u Each collection type has a method called iterator() which returns an Iterator for that collection
University of Limerick17 Iterator u Methods: –boolean hasNext() »returns true if there are more elements –Object next() »returns the next element –void remove() »removes the last element returned by the iterator »must have called next() »can only call once per call to next()
University of Limerick18 Iterator Example System.out.println("\nSize: " + aList.size()); Iterator iter = aList.iterator(); System.out.println(iter.next()); iter.remove(); System.out.println(iter.next()); System.out.println("\nSize: " + aList.size()); System.out.println(aList);
University of Limerick19 List Iterator u List collection types have an additional iterator called a ListIterator –has all functionality of a normal Iterator –plus the ability to move backward in a list –also, can add new elements
University of Limerick20 List Iterator u Additional methods: –Object previous() »returns the previous element –boolean hasPrevious() –returns true if there are more elements (in reverse) –void add(Object o) »inserts an element before the element that would be returned by next or and after the element that would be returned by previous
University of Limerick21 ListIterator Example System.out.println("\nSize: " + aList.size()); ListIterator iter = aList.listIterator(); System.out.println("Next: " + iter.next()); iter.remove(); System.out.println("\nSize: " + aList.size()); System.out.println("Next: " + iter.next()); System.out.println("Previous: " + iter.previous()); iter.add(" to be "); System.out.println("\nSize: " + aList.size()); System.out.println(aList);
University of Limerick22 Additional Collection Utilities u The Collections class provides many additional methods for manipulating collection objects u This class is found in the java.util package
University of Limerick23 Collections class u Selected methods: –void sort(List) »sorts a list –void shuffle(List) »randomly shuffles a list –void reverse(List) »reverses all the elements in a list –Object min(Collection), Object max(Collection) »returns the minimum or maximum element in a collection
University of Limerick24 Sorting u How do collections know how to sort elements? –many classes, such as String or Integer, include a method called compareTo() that provides default comparison behavior »numeric wrapper objects are sorted by primitive values »Strings sorted in ascending lexicographic order (compared character by character)
University of Limerick25 Sorting u To sort objects of classes that you create –determine rules for how those objects compare with one another –write a custom compareTo() method »for example, a list of Book objects could be sorted by Author or Title