gpt4 book ai didi

java - 填充嵌套 HashMap

转载 作者:行者123 更新时间:2023-12-01 13:25:59 32 4
gpt4 key购买 nike

我有一个 HashMap ,其中包含一个 HashMap,而第二个 HashMap 包含另一个 HashMap :

public static final Map<String, Map<String, Map<String, Boolean>>> questionnaireData;

我希望我的 HashMapstaticfinal 并包含数据,所以我执行了以下操作:

public static final Map<String, Map<String, Map<String, Boolean>>> questionnaireData;
static {
Map<String, Map<String, Map<String, Boolean>>> data = new HashMap<>();
data.put(
"Architecture des ordinateurs",
new HashMap<String, Map<String, Boolean>>() {{
put(
"1. La partie du processeur spécialisée pour les calculs est :",
new HashMap<String, Boolean>() {{
put("L’unité mathématique", false);
put("Les Registres", false);
put("L’unité arithmétique et logiqueue et logique", true);
put("UCC", false);
}}
);
put(
"2. Dans un ordinateur, les données sont présentées par un signal électrique de la forme :",
new HashMap<String, Boolean>() {{
put("Analogique", false);
put("Numérique", true);
put("Alphanumérique", false);
put("Alphabétique", false);
}}
);
put(
"3. Les différents éléments d’un ordinateur (mémoire, processeur, périphériques…) sont reliés entre eux par des:",
new HashMap<String, Boolean>() {{
put("Fils/câbles", true);
put("Registres", false);
put("Cartes d’extensions", false);
put("Bus", false);
}}
);
}}
);
data.put(
"Bureautique",
new HashMap<String, Map<String, Boolean>>() {{
put(
"1. Quelles sont les fonctions d’un logiciel de traitement de texte ?",
new HashMap<String, Boolean>() {{
put("Mise en page d’un texte", true);
put("Compilation d’un texte", false);
put("Présentation d’un texte sous forme de diaporama ", false);
put("Edition d’un texte", true);
}}
);
put(
"2. Insérer des lignes supplémentaires dans un tableau Word :",
new HashMap<String, Boolean>() {{
put("Cela n'est pas possible et il faut calculer dès le départ le nombre de lignes qui seront nécessaires.", false);
put("Peut se faire à n'importe quel moment en allant dans la dernière cellule du tableau et en appuyant sur la touche Espace.", false);
put("Peut se faire à n'importe quel moment en utilisant le menu Tableau - Insérer Lignes.", true);
put("Peut se faire à n'importe quel moment en allant dans la dernière cellule du tableau et en appuyant sur la touche Entrer.", false);
}}
);
put(
"3. Qu’est ce qu’une cellule :",
new HashMap<String, Boolean>() {{
put("La cellule est une colonne.", false);
put("La cellule est une ligne.", false);
put("Aucune des deux réponses.", true);
}}
);
}}
);
questionnaireData = Collections.unmodifiableMap(data);
}

我听说使用匿名类会出现问题,但我找不到除此之外的任何其他方法。

事实上,有一种方法我必须声明并填充 HashMap 并将其用作父 HashMap 的参数,但就我而言,我'最终会得到数百个声明。

你觉得我的代码怎么样?如果有更好的方法,请告诉我。

最佳答案

既然您要求提供示例,我们就开始吧。

主题:

package com.answer.stack.overflow.questionnaire;

import java.util.Set;
import java.util.TreeSet;

public class Topic {
private String title;

private Set<Question> questions = new TreeSet<Question>();

public Topic(String title) {
this.title = title;
}

public String getTitle() {
return title;
}

public void addQuestion(Question question) {
questions.add(question);
}

public Set<Question> getQuestions() {
return questions;
}

@Override
public String toString() {
String text = "Topic is ... " + title + "\n";

for (Question question : questions) {
text += question.toString() + "\n";
}

return text;
}
}

问题:

package com.answer.stack.overflow.questionnaire;

public class Question implements Comparable<Question> {
private String text;
private String code;

private Answer correctAnswer;

public Question(String code, String text, String correctAnswerText) {
this.code = code;
this.text = text;
this.correctAnswer = new Answer(code, correctAnswerText);
}

public String getText() {
return text;
}

public String getCode() {
return code;
}

public boolean isCorrect(String answer) {
return correctAnswer.getText().equalsIgnoreCase(answer);
}

@Override
public int compareTo(Question o) {
return code.compareToIgnoreCase(o.code);
}

@Override
public String toString() {
return code + ": " + text + " (" + correctAnswer.getText() + ")";
}
}

答案:

package com.answer.stack.overflow.questionnaire;

public class Answer {
private String code;
private String text;

public Answer(String code, String text) {
this.code = code;
this.text = text;
}

public String getText() {
return text;
}

@Override
public String toString() {
return code + ": " + text;
}
}

调查问卷:

package com.answer.stack.overflow.questionnaire;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Questionnaire {
private List<Topic> topics = new ArrayList<Topic>();

public void addTopic(Topic topic) {
topics.add(topic);
}

public void checkAnswers(Map<String, String> answers) {
for (Topic topic : topics) {
for (Question question : topic.getQuestions()) {
String code = question.getCode();
String answerText = answers.get(code);

if (answerText == null) {
System.out.println("Answer not provided for question " + code + ".");
} else if (!question.isCorrect(answerText)) {
System.out.println("\"" + answerText + "\" is an incorrect answer for question " + code + ".");
} else {
System.out.println("Question " + code + " was answered correctly!");
}
}
}
}

@Override
public String toString() {
String text = "";

for (Topic topic : topics) {
text += topic.toString() + "\n";
}

return text;
}

public static void main(String[] args) {
Questionnaire questionnaire = new Questionnaire();
Map<String, String> answers = new HashMap<String, String>();
Topic topic = new Topic("My attributes");

topic.addQuestion(new Question("1A", "What is my name?", "Anonymous"));
topic.addQuestion(new Question("1B", "How tall am I?", "190 cm"));

questionnaire.addTopic(topic);

topic = new Topic("Maths");

topic.addQuestion(new Question("2A", "How much is 1+1?", "2"));
topic.addQuestion(new Question("2B", "How much is 3/0?", "Not a number"));

questionnaire.addTopic(topic);

System.out.println(questionnaire.toString());

answers.put("1B", "190 cm");
answers.put("2A", "2");
answers.put("2B", "infinite?");

questionnaire.checkAnswers(answers);
}
}

输出:

Topic is ... My attributes
1A: What is my name? (Anonymous)
1B: How tall am I? (190 cm)

Topic is ... Maths
2A: How much is 1+1? (2)
2B: How much is 3/0? (Not a number)

Answer not provided for question 1A.
Question 1B was answered correctly!
Question 2A was answered correctly!
"infinite?" is an incorrect answer for question 2B.

请注意,您应该从文件中读取所有问题,就像 qilihq 建议的那样,而不是将它们硬编码到您的程序中。

此外,这绝不是优化版本。重点是演示如何使用自定义类解决相同的问题。您可能想要研究访问者模式,以便以优雅且简单的方式检查答案,例如对不同类型的答案进行子类化。

关于java - 填充嵌套 HashMap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21803719/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com