Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Vectors and Collections and Hashtables - which to choose

 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 884
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could also use the comparator, its quite easy too.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic