使用Set Collection作为我的问题类的成员

时间:2015-04-02 05:28:27

标签: java

我想使用Set Collection作为我的问题类的成员来存储唯一的多个amswers

class Questions
{
   int id ;
   String question;
   Set<String> answers ;
}

但是Don不知道如何存储答案并从中检索答案 任何人都可以通过提供相关的例子来帮助我吗?

1 个答案:

答案 0 :(得分:0)

问题类:

class Questions {

  private int id;
  private String question;
  private Set<String> answers;

  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }

  public String getQuestion() {
    return question;
  }

  public void setQuestion(String question) {
    this.question = question;
  }

  public Set<String> getAnswers() {
    return answers;
  }

  public void setAnswers(Set<String> answers) {
    this.answers = answers;
  }

}

<强>示例:

public class Sample {

  public static void main(String[] args) {
    Questions question = new Questions();
    question.setId(101);
    question.setQuestion("Random Question");

    Set<String> answer= new HashSet<String>();
    answer.add("random answer1");
    answer.add("random answer2");

    question.setAnswers(answer);

    //Similarly invoke getXXX to get the values
  }

}