J2SE Collections


The collections received some welcome new features; two new classes are discussed in the following sections.

The LinkedHashMap Class

The most useful new feature is the LinkedHashMap class, which provides an insertion-ordered Map that remembers in which order you added items to it. Normally, a Map keeps its keys in random order because that is how it stores them. However, sometimes (as in first-come, first-serve situations) you need the insertion order. Previously, you had to manually do this. Now the LinkedHashMap class (and the similar LinkedHashSet class) uses an internal doubly linked list to keep the insertion order. Remarkably, it is nearly as fast as HashMap .

For the assignment, you might want to keep track of user actions. It is extra work, but you could add this functionality with little effort and low risk. Perhaps you want users to be able to review the last reservations they made. You can use the seat number as the key and the aisle as the value, for example. Listing 8.3 demonstrates how to use the LinkedHashMap class.

Listing 8.3 Using the LinkedHashMap Class
 import java.util.LinkedHashMap; import java.util.HashMap; import java.util.Map; import java.util.Iterator; public class MyLinkedHashMap {     public static void main(String[] args)     {         //Map map = new LinkedHashMap();         Map map = new HashMap();         map.put("A", "first");         map.put("B", "second");         map.put("C", "third");         map.put("D", "fourth");         map.put("E", "fifth");         map.put("F", "sixth");         map.put("G", "seventh");         map.put("H", "eighth");         map.put("I", "ninth");         map.put("J", "tenth");         for (Iterator list=map.keySet().iterator(); list.hasNext(); )         {             Object key = list.next();             String value = (String) map.get(key);             System.out.print(value + ", ");         }     } } 

You can play around with the key-value pairs in the code. Listing 8.3 on the CD has a few variations of the key-value pairs mentioned here and below to demonstrate what happens when the same key is added to a Map type object repeatedly. Listing 8.3, using a regular HashMap , produces the following random ordered output:

 ninth, fourth, first, sixth, eighth, tenth, third, second, seventh, fifth, 

Compare that with the following insertion-order output, which is produced by using LinkedHashMap instead of HashMap :

 first, second, third, fourth, fifth, sixth, seventh, eighth, ninth, tenth, 

The LinkedHashMap class kept the insertion order. Instead of adding items to the Map object in the order shown in Listing 8.3, you might use the following:

 map.put("A", "first"); map.put("B", "second"); map.put("C", "third"); map.put("D", "fourth"); map.put("E", "fifth"); map.put("A", "sixth"); map.put("A", "seventh"); map.put("A", "eighth"); map.put("A", "ninth"); map.put("A", "tenth"); 

You get the following output using LinkedHashMap :

 tenth, second, third, fourth, fifth, 

As you can see, the LinkedHashMap class keeps the insertion order of the keys. Even though the last insertion had the "A" key, LinkedHashMap returns the "tenth" value in the first position because that was when the key was added. Of course, the original value was replaced by the last value of that key.

There is one more interesting use for LinkedHashMap . You can make a copy of another Map with it. You could do that before by using a Map constructor; however, the two copies had independent orders. Now, with a LinkedHashMap , you can copy a Map and its order, which was difficult to do previously. If you want to take a snapshot of a Map 's order (in which map is the Map instance you'd like to copy), you can do this:

 Map insertionOrderCopy = new LinkedHashMap(map); 

The IdentityHashMap Class

The IdentityHashMap class differs from the other Map classes in one important aspect. Internally, a Map tests for equality based on the Object.equals() method. The IdentityHashMap class doesn't use this method, however. Instead, it checks whether two keys are referencing the same object, like so:

 key1==key2 

If you revise Listing 8.3 to include the following code (you'll find the source code file with this revision):

 Map map = new LinkedHashMap(); String a = new String("A"); String b = new String("A"); String c = new String("A"); String d = new String("A"); String e = new String("A"); String f = new String("A"); map.put(a, "first"); map.put(b, "second"); map.put(c, "third"); map.put(d, "fourth"); map.put(e, "fifth"); map.put(f, "sixth"); map.put("G", "seventh"); map.put("H", "eighth"); map.put("I", "ninth"); map.put("J", "tenth"); 

Listing 8.3 generates the following output:

 sixth, seventh, eighth, ninth, tenth, 

However, using the same revision but replacing the LinkedHashMap class with an IdentityHashMap class, you get this:

 seventh, second, tenth, third, fifth, sixth, fourth, eighth, first, ninth, 

Notice that all insertions were stored, even though the first six had the same letter. Although the Strings a , b , c , d , e , and f contained the same letter "A" , each is pointing to a unique Object , so they were all considered unequal in the IndentityHashMap class. Conversely, the LinkedHashMap class considered them duplicates.



JavaT 2 Developer Exam CramT 2 (Exam CX-310-252A and CX-310-027)
JavaT 2 Developer Exam CramT 2 (Exam CX-310-252A and CX-310-027)
ISBN: N/A
EAN: N/A
Year: 2003
Pages: 187

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