Exam Objectives of Java Collections Framework for Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035)- Make appropriate selection of collection classes/interfaces to suit specified behavior requirements.
- Distinguish between correct and incorrect implementations of hashcode methods.
Terms you'll need to understand of Java Collections Framework for Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035)- Enumeration
- Collection
- Set
- List
- Map
- Iterator
- Sorted
- Ordered
Java VectorThe Vector class holds an array of Object references that can automatically grow in size as needed. Because all Java classes inherit from Object, you can add any reference to a Vector. All methods that retrieve an object are defined as returning Object; therefore, you must use a cast to any other reference type, as in the following: int[] xx = ( int[] )myVector.firstElement() ; Various methods provide for retrieving objects by index and for searching. The internal storage order retains the order in which objects are added. For JDK 1.2 and subsequent releases of Java, several changes were made to the Vector class. These changes do not break existing code but add functions to make Vector compatible with the new Collections API. In particular, Vector now implements the List interface (discussed in the section "The List Interface" in this chapter) and has an AbstractList parent class. The Hashtable ObjectThe Hashtable classis designed to store key/value pairs. Note the lowercase letter "t" in the name Hashtable. Like a key in a database, a key in a Hashtable is a unique value that can be used to reference an element. The elements in a Hashtable are not ordered. Hashtable implements the Map interface. Hashtable objects are used extensively in Java to associate objects with names efficiently. Although the key objects are frequently strings, any non-null object can be a key. For example, this code fragment creates a Hashtable and stores a Uniform Resource Locator (URL) string with a short string as a key: 1. Hashtable pageTable = new Hashtable() ; 2. String key = "que" ; 3. String page = "http://www.ebooklobby.com" ; 4. pageTable.put( key, page ) ; As with Vector, the methods that retrieve objects from Hashtables are defined as returning an Object reference. You have to provide a specific cast that would retrieve the string stored in the Hashtable under the "que" key, as in the following line: String val = (String)pageTable.get("que") ; A Hashtable can store only one "value" per "key"—if you add a new reference with a duplicate key, the old reference is dropped. The contains and containsKey methods let you discover whether a given object or key is present. Internally, Hashtables store object references according to hashcodes, so if you get an Enumeration of all of the stored "value" or "key" objects, the order is not predictable. Collections API InterfacesThe basis for the new Collections API is a set of six interfaces that define the basic types. These are supplemented by a set of classes, such as AbstractCollection, that implement the interfaces to provide root classes suitable for programmers to extend. The Collection InterfaceThis interface is the root of the Collections API hierarchy and defines the basic functions that all Collection classes must provide. The AbstractCollection class provides a skeleton set of the interface methods to serve as a starting point for concrete classes. Among other things, the Collection interface requires methods for adding, removing, locating, and selecting objects. Classes that implement Collection may contain duplicates and null values. The Set InterfaceThe key feature of the Set interface is that no duplicates are allowed. Only one element can have the null value. A class that implements this interface holds objects in an order that is fixed when the set is created. The Set interface extends the Collection interface, so any class that implements Set also implements Collection. The List InterfaceUnlike the Set interface, the List interface allows duplicate elements. A class that implements this interface holds an ordered collection of objects. The Vector class as revised for SDK 1.2 implements the List interface. The List interface extends the Collection interface. The Map InterfaceA class that implements Map associates "key" objects with "value" objects. Each key must be unique. The Hashtable class as modified for SDK 1.2 implements the Map interface. The actual order of the keys and values is not specified, and cannot be guaranteed. The SortedSet InterfaceThis interface extends the Set interface by requiring that the contents be sorted according to the natural ordering of its elements. For the purpose of the exam, you do not have to be concerned about the meaning of "natural ordering," but as an example, strings are sorted alphabetically and Integer objects are sorted numerically. Objects inserted into a SortedSet must implement the Comparator interface or be able to be compared by an external Comparator object. The SortedMap InterfaceThis interface extends the Map interface by requiring that the collection be ordered according to key values. The Iterator InterfaceThis interface replaces the Enumeration interface that was used in Java 1. An object that implements Iterator lets the programmer examine every object in a Collection in a fixed order. All classes that implement Collection must have an iterator method that returns an Iterator object. The main differences between Iterator and Enumeration are that Iterator has more compact names, and contains a method that allows the removal of an object from the underlying Collection
Question 1 ( Java Collections Framework tutorial for Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035))Given: 1. package foo; 2. 3. import java.util.Vector; 4. 5. private class MyVector extends Vector { 6. int i = 1; 7. public MyVector() { 8. i = 2; 9. } 10. } 11. 12. public class MyNewVector extends MyVector { 13. public MyNewVector() { 14. i = 4; 15. } 16. public static void main(String args[]) { 17. MyVector v = new MyNewVector(); 18. } 19. } What is the result? A. Compilation succeeds. B. Compilation fails because of an error at line 5. C. Compilation fails because of an error at line 6. D. Compilation fails because of an error at line 14. E. Compilation fails because of an error at line 17. Answer: B
Question 2 ( Java Collections Framework tutorial for Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035)) Given: 1. package foo; 2. 3. import java.util.Vector; 4. 5. protected class MyVector Vector { 6. init i = 1; 7. public MyVector() { 8. i = 2; 9. } 10. } 11. 12. public class MyNewVector extends MyVector { 13. public MyNewVector() { 14. i = 4; 15. } 16. public static void main(String args[]) { 17. MyVector v = new MyNewVector(); 18. } 19. } What is the result? A. Compilation succeeds. B. Compilation fails because of an error at line 5. C. Compilation fails because of an error at line 6. D. Compilation fails because of an error at line 14. E. Compilation fails because of an error at line 17. Answer: B
Question 3 ( Java Collections Framework tutorial for Sun Certified Programmer for Java 2 Platform 1.4 (CX-310-035)) Which of the following statements about the java.util.Vector and java.util.Hashtable classes are correct? [Check all correct answers.] A. A Vector can hold object references or primitive values. B. A Vector maintains object references in the order they were added. C. A Hashtable requires string objects as keys. D. A Hashtable maintains object references in the order they were added. E. Both Vector and Hashtable use synchronized methods to avoid problems due to more than one thread trying to access the same collection.
Answers B and E are correct. Answer B is correct because Vector objects maintain the order in which objects are added. Answer E is correct because this is one of the major distinctions between the original and new Collections API classes. Answer A is incorrect because only object references can be held in a vector. If you need to store primitive values, you have to use the wrapper classes to create objects. Answer C is incorrect because although keys are frequently strings, they can be any object. Answer D is incorrect because the order of items in a hashtable is not predictable.
|