• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

How to control the element's order in a collection

 
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
 
ranger
Posts: 17346
11
Mac IntelliJ IDE 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
 
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
 
Ranch Hand
Posts: 2376
MySQL Database Spring 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.
 
Barry's not gonna like this. Barry's not gonna like this one bit. What is Barry's deal with tiny ads?
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic