how to compeer Arraylist contains hashtables by key name

Hello all i have Arraylist that contains hashtables how can i sort the ArrayList by the hashtables key?
[124 byte] By [Meirya] at [2007-11-26 21:51:18]
# 1

You need to provide a [url=http://java.sun.com/j2se/1.4.2/docs/api/java/util/Comparator.html]Comparator[/url] for comparing hash tables, based on your criteria.

However, your order criteria are unclear: by the hashtables key

(note that a hash table contains a set of keys, not just one key.)

So you have to clearly define what would make a given hash table being before or after another one.

TimTheEnchantora at 2007-7-10 3:44:31 > top of Java-index,Java Essentials,Java Programming...
# 2
hello in my case is list that holds hashtables in my hash table i will have only one key and value this is somekind of data structure of the application that i need to add sorting capabilities
Meirya at 2007-7-10 3:44:31 > top of Java-index,Java Essentials,Java Programming...
# 3

Hashtable is overkill in that case, IMHO. Why not using your own class, which would have key and value members (and could implement Comparable, btw.)

Anyway, if you still want/have to keep using hashtable, then your Comparator implementation should compare the only key of the Hashtables, something like: public int compare(Object o1, Object o2) {

return ((String)((Hashtable)o1).keySet().iterator().next()).compareTo(((Hashtable)o2).keySet().iterator().next());

}

(Fastidious, isn't it ?)

Then, use this comparator to [url=http://java.sun.com/j2se/1.4.2/docs/api/java/util/Collections.html#sort(java.util.List, java.util.Comparator)]sort[/url] your list.

TimTheEnchantora at 2007-7-10 3:44:31 > top of Java-index,Java Essentials,Java Programming...
# 4

i have to use hashtable this is applications resons

here is what i have :

my data struct :

public class Tuple extends Hashtable{

@Override

public synchronized Object get(Object key) {

// TODO Auto-generated method stub

return super.get(key);

}

@Override

public synchronized Object put(Object key, Object value) {

// TODO Auto-generated method stub

return super.put(key, value);

}

public String getKeybyIndex(int i){

Enumeration e = super.keys();

String key = null;

int j = 1 ;

while(e.hasMoreElements()){

key = (String)e.nextElement();

if(i == j )

break;

++j;

}

return key;

}

}

public class TupleList {

ArrayList<Tuple> list = new ArrayList();

public void addTuple(Tuple t){

list.add(t);

}

public Tuple getTuple(int i){

return list.get(i);

}

}

and my comperetor class and main class

class Comp implements Comparator {

public int compare (Object o1, Object o2) {

String s1 = ((Tuple)o1).getKeybyIndex(1).toString().toUpperCase() ;

String s2 = ((Tuple)o2).getKeybyIndex(1).toString().toUpperCase() ;

return s1.compareTo ( s2 ) ;

}

}

public class Main {

public static void main(String[] args) {

Tuple t1 = new Tuple();

Tuple t2 = new Tuple();

Tuple t3 = new Tuple();

t1.put("1","1");

t1.put("2","2");

t1.put("3","3");

TupleList tls = new TupleList();

tls.addTuple(t3);

tls.addTuple(t2);

tls.addTuple(t1);

Collections.sort ( (List<Tuple>tls, new Comp ( ) ) ;

System.out.println(tls);

}

}

but after all that im getting

Exception in thread "main" java.lang.ClassCastException: com.test.ds.TupleList at com.test.ds.Main.main(Main.java:38)

and i cant understand why , can someone please help me here ? Thanks

Meirya at 2007-7-10 3:44:31 > top of Java-index,Java Essentials,Java Programming...
# 5

The exception is thrown because the tls variable is not a List<Tuple> and you are trying to cast it.

You might simply add a sort method in your TupleList class:public class TupleList {

ArrayList<Tuple> list = new ArrayList();

public void addTuple(Tuple t) {

list.add(t);

}

public Tuple getTuple(int i) {

return list.get(i);

}

public void sort(Comp comparator) {

Collections.sort (list, comparator);

}

}

Note that, as soon as you don't define a specific behaviour in your TupleList class, why don't you simply use a List<Tuple> directly in your app ? (in other words, what is the reason for having a TupleList class ?)

Finally, your Tuple class looks weird to me. You said that "hash table i will have only one key and value", but nothing prevent it from hapening in your code. I cannot see why it has to extend Hashtable.

TimTheEnchantora at 2007-7-10 3:44:31 > top of Java-index,Java Essentials,Java Programming...
# 6

hello and thanks for your reply well this struct has reason its mostly historically and i cant change it , any way i tried what you suggest but still

im getting exception :

Exception in thread "main" java.lang.NullPointerException

at com.test.ds.Comp.compare(Main.java:12)

at com.test.ds.Comp.compare(Main.java:1)

at java.util.Arrays.mergeSort(Arrays.java:1284)

at java.util.Arrays.sort(Arrays.java:1223)

at java.util.Collections.sort(Collections.java:159)

at com.test.ds.TupleList.sort(TupleList.java:20)

at com.test.ds.Main.main(Main.java:38)

maybe because the Tuple when created doing allocation of 10 elements that are null

Meirya at 2007-7-10 3:44:32 > top of Java-index,Java Essentials,Java Programming...
# 7

This NullPointerException comes from the fact that you are putting values in the Tuple t1, but you don't put any value int the two others:t1.put("1","1");

t1.put("2","2");

t1.put("3","3");

Therefore, the getKeyByIndex(1) used by your comparator returns null for t2 and t3.

Regards,

Tim

Anyway, my advise remains: you should review your design. Extending Hashtable is not the way to go here. And I cannot see any reason for not refactoring you classe(s). Your code could become clearer and more robust.

TimTheEnchantora at 2007-7-10 3:44:32 > top of Java-index,Java Essentials,Java Programming...