Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

How to control the element's order in a collection

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2378
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does using TreeMap makes some sense here?
 
Manish Kumar
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic