This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes How to control the element's order in a collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "How to control the element Watch "How to control the element New topic
Author

How to control the element's order in a collection

Manish Kumar
Ranch Hand

Joined: Apr 15, 2002
Posts: 53
Hi,
I've used a dialog box for the ticket booking. I pass the all the parameter of the selected flight to the constructor of the BookingDialog.
The details are passed thru a HashMap which contains the flight attributes and their values.
But the HashMap doesn't guarantee the order of the stored elements.

What could be the best way to maintain the order? I think the other way is to pass two arrays, one for the attribute and one for the value. But it may create a serious problem in case of the index mismatch..

Any comment will be appreciated.

Thanks and regards
Manish Kumar
---------------
- SCJP2
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17276
    
    6

I pass the all the parameter of the selected flight.
Sounds like the DataInfo class is the class you want to pass instead of the HashMap.
Mark


Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937

The details are passed thru a HashMap which contains the flight attributes and their values.
But the HashMap doesn't guarantee the order of the stored elements.

Why do you need to know the order of the elements? The access to the map values is given using the keys, so your code would look something like this:
String flightNumber = (String)flightAttribMap.get("flightNumber");
int seatsToBook = ((Integer)flightAttribMap.get("seatsToBook")).intValue();
Eugene.
Manish Kumar
Ranch Hand

Joined: Apr 15, 2002
Posts: 53
Hi,
I just wanted to display the flight attributes in following order..
Flight number
Origin airport
Destination airport
Carrier
...
.
but i didn't want to hard-code the attribute names except the flight number.. just to make the booking dialog more generic.
now i think, i'll have to hard-code all the attributes like this
Manish Kumar
Ranch Hand

Joined: Apr 15, 2002
Posts: 53
Hi,
I just wanted to display the flight attributes in following order..
Flight number
Origin airport
Destination airport
Carrier
...
. in same order as it appeares in the flight table.
but i didn't want to hard-code the attribute names except the flight number.. just to make the booking dialog more generic.
now i think, i'll have to hard-code all the attributes like this
Ashik Uzzaman
Ranch Hand

Joined: Jul 05, 2001
Posts: 2373

Does using TreeMap makes some sense here?


Ashik Uzzaman
Senior Software Engineer, TubeMogul, Emeryville, CA, USA.
Manish Kumar
Ranch Hand

Joined: Apr 15, 2002
Posts: 53
no..no.. tree map will gurantee the order in the sorted form..
I was trying get the same order as i used to store the elements.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to control the element's order in a collection
 
It's not a secret anymore!