Implement a hash table for a question / answer group

What is the easiest way to implement a hash table (or use the best way) to store a list of questions and related answers (1 possible answer to a question)?

I originally created an ArrayList to store questions. I could make a second ArrayList for answers, but as soon as I have a lot of questions, trying to combine questions and answers gets complicated.

Can someone send a quick code sample? Thanks!

+4
source share
4 answers

What would you like the key to be? If there is only one possible answer to the question, and in some cases there may be no answer, this sounds like an ideal reason to have an Answer link in the Question class. Then just provide a list of questions.

If this does not meet a specific requirement, please provide additional information about what you are trying to do. (For example, you may need to move from the answer to the question ... in this case, you may have a link from the answer to the question in the object ... this is a little random from the point of view of circular references and does not lend itself to immutable types, but it would not completely unreasonable.)

One reason to prefer to have a list of questions on a simple map is that the questions are usually naturally sorted in some way, while the cards are inherently disordered. But we will need to learn more about your actual context in order to say for sure whether this is really relevant.

+4
source

HashMap is a great choice, but I would recommend you take another step.

Java is an object oriented language. Why not create an abstraction of the questionnaire?

 public class Questionnaire { private Map<String, String> questionsAndAnswers = new HashMap<String, String>(); // more follows. } 
+1
source

For this simple 1: 1 match, you don’t need a complex HashMap. A simple class consisting of a question and an 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 containing all the questions:

  List<Question> questionnaire = new ArrayList<Question>(); questionnaire.add(new Question("1+1?", "2")); System.out.println("Question 1: " + questionnaire.get(0).getText()); 
0
source

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

However, when choosing an implementation, you need to consider what operations you want to optimize for: { Insert , Delete , Random Access , Serial Access }

If you need only random access to each question (and answer), then HashMap best.

The most common use case for a dictionary, such as an application, is a list of series of entries. Thus, if you are going to show a series of questions (and answers), use the HashMap option, LinkedHashMap .

Your basic hash table functionality is provided by HashMap , and this will lead to O (1) complexity for searching, however the iteration needed to display a “series” or “listing” can be expensive.

In this case, LinkedHashMap best suit your needs as it provides a linked list bound to a base hash table; therefore, iteration or sequential access becomes inexpensive. However, the trade-off is that it makes adding and removing new entries (questions) more expensive.

0
source

All Articles