Hi guys i've written a hash table as a collection for a program the key is a string, show below:

private Hashtable<String, Planes> planesFlying = new Hashtable<String, Planes>();

How can i write a comparator to sort this into alphabetical order? i've tried a lot. Do you need to overide equals and do the overide hashcode ?

your should create your comparator class by implementing Comparator Interface.

Once you do it, then you can create your hashtable as follows:

private Hashtable<String, Planes> planesFlying = new Hashtable<String, Planes>(new YourMapComparator());

something similar to this.

your should create your comparator class by implementing Comparator Interface.

Once you do it, then you can create your hashtable as follows:

private Hashtable<String, Planes> planesFlying = new Hashtable<String, Planes>(new YourMapComparator());

something similar to this.

That is COMPLETELY gangsta1903 wrong. If you don't know java don't give wrong advices. Where did you see that constructor:

private Hashtable<String, Planes> planesFlying = new Hashtable<String, Planes>(new YourMapComparator());

Here is the solution.
Do you want to sort this out based on the key (String) or the values (Planes)?

If you want it based on the key then look at the API for the Hashtable class. There should be a method that lets you take the keys as a collection (Set<K> keySet() ). Use the toArray method of that Set to create an array. Then you can use any algorithm you want or you can use the sort method of the java.util.Arrays. You don't need to implement the Comparable interface since the String class already does that.
After the array with the keys is sorted you can loop that array and use each key to get each plane from the Hashtable.

I would like also to add that it is not a good idea to to use Hashtable if you want them sorted. You'd better use a Vector to put your Planes and have the "key" as part of the Plane's class attribute. If you do that you will have to implement the Comparable class. Check the derscription of this:
Arrays.sort(Object [] a): Arrays.sort(Object [] a) as well as the API for the Comparable interface.

Is also good when you add Planes to the vector to have that vector private in a seperate class and have an add method:

public class ListOfPlanes {
  private Vector<Plane> planes = new Vector<Plane>();

  public void add(Plane pl) {
      planes.add(pl);
  }

  public Plane get(int i) {
      return planes.get(i);
  }
}

In the add nethod you can add code that loops the vector and compare each plane with the one being added. Then you can add the new plane at the right position so the Vector will always be sorted and you will not have to call the sort method of the Arrays.

If this doesn't cover you be more specific on what exactly you want to do

Thanks so much !

Another thing i dont get is that the hashtable stores plane objects (dont ask) i wrote some code that iterates through the table, how can i access some of the variables for the plane object? i have get methods for it. Sorry this key thing confuses me.

Edited 6 Years Ago by beforetheyknew: n/a

Thanks so much !

Another thing i dont get is that the hashtable stores plane objects (dont ask) i wrote some code that iterates through the table, how can i access some of the variables for the plane object? i have get methods for it. Sorry this key thing confuses me.

Plane plane = new Plane();
Hashtable<String, Plane> table = new Hashtable<String, Plane>();
table.put("keyPlane1", plane);
....

Plane p = table.get("keyPlane1");
p.getName();
p.getSeats();
p.getNumberOfEngines();
...


table.get("keyPlane1").setName("Boing 777");

In case the you are using a key that doesn't exist in the table then you should always do this just in case:

Hashtable<String, Plane> table = new Hashtable<String, Plane>();
table.put("keyPlane1", plane);

Plane p = table.get("keyPlane2");
if (p==null) {
  System.out.println("keyPlane2 doesn't exist");
} else {
   system.out.println("Plane found: " + p);
}

Look at the API for Hashtable

sorry for my post. it was quick and careless.
in fact, I meant this assuming using HashTable is not mandatory :

Map<String, Planes> map = new TreeMap<String, Planes>(new MyComparator());
	
	class MyComparator implements Comparator<String> {
		@Override
		public int compare(String o1, String o2) {
			return o1.compareTo(o2);
		}
	}

but, I dont get why you post such an offensive message. I may get wrong. What makes you to judge me?

This question has already been answered. Start a new discussion instead.