The JavaTM Tutorial

| Start of Tutorial > Start of Trail | Search Feedback Form

Trail: Collections

## Lesson: Solving Common Collections Problems

> Problem: When I try to use `Arrays.sort()` or `Collections.sort()` I get a `ClassCastException`.
> 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 implement `Comparable`. In 1.1, you have to provide an explicit `String` comparator. Since this is your lucky day, I'll provide you with one:

> >         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 this in your program, merely replace the line:

> >             Collections.sort(l);

> With:

> >             Collections.sort(l, stringCmp);

> and you're in business!

> > Note: A better solution might be to download the Java 2 Platform (formerly known as JDK 1.2) to take advantage of the implementation of `Comparable`.

> Problem: What collection interface behaves like the legacy `Vector` class? The feature I'm most interested in is a `Vector`'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 is `List`. The two general purpose implementations of `List` are `ArrayList` and `LinkedList`. The one whose performance properties are similar to `Vector`'s is `ArrayList`. All other things being equal, `ArrayList` is the preferred `List` implementation. So, to recap, the Java 2 replacement for the legacy:

>   >         Vector a = new Vector();

>   is:

>   >         List a = new ArrayList();

> Problem: Are `Hashtables` 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` and `Hashtable` were synchronized in versions of the JDK prior to 1.2. In other words, legacy collections (like `Vector` and `Hashtable`) are synchronized, whereas new collections (like `ArrayList` and `HashMap`) are unsynchronized, and must be "wrapped" via `Collections.SynchronizedList` or `Collections.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-2004 Sun Microsystems, Inc. All rights reserved.