I am trying to append values to a map in a scala class.
I have a generic map with question / answerList pairs in my controller class:
static Map<Question, List<Answer>> myMap = new HashMap<Question, List<Answer>>();
Question question1 = new Question("xyz", "Do Androids dream?", 127, "Marcus");
Answer answer11 = new Answer("zab", "xyz", "Only of electric sheep!", 70, "Sarah");
List<Answer> answerList1 = new ArrayList<Answer>();
answerList1.add(answer11);
// puts the question and its answers into the hashmap
myMap.put(question1, answerList1);
public static Result askQuestion(){
return ok(views.html.questionAnswer.render(myMap));
}
In my viewclass I add a new question / answer pair into the map:
@import model.Question
@import model.Answer
@(myMap: Map[Question, List[Answer]])
@main("Ask question"){
@myMap.put(new Question("id123", "questiontext", 34, "userID"), new List[Answer]);
}
But when I run the code the error page comes up, saying: "trait List is abstract; cannot be instantiated". Why cant I put a (question, List[answer]) pair into my map?
You get an error because, just as your error message states: you can not instantiate an abstract class.
You'll need to instantiate a subclass of it.
new List[Answer]
will never work. Instantiate a subclass that suits your needs.
As Stultuske and Kulu Limpa have pointed out you need to instantiate a subclass. This will work:
// Note: You need to tell the compiler which type Nill should use
@myMap.put(new Question("id123", "questiontext", 34, "userID"), Nil: List[Answer]);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.