Lakshmeenarayana, You wanted the keys to be sorted? You really didn't mention that in any of your previous posts. Everyone, including myself, gave you solutions that resulted in an ordered list. This is to say that the order that the items that were put into the collection is preserved and returned in an Iterator.
I know this is not efficient way of implementing this solution. But why SUN has not given the hashmap.sortOnKeys() ?? Ok if hashMap takes the null values.. then at least they could have given hashtable.sortOnKeys()...??!!!
The reason Sun does not include a method such as hashmap.sortOnKeys() is that it doesn't make sense to do so if you understand the Collections classes. If you use a hashmap, then you don't care about order or sorting the items. You can use a TreeMap, which will sort the items according to their natural order based on the key values. If you want the items to be sorted on their values, then you can provide a Comparator class at creation time that will cause the sorting to be done on the values. I'd suggest that you pick up a book or go through an online tutorial and learn and understand how the Collections classes because it is an important concept for any good developer. What exactly are the requirements for storing and retrieving your name-value pairs? Do you need to randomly access a value for a given key or are you always going to retrieve the entire list of values and iterate through them one at a time? Is order important? Do you need to sort the items? Are you going to insert values into the middle or always append at the end. Is the collection going to be accessible from multiple threads? The answer to these questions will determine which collection you should use. By learning to use the Collections classes, you can avoid writing a lot of extra code as you've described. Regards, Richard At 10:56 AM 11/23/2002 +0530, you wrote:
Well guys... I got so confused with all these Collection API.. At last I sorted the values by using... ArrayList alist = new ArrayList(hashmap.values()); Collections.sort(alist); Then with the original map, I am looping through and getting the keys for the sorted values and in the loop I am feeding it to two Object Arrays. One for the Key and one for the Value. Now the two Object Arrays are having the sorted key values. I know this is not efficient way of implementing this solution. But why SUN has not given the hashmap.sortOnKeys() ?? Ok if hashMap takes the null values.. then at least they could have given hashtable.sortOnKeys()...??!!! thanks again for your time. Cheers.. L G Goundalkar. -----Original Message----- From: A mailing list about Java Server Pages specification and reference [mailto:[EMAIL PROTECTED]]On Behalf Of Amit Ghaste Sent: Saturday, November 23, 2002 1:46 AM To: [EMAIL PROTECTED] Subject: FW: Hashtable / HashMap Problem. I wonder why my reply went to the user and not to the list... anyways sorry to send it to uy directly but u can use java.util.LinkedHashMap as shown below Regards Amit -----Original Message----- From: Amit Ghaste [mailto:[EMAIL PROTECTED]] Sent: Friday, November 22, 2002 12:32 AM To: [EMAIL PROTECTED] Subject: RE: Hashtable / HashMap Problem. internally the hashtable stores object refrences according to hashcodes, if u get an enumeration of the stored values or keys, the order is not predictible its the same result with a iterator.... since hashmap is similar to hashtable (except that its meathods are not synched) it will give u the same result to preserve order use the LinkedHashMap... this preserves the order when u retrive it thro the keyset iterator but its meathods are not synchronized... so if u are doing multi threaded operation on shared data u need to synch it get more info from the api if that helps Amit -----Original Message----- From: A mailing list about Java Server Pages specification and reference [mailto:[EMAIL PROTECTED]]On Behalf Of Lakshmeenarayana G G Sent: Thursday, November 21, 2002 8:51 PM To: [EMAIL PROTECTED] Subject: Hashtable / HashMap Problem. Hi, I am using Hashtable or HashMap to keep Key-Value pair of elements. But When I add them, The Hashtable or HashMap gets sorted depending on the keys. I want to retain the order in which I have added the key-value pair. Is there anyway to do it with Hashtable or HashMap ? Or Can I use any other Collection type which will have the mapping of key-value which is not sorted other than two dimensional Array. Thanks for the time. Lakshmeenarayana G G CLI Division - SysArris Software Bangalore - India Tel. No. +91-80-665 4965, 665 5052, 664 2690 Xtn-240 Fax No. +91-80-665 0374, 655 4285 http://www.sysarris.com/ =========================================================================== To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST". For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST". Some relevant FAQs on JSP/Servlets can be found at: http://archives.java.sun.com/jsp-interest.html http://java.sun.com/products/jsp/faq.html http://www.esperanto.org.nz/jsp/jspfaq.jsp http://www.jguru.com/faq/index.jsp http://www.jspinsider.com =========================================================================== To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST". For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST". Some relevant FAQs on JSP/Servlets can be found at: http://archives.java.sun.com/jsp-interest.html http://java.sun.com/products/jsp/faq.html http://www.esperanto.org.nz/jsp/jspfaq.jsp http://www.jguru.com/faq/index.jsp http://www.jspinsider.com =========================================================================== To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST". For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST". Some relevant FAQs on JSP/Servlets can be found at: http://archives.java.sun.com/jsp-interest.html http://java.sun.com/products/jsp/faq.html http://www.esperanto.org.nz/jsp/jspfaq.jsp http://www.jguru.com/faq/index.jsp http://www.jspinsider.com
=========================================================================== To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST". For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST". Some relevant FAQs on JSP/Servlets can be found at: http://archives.java.sun.com/jsp-interest.html http://java.sun.com/products/jsp/faq.html http://www.esperanto.org.nz/jsp/jspfaq.jsp http://www.jguru.com/faq/index.jsp http://www.jspinsider.com