views:

80

answers:

4

What's the easiest way to implement a hashtable (or use a better way) to store a list of questions and their associated answers (1 possible answer per question)?

Originally I had created an ArrayList to store the questions. I could have made a second ArrayList for the answers, but once I have a lot of questions, trying to match up questions and answers gets complicated.

Can someone post a quick code sample? Thanks!

+4  A: 

What would you want the key to be? If there's only one possible answer per question, and there may not be an answer in some cases, that sounds like an ideal reason to have an Answer reference within the Question class. Then just have a list of questions.

If that doesn't meet some particular requirement, please give more information about what you're trying to do. (For example, you may need to navigate from an answer to a question... in which case you could have a reference from the answer back to the question in the object... it's a bit messy in terms of cyclic references, and doesn't lend itself to immutable types, but it wouldn't be entirely unreasonable.)

One reason to prefer having a list of questions over a simple map is that questions tend to be naturally ordered in some way - whereas maps are inherently unordered, usually. But we'd need to know more about your actual context to say for sure whether or not that was relevant.

Jon Skeet
+1  A: 

A HashMap is a perfect choice, but I'd recommend that you go a step further.

Java's an object-oriented language. Why not create a Questionnaire abstraction?

public class Questionnaire
{
    private Map<String, String> questionsAndAnswers = new HashMap<String, String>();

    // more follows.

}
duffymo
A: 

For this simple 1:1 mapping you don't necessarily need a complex HashMap. A simple class, composed of a question and it's associated answer is enough:

public class Question {
   final private String text;
   final private String answer;
   public Question(String text, String answer) {
     this.text= text;
     this.answer = answer;
   }

   public String getText() { return text; }
   public String getAnswer() { return answer; }
   public String toString() { return text + " - " + answer; }
}

Then, later in your code, you can create a list that holds all Question:

   List<Question> questionnaire = new ArrayList<Question>();
   questionnaire.add(new Question("1+1?", "2"));
   System.out.println("Question 1: " + questionnaire.get(0).getText());
Andreas_D
You should make `text` and `answer` final if they're not going to be modified after `Question` is constructed.
Steve Kuo
A: 

Your basic requirement is met by any class that implements the Map interface (java.util.Map).

However in choosing the implementation it is imperative to consider which operations you wish to optimise for: {Insert, Delete, Random Access, Sequential Access}

If you require only random access to each question (and answer), then the HashMap is probably your best bet.

The most common use-case of a dictionary like application is listing series of entries. Thus, if you are going to be displaying series of questions (and answers) consider using a variant of HashMap, LinkedHashMap.

Your basic hash table functionality is provided by HashMap, and it will yield O(1) complexity for lookup, however iteration, required for displaying "series" or "listings" can be expensive.

In this case a LinkedHashMap would serve your needs best because it provides a linked list tacked on to the underlying hashtable; therefore iteration or sequential access becomes inexpensive. However, the tradeoff of this is that this makes addition and deletion of new entries (questions) more expensive.

bguiz