Java Reference
In-Depth Information
The collections classes use the equals method to determine the equality of two objects.
The Comparable interface declares the compareTo method. Objects that implement
Comparable are said to have a natural ordering, and the compareTo method is consistent
with equals if it returns 0 for two objects that are equal in terms of the equals method.
The Comparator interface declares the compare method and provides a mechanism for
ordering objects in whatever order you defi ne.
The generics feature, new to J2SE 5.0, provides support for parameterized data types.
We discussed the details of using generics with the Collections Framework, along with
defi ning your own classes, interfaces, and methods that take advantage of generics.
Generics can use the wildcard ? to represent any generic type. Use the extends keyword to
defi ne an upper bound for a generic type and super to defi ne a lower bound.
The Collections class contains sort methods for sorting lists and binarySearch
methods for searching lists. The Arrays class contains sort methods for sorting arrays and
binarySearch methods for searching arrays.
Be sure to test your knowledge of collections and generics by answering the Review
Questions that follow the Exam Essentials. Attempt to answer the questions without
looking back at the pages of this chapter. Make sure you have a good understanding of the
following Exam Essentials before you attempt the Review Questions, and good luck!
Exam Essentials
Understand the different types of collection data structures. Know the difference between
lists, sets, queues, and maps. Be able to recognize which type of collection to use given a
specifi c scenario. A list is an ordered collection of elements that allows duplicate entries, a
set is a collection that does not allow duplicate entries, a queue is a collection that orders
its elements in a specifi c order for processing, and a map is a collection that maps keys to
values, with no duplicate keys allowed.
Understand the generics syntax. Be sure you understand how to instantiate an object
that uses generics, as well as invoke a generic method. Generics provide compile-time
type safety for collections by allowing your Collection objects to specify what types they
contain.
Understand generics and polymorphism. You need to be able to recognize proper and
improper use of polymorphic references and generics, especially with wildcards and upper
and lower bounds. A question mark ( ? ) represents an unknown generic, the extends
keyword creates an upper-bound generic, and the super keyword creates a lower-bound
generic.
Be familiar with the various collections class. You are not expected to know all the
details of all the collections classes, but you should be able to understand code that uses
ArrayList , Vector , Stack , LinkedList , HashSet , TreeSet , HashMap , and TreeMap .
Search WWH ::




Custom Search