Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help.. Do I need an Vector and Hashtable used together?

Status
Not open for further replies.
Jan 8, 2001
163
US
I have a program that's currently designed as follows

UserData.class - contains the following method

Public Hashtable Get_Companies(){[ul][li]selects every company name and company id from Companies table alphabetically by company name [/li][li] Puts resultset into a hashtable. [/li][li] Then returns the hashtable.[/li][/ul]}

AddUser.jsp[ul][li]Calls Get_Companies() [/li][li]Uses a loop to retrieve the company name and company id from the returned hashtable & Puts each company name into a drop down list box that the user can choose from.[/li][/ul]

The issue is that when I loop through the hashtable to display the company names in the drop down list box, it's no longer in alphabetical order. So, I now understand that a hashtable is not what I need because it doesn't hold the order of the elements. But will a Vector work? My goal is to let the user select the company name but to have the company id available in the background. Does anyone have any advice or examples of how I can achieve this? Do I need a hashtable and a vector to work together for my purposes? I'd appreciate any help you may be able to offer.

Thank You,
CrystalVisualBOracle
 
You could use a TreeMap or a TreeSet. They both keep order, actually a TreeSet uses TreeMap internally. You need to make sure you objects implement the Comparable interface or that you explicitly write a Comparator for the TreeMap. Wushutwist
 
Sorry I submitted the post before I was finished. The other option is if you are getting the results from your DB in an order format (ie using ORDER BY blah blah) then you could just one by one them in any List using add() and the order would be retained. If this is the case I recommend using an ArrayList, it tends to be slightly faster than Vector because a Vector object is always synchronized. Wushutwist
 
I might consider using 2d array instead of ArrayList or Vector. Reason being, you need to keep the company's id available in the background. ArrayList or Vector will work though just that 2d array might make it look neater.

String[][] companyList = new String[x][y];

x - number of companies, which you should be able to get from your resultset
y - this value should always 1 because you need to store only the company id for the corresponding company.

This is how it would look like:-

-----------------------------------
|Company Name|Company ID|
|------------------|--------------|
|abc co. |123a |
|------------------|--------------|
|bcd co. |123b |
|------------------|--------------|
...

So all you have to do is to return the company name in order to populate your list box

Hope this helps,
Leon
 
Thanks for your help! I considered using the 2D array however, I never know the number of rows that will be returned from the database so I can't set the array size. I thought there may be a ResultSet method which would give me that number but I wasn't able to find one.

So that left me with the Vector which isn't two dimensional. I really don't grasp the concept of the tree map, unfortunately, and it doesn't sound like I need that much overhead. So I'm thinking of tokenizing my compid and companyname togther when their returned from the database and putting them into a vector. Then I can just grab them in order and detokenize as I display them.

Thoughts?
 
Why not place them in a data object?
Example:
Code:
public class Company {

  private String companyName;
  private String companyID;

  public Company() {
    this(null,null);  
  }
  
  public Company(String companyName, String companyID) {
    setCompanyName(companyName);
    setCompanyID(companyID);
  }

  public void setCompanyName(String companyName) {
    this.companyName = companyName;
  }

  public void setCompanyID(String companyID) {
    this.companyID = companyID;
  }
  
  public String getCompanyName() {
    return companyName;
  }

  public String getCompanyID() {
    return companyID;
  }

  public String toString() {
    StringBuffer sb = new StringBuffer();
    sb.append("[companyName=" + getCompanyName()+ ",");
    sb.append(" companyID=" + getCompanyID() + "]");
    return sb.toString();
  }

  public boolean equals(Object o) {
    if( o instanceof Company ) {
      Company c = (Company)o;
      if (getCompanyName() != null && getCompanyID() != null) {
        return getCompanyName().equals(c.getCompanyName()) && getCompanyID().equals(c.getCompanyID());
      }
    }
    return false;
  }

}

Sure beats detokenizing a String each time and you would be using good Object-Oriented Design. Wushutwist
 
Hey, Wushutist is right. It is quite a good idea to use Objects to store your data, which sure beats using 2d array too. It would be easy to do so too; create a method which accepts 2 parameters. The method would create a new Company object and add it to an ArrayList. So all you need to do is loop through your resultset and call this method for every rs.next() (where rs is your resultset)

If you still want to implement the 2d array way, you can find the number of rows by calling the getArray() method to return an Array. Get the size of the returned array and it would be the number of rows :)

Leon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top