• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

how to compeer Arraylist contains hashtables by key name

 
Ranch Hand
Posts: 620
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all
i have Arraylist that contains hashtables
how can i sort the ArrayList by the hashtables key?
 
Marshal
Posts: 80867
505
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
ben josh
Ranch Hand
Posts: 620
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 80867
505
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 80867
505
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and there is mention of Comparator in the Java tutorial under Collections . . . algorithms.
 
ben josh
Ranch Hand
Posts: 620
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 80867
505
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
ben josh
Ranch Hand
Posts: 620
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
ben josh
Ranch Hand
Posts: 620
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
ben josh
Ranch Hand
Posts: 620
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
some one?
is there any sulotion for this problem ?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic