java—通过对象列表进行流式处理,以检索具有特定枚举的对象

unhi4e5o  于 2021-07-09  发布在  Java
关注(0)|答案(0)|浏览(222)

我有一个课堂问题:

public class Question {
private String question, correctAnswer, answer1, answer2, answer3;
private Topic topic;
private Difficulty difficulty;

public Question(String question, String correctAnswer, String answer1, String answer2, String answer3, Topic topic, Difficulty difficulty) {
    this.question = question;
    this.correctAnswer = correctAnswer;
    this.answer1 = answer1;
    this.answer2 = answer2;
    this.answer3 = answer3;
    this.topic = topic;
    this.difficulty = difficulty;
}
Getters & setters.....

枚举难度等级:

public enum Difficulty {
EASY,MEDIUM

}
当我创建一个对象

public List<Question> questions = new ArrayList<>();
 public void addQuestion(Question q){
    this.questions.add(q);
}

 Question q1 = new Question("What is the Queen of England?", "Elizabeth", "Patrick", "John", "Philip", Topic.HISTORY, Difficulty.EASY);
    Question q2 = new Question("When did WW2 Started??", "1939", "1945", "1942", "1938", Topic.HISTORY, Difficulty.MEDIUM);
addQuestion(q1);
addQuestion(q2);

控制器类:

public class Controller {
public List<Question> questions;
public int currentQuestion = 0;

public void initialize(){
    List<Question> easyQuestions;
    easyQuestions = questions.stream().filter(q-> q.getQuestion().contains(q.getDifficulty())).collect(Collectors.toList());
}

我想通过流问题和检索有困难的问题很容易,我怎么能做到这一点与流?因为现在我只得到错误:d

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题