14.1 The java.util Package
14.1.1 The java.util package Predefined utility classes to: Manage collections of objects Manage communication between objects Manipulate information within objects
14.2.1 Understanding collections Collections are objects that reference a group of objects Unlike arrays, collections can only reference the Object data type This means that any object can be stored in a collection, but casting is required to retrieve the object Collections can: Change size Provide sorting Support adding and deleting elements
14.2.2 Collection storage technologies Array: Fixed size, fast & efficient to access, difficult to modify Linked List: Elements have references to the preceeding and following element, easy to change, slow to search Tree: Easy to change, stores elements in order Hashtable: Uses an indexing key to identify elements. Elements are retrieved from the hashtable using the element’s key
14.2.4 Types of collections Collection List Set Map Simple container of unordered objects. Duplicates are permitted List Container of ordered elements. Duplicates are permitted Set Unordered collection of objects in which duplicates are not permitted Map Collection of key/value pairs. The key is used to index the element. Duplicate keys are not permitted
14.3.2 Collection interfaces Defines behavior of collection objects Hierarchy of six interfaces
14.3.3 Collection classes
14.3.4 Set objects HashSet implements the Set interface. Duplicates are not permitted TreeSet implements OrderedSet. Includes methods to take advantage of the ordering, eg: TreeSet.first() TreeSet.last() TreeSet.headSet() TreeSet.subSet()
14.3.5 List objects Vector ArrayList LinkedList A collection of objects. Implements the List interface. Grows as necessary. Retains the order in which the objects were added ArrayList Resizable, ordered array LinkedList Each element has a reference to the preceeding and following element
14.3.6 Map objects Use a unique key to reference the element Key determines where the element is stored The hashCode() method of the Object class can be overridden to provide unique keys
14.3.7 Iterators Provide a method for stepping through collections Iterating through a set is non-deterministic Use the ListIterator class for iterating through lists
14.3.8 Sorting and shuffling list objects Collection has methods for sorting Collection.sort() sorts the entire list or a subsection Collection.reverse() reverses the current list Shuffling moves elements within the list Collection.shuffle() Collection.shuffle(Random r)