Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java Question

Options
  • 25-10-2008 5:08pm
    #1
    Closed Accounts Posts: 362 ✭✭


    Storing data in a hashtable
    ArrayList<String> details;
    Hashtable<String, ArrayList<String>> detailslist;
    

    So this would be John -> Date, Score, Time

    so details would be ( Date, Score, Time ), key would be John

    Now john could have multiple details.
    You cannot store multiple same keys in a hashtable ?

    example Hashtable
    ht.put(John, details1)
    ht.put(John, details2)
    ht.put(John, details3)
    ht.put(Paul, details1)
    ht.put(Paul, details1)
    

    I get a list of names(keys) and would like to be able to pull off multiple enties from the hashtable, but it can't be done ?
    So any other objects I should use ?

    I have a solution, that changes the hastable to Hashtable<String, ArrayList<ArrayList<String>>>, but it's a good but more overhead in code thats not relevent here
    details1.add(date, Score, Time)
    details2.add(date, Score, Time)
    details3.add(date, Score, Time)
    details.add(details1,details2,details3)
    ht.put(John,details)
    

    Just seeing if there is a simplier way to do it ?


Comments

  • Registered Users Posts: 11,980 ✭✭✭✭Giblet


    The whole point of a Hashtable is to have a unique key. Make a Details class that has date time and score and maybe an identity of some kind as member variables, and store all the details in an array list of type <Details>

    So you have <John,DetailsList>

    You can access the Hashmap and add more to the list when required.


  • Closed Accounts Posts: 362 ✭✭information


    So you have <John,DetailsList>

    Like I said, I know that, but it is effecting performance a little bit, processing the results into another object rather than just throwing them into the Hashtable.
    details1.add(date, Score, Time)
    details2.add(date, Score, Time)
    details3.add(date, Score, Time)
    details.add(details1,details2,details3)
    ht.put(John,details)
    
    Just seeing if there is a simplier way to do it ?


  • Registered Users Posts: 11,980 ✭✭✭✭Giblet


    Make an object,not another list.
    public class Details
    {
        private String date,score,time;
        public Details(String date,String time, String score)
        {
            this.date = date;
            this.score = score;
            this.time = time;
        }
    
        public String GetDate(){ return date; }
        public String GetTime(){ return time; }
        public String GetScore(){ return score; }
        public void SetDate(String date){ this.date = date; }
        public void SetTime(String time){ this.time = time; }
        public void SetScore(String score){ this.score = score; }
    }
    

    You can then create the object inline when adding to the hashtable.

    Hashtable.Get("John").add(new Details("Blah","Blah","Blah");

    If you get performance issues with that, then you have serious problems in your code.


  • Closed Accounts Posts: 362 ✭✭information


    Make an object,not another list.
    public class Details
    {
        private String date,score,time;
        public Details(String date,String time, String score)
        {
            this.date = date;
            this.score = score;
            this.time = time;
        }
    
        public String GetDate(){ return date; }
        public String GetTime(){ return time; }
        public String GetScore(){ return score; }
        public void SetDate(String date){ this.date = date; }
        public void SetTime(String time){ this.time = time; }
        public void SetScore(String score){ this.score = score; }
    }
    

    You can then create the object inline when adding to the hashtable.

    Hashtable.Get("John").add(new Details("Blah","Blah","Blah");

    If you get performance issues with that, then you have serious problems in your code.
    Thank you for your effort, I know what you are talking about but it is not the solution I am looking for, I already have a solution that does the same as what you attempted, but in a much more efficient manner.

    Any one else who understood my question have any advise ?


  • Registered Users Posts: 981 ✭✭✭fasty


    Use a key that's more unique than just a name.


  • Advertisement
  • Registered Users Posts: 569 ✭✭✭none


    Like I said, I know that, but it is effecting performance a little bit, processing the results into another object rather than just throwing them into the Hashtable.

    Generally, from the point of view of performance, the order is (from best to worst):
    1) <key, DetailsArray>
    2) <key, DetailsList>
    3) <key, DetailsMap>
    4) <key, DetailsObject>
    But it depends of what types of operations you do most.


Advertisement