| Author |
how to compeer Arraylist contains hashtables by key name
|
Meir Yan
Ranch Hand
Joined: Apr 27, 2006
Posts: 597
|
|
Hello all i have Arraylist that contains hashtables how can i sort the ArrayList by the hashtables key?
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32654
|
|
Sorting depends on comparisons. Find the sort method of the Collections class. Find the Comparator interface and set it up to compare the keys from the Hashtable entries. Alternatively it is possible to get a Set of the map entries. Check whether you can sort that set by the keys of the entry. It seems slightly odd that you are putting Hashtables into an ArrayList in the first place.
|
 |
Meir Yan
Ranch Hand
Joined: Apr 27, 2006
Posts: 597
|
|
well it kind of data structure i need to have in my application so i bent to that . can i get some kind of example how to set up comparator for this scenario ? link or tutorial thanks
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32654
|
|
|
Have you read the Comparator interface in the API? Work out a way to get it to return 0 if the two objects are "equal" negative number if less and positive number if more. Also implement the "equals" method of Comparator. Then you pass the Comparator object to the sort() method as a parameter.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32654
|
|
|
. . . and there is mention of Comparator in the Java tutorial under Collections . . . algorithms.
|
 |
Meir Yan
Ranch Hand
Joined: Apr 27, 2006
Posts: 597
|
|
hello all after reading some tutorials i still cant do this comparison right .. here is what i have : my data strut : but after all that im getting and i cant understand why , can someone please help me here ? Thanks
|
 |
Garrett Rowe
Ranch Hand
Joined: Jan 17, 2006
Posts: 1295
|
|
The current problem is here. a TupleList is not a List<Tuple> and can't be made one even with a cast. Internally a TupleList contains a reference to a List<Tuple>, but this is irrelevant. Either you can make your TupleList class implement List<Tuple> or you can sort the internal List<Tuple> by some other means. On another note, since you are using generics in the rest of your project, you could eliminate the casts in your Comp.compare() method and make it typesafe by changing it to: [ March 16, 2007: Message edited by: Garrett Rowe ]
|
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
|
 |
Garrett Rowe
Ranch Hand
Joined: Jan 17, 2006
Posts: 1295
|
|
Originally posted by Campbell Ritchie: Have you read the Comparator interface in the API? Work out a way to get it to return 0 if the two objects are "equal" negative number if less and positive number if more. Also implement the "equals" method of Comparator...
Why would it be helpful to override the equals() method here? Normally Comparators that I've used have no state.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32654
|
|
Override equals? I thought you had to because I thought there was an equals method in Comparator, but maybe I was mistaken. Thank you.
|
 |
Meir Yan
Ranch Hand
Joined: Apr 27, 2006
Posts: 597
|
|
Hi even so i did your changes also to the TupleList and the comp class i still have exceptions i cant understand it ...
|
 |
Garrett Rowe
Ranch Hand
Joined: Jan 17, 2006
Posts: 1295
|
|
I thought there was an equals method in Comparator...
There is but according to the documentation:
Note that it is always safe not to override Object.equals(Object). However, overriding this method may, in some cases, improve performance by allowing programs to determine that two distinct Comparators impose the same order.
I've just never written one where this would be useful.
|
 |
Garrett Rowe
Ranch Hand
Joined: Jan 17, 2006
Posts: 1295
|
|
Hi even so i did your changes also to the TupleList and the comp class i still have exceptions
Which line is line 38? Exactly what changes did you make?
|
 |
Meir Yan
Ranch Hand
Joined: Apr 27, 2006
Posts: 597
|
|
here is what i did : in the Tuplelist class now lookes like this : and my main class looks like this : and im geting this excption : maybe because the Tuple when created doing allocation of 10 elements that are null ? i reallydont know .. help
|
 |
Meir Yan
Ranch Hand
Joined: Apr 27, 2006
Posts: 597
|
|
some one? is there any sulotion for this problem ?
|
 |
 |
|
|
subject: how to compeer Arraylist contains hashtables by key name
|
|
|