In the early days, java platform had only two basic classes:
java.util.Vector
java.util.Hashtable
Prior to java 5.0, the Collections Framework had two major drawbacks.
The collections were untyped and worked only with anonymous Objects instead of real types like Dates and Strings.This meant that you had to perform a type cast every time you took an object out of a collection. In theory, this broke Java's compile-time type safety.
The second problem was that, for practical reasons, collections could work only with objects and not with primitive types. This meant that any time you wanted to put a number or other primitive type into a collection you had to store it in a wrapper class first and, of course, unpack it later upon retrieving it. The combination of these factors made code working with collections less readable and more dangerous to boot.
Two biggest new features:
Generic types make it possible to have truly typesafe collections.
The introduction of autoboxing and unboxing of primitive types means that you can generally treat objects and primitives as equals as far as collections are concerned.
Java.util pachage:
Collection interface represents a container that holds other objects.
Map interface represents a group of key/value pairs.
java.util package (J2SE 5.0)
General Rules
ArrayList and LinkedList provide the array and linked list implementations of the List interface described earlier. ArrayList is satisfactory for most purposes but use LinkedList when you plan to do a lot of insertions or deletions at various points in the list.
HashSet and HashMap provide a good hash map implementation of the Set and Map interfaces. The LinkedHashSet and LinkedHashMap implementations combine the hash algorithm with a linked list that maintains insertion order of the elements. (Note that these linked collections are ordered, but not sorted collections.)
TreeSet and TreeMap maintain sorted collections using a tree data structure. In the case of TreeMap, it is the key values that are sorted. The sorting is accomplished by a comparator object. We'll discuss sorting later in this chapter.
Queue is implemented both by LinkedList (which implements both List and Queue) and PriorityQueue. A PriorityQueue's prioritization comes from a sorting order determined by a comparator supplied with its constructor. Elements that sort "least" or "lowest" have the highest priority. The various implementations of BlockingQueue mirror these for concurrency-aware queues.
Finally, IdentityHashMap is an alternate type of HashMap that uses object identity instead of object equality to determine which keys match which objects. Normally any two objects that test equal with equals( ) operate as the same key in a Map. With IdentityHashMap, only the original object instance retrieves the element.
We should also mention three specialized collections that we'll talk about later: EnumSet and EnumMap are specifically designed to work with Java enumerations. WeakHashMap uses weak references to cooperate with Java garbage collection.
Synchronized and Unsynchronized Collections
To create a synchronized versioin of any collection:
A threadsafe List:
List list = new ArrayList();
List syncList = Collections.synchronizedList(list);
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment