| Author |
Vectors and Collections and Hashtables - which to choose
|
Rachel Swailes
Ranch Hand
Joined: May 18, 2004
Posts: 434
|
|
I have this class public class Contacts{ String name, email; int referenceID etc... } These contacts get stored to a file and then read back into a Vector when I want to use them as I want to display them in a JList. Because I want the JList to display the Contacts alphabetically by name, I would like the Vector to sort alphabetically by name field. Is this possible without writing a full sort method myself? Or am I going to have to use another type of Collection. Any thoughts or suggestions would be great! Thanks, Rachel
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24061
|
|
|
Vector implements the java.util.List interface, and there are a couple of static sort() methods in java.util.Collections which will sort a List -- so you can just use those.
|
[Jess in Action][AskingGoodQuestions]
|
 |
Rachel Swailes
Ranch Hand
Joined: May 18, 2004
Posts: 434
|
|
Thanks. I've tried sorting a Vector when it just has the Contact's name in it. example myVec.elementAt(0) would be Rachel Sorting that kind of Vector is fine. When I try to sort a vector with the actual Contact in it example myVec.elementAt(0) would the the Contact Object with the name Rachel Applying Collections.sort(myVec) gives me ... Exception in thread "main" java.lang.ClassCastException: satstar.data.AddressCon tactPerson at java.util.Arrays.mergeSort(Arrays.java:1124) at java.util.Arrays.sort(Arrays.java:1074) at java.util.Collections.sort(Collections.java:110) at satstar.data.AddressContactHandler.getAllContactsAddressContactHandl er.java:109) at satstar.data.AddressContactHandler.main(AddressContactHandler.java:15 7) Why does this happen?
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24061
|
|
If you look at the Javadoc for the one-argument sort method, you see
All elements in the list must implement the Comparable interface.
This interface has a method compareTo() in it. String implements this interface already, but your Contact class doesn't. All you need to do is to declare that the class implements this interface, and then forward the implementation to the name member variable (given that you want to sort on names This method will throw ClassCastException if your Vector contains anything other than Contacts objects, and if name is ever null, it can throw NullPointerException, so you may want to improve it a little, but this gives you the idea.
|
 |
Rachel Swailes
Ranch Hand
Joined: May 18, 2004
Posts: 434
|
|
Thanks! I'll give that a try. I've never used the Comparable interface - something new is always fun! Thanks again. Rachel
|
 |
Chengwei Lee
Ranch Hand
Joined: Apr 02, 2004
Posts: 884
|
|
You could also use the comparator, its quite easy too.
|
SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
|
 |
 |
|
|
subject: Vectors and Collections and Hashtables - which to choose
|
|
|