24.22. Programming Exercises

 
[Page 738 ( continued )]

Chapter Summary

  • The Java Collections Framework supports three types of collections: sets , lists , and maps . They are defined in the interfaces Set , List , and Map . A set stores a group of nonduplicate elements. A list stores an ordered collection of elements. A map stores a group of objects, each of which is associated with a key.

  • A set stores nonduplicate elements. To allow duplicate elements to be stored in a collection, you need to use a list. A list can not only store duplicate elements, it also allows the user to specify where they are stored. The user can access elements by an index.

  • Three types of sets are supported: HashSet , LinkedHashSet , and TreeSet . HashSet stores elements in an unpredictable order. LinkedHashSet stores elements in the order they were inserted. TreeSet stores elements sorted. All the methods in HashSet , LinkedHashSet , and TreeSet are inherited from the Collection interface.


    [Page 739]
  • Two types of lists are supported: ArrayList and LinkedList . ArrayList is a resizable-array implementation of the List interface. All the methods in ArrayList are defined in List . LinkedList is a linked list implementation of the List interface. In addition to implementing the List interface, this class provides the methods for retrieving, inserting, and removing elements from both ends of the list.

  • The Vector class implements the List interface. In Java 2, Vector is the same as ArrayList , except that it contains synchronized methods for accessing and modifying the vector. The Stack class extends the Vector class and provides several methods for manipulating the stack.

  • The Collection interface represents a collection of elements stored in a set or a list. The Map interface maps keys to the elements. The keys are like indexes. In List , the indexes are integers. In Map , the keys can be any objects. A map cannot contain duplicate keys. Each key can map to at most one value. The Map interface provides the methods for querying, updating, and obtaining a collection of values and a set of keys.

  • The Queue interface represents a queue. The PriorityQueue class implements Queue for a priority queue.

  • Three types of maps are supported: HashMap , LinkedHashMap , and TreeMap . HashMap is efficient for locating a value, inserting a mapping, and deleting a mapping. LinkedHashMap supports ordering of the entries in the map. The entries in a HashMap are not ordered, but the entries in a LinkedHashMap can be retrieved either in the order in which they were inserted into the map (known as the insertion order ) or in the order in which they were last accessed, from least recently accessed to most recently ( access order ). TreeMap is efficient for traversing the keys in a sorted order. The keys can be sorted using the Comparable interface or the Comparator interface.

 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net