• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to compeer Arraylist contains hashtables by key name

 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all
i have Arraylist that contains hashtables
how can i sort the ArrayList by the hashtables key?
 
Campbell Ritchie
Sheriff
Pie
Posts: 50168
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50168
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50168
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and there is mention of Comparator in the Java tutorial under Collections . . . algorithms.
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50168
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
some one?
is there any sulotion for this problem ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic