Presentation is loading. Please wait.

Presentation is loading. Please wait.

 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/21) Collections Joel Adams and Jeremy Frens Calvin College.

Similar presentations


Presentation on theme: " 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/21) Collections Joel Adams and Jeremy Frens Calvin College."— Presentation transcript:

1  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/21) Collections Joel Adams and Jeremy Frens Calvin College

2  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(2/21) Review: The API We’ve seen that classes can be organized into hierarchies… We’ve also seen that the Java API provides documentation…Java API The API lets you easily find:  What methods are defined within a class  What methods are inherited from its superclass ……  What methods are inherited from the Object class. The API is an invaluable resource for Java programmers.

3  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(3/21) javadoc How is the Java API created and maintained? Java distributions include a utility called javadoc that automatically generates HTML API-style pages for a class. javadoc scans a.java file for special javadoc comments: /** */ and within those comments, scans for javadoc tags: @author Adams @version 1.0 @param Siemens @param Dematic @return Java @see Frens Running javadoc on that file will then generate documentation pages similar to those of the API.

4  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(4/21) Work through Part I of today’s exercise: Exercise: Part I Name.java PhoneNumber.java Person.java Employee.java 1. Examine the javadoc comments in the source files below: 2. Then run javadoc on them. 3. Then use your browser to examine the HTML files javadoc generated. 4. Make the suggested modifications to the source files, repeat steps 2 & 3 to see view the changes.

5  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(5/21) javadoc Tags As of version 1.4.2, javadoc looks for these tags: @author @docRoot @deprecated @exception @inheritDoc @link @linkPlain @param @return Javadoc also lets you create your own custom tags via doclets… @see @serial @serialData @serialField @since @throws @value @version @see http://java.sun.com/j2se/1.4.2/docs/tooldocs/javadoc/index.html

6  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(6/21) Interfaces In addition to the class, Java also supports the interface … A Java class can extend just 1 superclass (single inheritance) A Java class can implement multiple interfaces… So what is an interface?  A set of requirements that: Classes wanting to conform to the interface must implement; and Users of such classes can assume have been implemented.  Uses the keyword interface in place of class  Implementers use the keyword implements instead of extends  An alternative means of declaring a handle.

7  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(7/21) implements Comparable { public int compareTo(Object other) { if (other instanceof Employee) { Employee emp = (Employee) other; if (myID < emp.getID()) { return -1; } else if (myID > emp.getID()) { return 1; } else { return 0; } } else { return 1; } } Java provides a Comparable interface: Interface Example public interface Comparable { } public int compareTo(Object other); class Employee extends Person... } We could update our Employee class by adding:

8  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(8/21) Java’s java.util.Arrays class provides these methods:  sort(), that uses compareTo() to implement a refined version of the quicksort algorithm;  binarySearch(), that uses compareTo() to search a sorted array using the binary search algorithm;  equals(), that uses compareTo() to determine whether or not two arrays are equal; …… Interface Example (Ct’d) If a data structure stores Comparable references (handles), any item in it can be sent the compareTo() message… We’ll see more of interfaces later.

9  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(9/21) Work through Part II of today’s exercise… Exercise: Part II 1. Follow the directions to implement the Comparable interface on the class files for Part II. 2. Use the provided Driver to verify your code. 3. Compare the result of == and equals(). Make sure you understand why what you observe is happening.

10  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(10/21) Containers The package java.util has Java’s container classes…  Classes that "contain" (store references to) objects… AbstractCollectionAbstractMap AbstractListAbstractSetHashMapTreeMap HashSetTreeSetArrayList Abstract Sequential List LinkedList The “leaf” classes are called the concrete containers, because they are not abstract.

11  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(11/21) The Container Interfaces Collection is an interface that other interfaces extend: «interface» Collection «interface» Map «interface» Iterator «interface» List «interface» Set «interface» SortedSet «interface» SortedMap «interface» ListIterator You can add(anObject) to a Collection … You can put(aKey, anObject) and get(aKey) via a Map … You can visit the values in either using an Iterator …

12  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(12/21) Each of the Collections classes implements one or more interfaces…. The Collections Framework ClassImplements Interfaces LinkedListCloneable, Collection, List, Serializable ArrayListCloneable, Collection, List, RandomAccess, Serializable HashSetCloneable, Collection, Serializable, Set TreeSetCloneable, Collection, Serializable, Set, SortedSet HashMapCloneable, Map, Serializable TreeMapCloneable, Map, Serializable, SortedMap

13  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(13/21) Lists The ArrayList and LinkedList classes implement List We can send either object any message from the List API: List API List list1 = new ArrayList(), // empty; cap. 10 list2 = new LinkedList(); // empty list1.add( anObject ); // O(1) list2.add( anObject ); // O(1) list1.remove( someObject ); // O(n)+O(n) list2.remove( someObject ); // O(n)+O(1) int i = list1.indexOf( anObject ), // O(n) j = list2.indexOf( anotherObject ); // O(n) Object obj1 = list1.get(i), // O(1) obj2 = list2.get(j); // O(n)...

14  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(14/21) Iterators … can be used to traverse any of the containers: Iterator it = list2.iterator(); while ( it.hasNext() ) { Object obj = it.next(); // like i++ // do something with obj } Gotcha: remove() removes the last thing next() returned: Iterator it1 = list1.iterator(), it2 = list2.iterator(); it1.next(); it1.remove(); // O(n) it2.next(); it2.remove(); // O(1) The ListIterator interface specifies additional operations that are useful for manipulating LinkedList objects.

15  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(15/21) Sets If the order in which objects are stored doesn’t matter, then a Set may be the appropriate container… HashSet hs = new HashSet(); // uses hashCode() TreeSet ts = new TreeSet(); // uses compareTo() add(), remove() are used manipulate either kind of Set : hs.add( anObject );ts.add( anObject ); hs.remove( anObject );ts.remove( anObject ); A HashSet stores objects in a hash table (an array of linked lists), and tries to achieve O(1) access time. A TreeSet stores objects in a red-black tree (a self-balancing binary search tree), guaranteeing O(lg(n)) access time.

16  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(16/21) A Set requires an exact copy to do a lookup… A Map lets you lookup using partial information (a key). Maps A Map (aka a dictionary) stores (key, value) pairs: HashMap hm = new HashMap(); // uses hashing TreeMap tm = new TreeMap(); // uses r-b tree hm.put( new Integer(emp1.getID() ), emp1); tm.put( new Integer(emp2.getID() ), emp2); The value part can then be accessed using the key part: Employee e1 = (Employee)hm.get( new Integer(anID) ); Employee e2 = (Employee)tm.get( new Integer(anotherID) );... hm.remove( new Integer(anID) ); tm.remove( new Integer(anotherID) );

17  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(17/21) binarySearch(aList, key) // return index of key copy(srcList, destList) // copy srcList into destList fill(aList, obj) // replace all values with obj max(aCollection) // return maximum value min(aCollection) // return minimum value replaceAll(aList, old, new) // replace each instance of old with new reverse(aList) // reverse the order of values rotate(aList, i) // rotate values i positions shuffle(aList) // randomly reorder values sort(aList) // order values (using Comparable) swap(aList, i, j) // swap the values at indices i and j The Collections Class … provides methods that manipulate containers, including: and many more (see the Collections API)…Collections API

18  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(18/21) UML Notation … represents interface implementation with dashed arrows: AbstractCollection AbstractMapAbstractListAbstractSet HashMapTreeMapHashSetTreeSetArrayList Abstract Sequential List LinkedList Collection ListSet Map Sorted Map Sorted Set ** * * Random Access * == Cloneable, Serializeable

19  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(19/21) Java also provides “legacy” containers that have been a part of the language since before the collections framework: Like ArrayList, with all accesses synchronized (thread-safe, slow) Legacy Classes AbstractList List Vector Stack An array-based LIFO (push, pop, peek, …) Like HashMap, with all accesses synchronized (thread-safe, slow) Dictionary Map HashTable Properties A (String,String) map; save/load via a file; secondary defaults table Deprecated These do not reflect good object-oriented design…

20  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(20/21) Javadoc provides a convenient way to create hypertext documentation for a class and its methods. Java supports only single inheritance, but a class can implement multiple interfaces. The java.util package provides several container classes: ArrayList Summary LinkedListHashSetTreeSetHashMapTreeMap These can be used as is, or extended to build other traditional data structures (Stack, Queue, PriorityQueue, …) plus the legacy classes (some of which are deprecated).

21  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(21/21) Exercise: Part III Use the time remaining to complete Part III of today’s exercise (anything you do not finish is homework).


Download ppt " 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/21) Collections Joel Adams and Jeremy Frens Calvin College."

Similar presentations


Ads by Google