• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Collections problem

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I use a hashtable which contains a String as a key and an arraylist as a value.
This hashtable is iterated on the jsp and first the string is displayed, then another iteration takes place for the arraylist for the string key and hence, submenu type of stuffs gets displayed.

Now, i want to display a particular String and its arraylist iterated on the top of the page and the rest as is.

Tree map sorts them, so i was thinking, is it possible to implement a comparator to the tree map which would never compare anything and hence, tree map would retain data in the order it was added and hence, displayed properly.

Can someone advise on this. Please provide a proper code for comparator implemented class
or some other solution for this.


Regards,
Fahad
 
Ranch Hand
Posts: 1087
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all your question doesn't fit into JDBC.

Now back to your problem, I would suggest you create a comparator and user TreeMap in your code.

Create instance of TreeMap by passing your comparator in constructor.

If you need help on comparator, visit the URL

http://www.onjava.com/pub/a/onjava/2003/03/12/java_comp.html?page=2


Shailesh
 
fahad siddiqui
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I knew that a comparator needs to be used, but i needed an example code on the same which would make no checking and hence, would just store the data in the treemap as is entered.

All the examples on google gives a code for comparison and when i return false in every case no matter of what compare method returns, then it doesnt give correct results.
 
Shailesh Chandra
Ranch Hand
Posts: 1087
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to write a comparator like this


Shailesh
[ September 05, 2006: Message edited by: Shailesh Chandra ]
 
author & internet detective
Posts: 41878
909
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm going to move this to Java in General since - as pointed out - it is not a JDBC question.
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

is it possible to implement a comparator to the tree map which would never compare anything and hence, tree map would retain data in the order it was added and hence, displayed properly.



Sounds like a LinkedHashMap could be of some use here.
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by fahad siddiqui:

Tree map sorts them, so i was thinking, is it possible to implement a comparator to the tree map which would never compare anything and hence, tree map would retain data in the order it was added and hence, displayed properly.



As your key is an String, so TreeMap will always sort the key. If you want to maintain insertion order for the keys, use LinkedHashMap. It maintains insertion order. Why you are looking for a comparator.

In those cases where you don't have source code with you and supppose your key class does not implement Comparable interface and you want to use the instances of the class as a key in TreeMap, there Comparator is very useful. You can write a seperate Comparator which will actually sort the key as per the logic written in compare() method.

Naseem
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by fahad siddiqui:
[QB]
Tree map sorts them, so i was thinking, is it possible to implement a comparator to the tree map which would never compare anything and hence, tree map would retain data in the order it was added and hence, displayed properly.



No, that wouldn't work - TreeSet heavily depends on the comparator defining a true ordering for orgizing its whole structure.

Like already suggested by someone else, a LinkedHashMap is what you should use.
 
reply
    Bookmark Topic Watch Topic
  • New Topic