This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Java in General and the fly likes Collections problem Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Collections problem" Watch "Collections problem" New topic
Author

Collections problem

fahad siddiqui
Ranch Hand

Joined: Jun 14, 2006
Posts: 85
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
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1076

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


Gravitation cannot be held responsible for people falling in love ~ Albert Einstein
fahad siddiqui
Ranch Hand

Joined: Jun 14, 2006
Posts: 85
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

Joined: Aug 13, 2004
Posts: 1076

You need to write a comparator like this


Shailesh
[ September 05, 2006: Message edited by: Shailesh Chandra ]
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 26489
    
  78

I'm going to move this to Java in General since - as pointed out - it is not a JDBC question.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1295
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.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
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


Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Collections problem
 
Similar Threads
Sorting of hashtables in a vector
Memorizing the Collections
Hashtable iterator question
Want to Create TreeTable in swings .Please help me out its urgent
Sorting ArrayList of Hashtables