Start of Tutorial > Start of Trail |
Search
Feedback Form |
Problem: When I try to useArrays.sort()
orCollections.sort()
I get aClassCastException
.
- The problem is that you're using the 1.1 backport. To sort objects without providing an explicit comparator, the objects must be "mutually comparable." In 1.2,
String
(and many other JDK classes) have been retrofitted to implementComparable
. In 1.1, you have to provide an explicitString
comparator. Since this is your lucky day, I'll provide you with one.With this in your program, merely replace the line:static final Comparator stringCmp = new Comparator() { public int compare(Object o1, Object o2) { String s1 = (String)o1; String s2 = (String)o2; int len1 = s1.length(); int len2 = s2.length(); for (int i=0, n=Math.min(len1, len2); i<n; i++) { char c1 = s1.charAt(i); char c2 = s2.charAt(i); if (c1 != c2) return c1 - c2; } return len1 - len2; } };With:Collections.sort(l);and you're in business!Collections.sort(l, stringCmp);
Note: A better solution might be to download the Java 2 Platform (formerly known as JDK 1.2) to take advantage of the implementation ofComparable
.
Problem: What collection interface behaves like the legacyVector
class? The feature I'm most interested in is aVector
's ability to grow dynamically in size.
- All of the new general-purpose collection implementations have the ability to grow dynamically in size. The new interface that models
Vector
's behavior isList
. The two general purpose implementations ofList
areArrayList
andLinkedList
. The one whose performance properties are similar toVector
's isArrayList
. All other things being equal,ArrayList
is the preferredList
implementation. So, to recap, the Java 2 replacement for the legacy:is:Vector a = new Vector();List a = new ArrayList();
Problem: AreHashtables
synchronized in Java 2 (formerly known as JDK 1.2)?
- Yes,
Hashtables
are still synchronized in Java 2. The fact that the new implementations in Java 2 are unsynchronized represents a break with the past:Vector
andHashtable
were synchronized in versions of the JDK prior to 1.2. In other words, legacy collections (likeVector
andHashtable
) are synchronized, whereas new collections (likeArrayList
andHashMap
) are unsynchronized, and must be "wrapped" viaCollections.SynchronizedList
orCollections.synchronizedMap
if synchronization is desired.
Problem: How do the Java collections compare to STL in C++?
- While the new Java collections is somewhat similar to STL, it's also markedly different: Iterators and iterator-pairs play a central role in STL which they do not play in our framework; collection interfaces play a central role in our framework which they don't play in STL.
We designed Java collections from a "clean sheet of paper" and chose names for clarity, brevity, and similarity with preexisting Java APIs. Happily, most STL users have had little difficulty adapting to the framework's nomenclature.
Start of Tutorial > Start of Trail |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.