Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to control the element's order in a collection

 
Manish Kumar
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2373
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does using TreeMap makes some sense here?
 
Manish Kumar
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic