在 Java 中实例化和初始化 "multi-level" 个地图
Instantiate and initialize "multi-level" maps in Java
我试图在不使用大量 if/elses 的情况下解决 This problem in BeeCrowd。
它包括从使用中读取 3 个输入,例如:
vertebrado
ave
carnivoro
并输出这些输入对应的单词。
aguia
我 python 比 java 了解更多,这将是我在 python 中的解决方案(有效):
dict = {
"vertebrado": {
"ave": {
"carnivoro": "aguia",
"onivoro": "pomba"
},
"mamifero": {
"onivoro": "homem",
"herbivoro": "vaca"
}
},
"invertebrado": {
"inseto": {
"hematofago": "pulga",
"herbivoro": "lagarta"
},
"anelideo": {
"hematofago": "sanguessuga",
"onivoro": "minhoca"
}
}
}
word1 = input()
word2 = input()
word3 = input()
word = dict[word1][word2][word3]
print(word)
但是我正在努力在 Java 中编写一个好的解决方案。这是我当前的 Java 解决方案(也有效):
import java.util.Scanner;
import java.util.HashMap;
public class Main {
public static void main (String[]args) {
Scanner scanner = new Scanner(System.in);
String word1 = new String(scanner.nextLine());
String word2 = new String(scanner.nextLine());
String word3 = new String(scanner.nextLine());
HashMap<String, HashMap<String, HashMap<String, String>>> dict = new HashMap();
dict.put("vertebrado", new HashMap<String, HashMap<String, String>>());
dict.put("invertebrado", new HashMap<String, HashMap<String, String>>());
dict.get("vertebrado").put("ave", new HashMap<String, String>());
dict.get("vertebrado").put("mamifero", new HashMap<String, String>());
dict.get("invertebrado").put("inseto", new HashMap<String, String>());
dict.get("invertebrado").put("anelideo", new HashMap<String, String>());
dict.get("vertebrado").get("ave").put("carnivoro", "aguia");
dict.get("vertebrado").get("ave").put("onivoro", "pomba");
dict.get("vertebrado").get("mamifero").put("onivoro", "homem");
dict.get("vertebrado").get("mamifero").put("herbivoro", "vaca");
dict.get("invertebrado").get("inseto").put("hematofago", "pulga");
dict.get("invertebrado").get("inseto").put("herbivoro", "lagarta");
dict.get("invertebrado").get("anelideo").put("hematofago", "sanguessuga");
dict.get("invertebrado").get("anelideo").put("onivoro", "minhoca");
String word = dict.get(word1).get(word2).get(word3);
System.out.println(word);
}
}
该解决方案的明显问题是,以这种方式填充字典是不切实际的。代码已经很大了。如果dict
有很多值,有很多“深度”,维护起来会很麻烦。
有什么办法可以减少吗? IE,在几行中声明和初始化 dict
?
按照 MC Emperor 的建议使用 Map.of():
Map<String, Map<String, Map<String, String>>> dict = Map.of(
"vertebrado",
Map.of(
"ave",
Map.of(
"carnivoro", "aguia",
"onivoro", "pomba"
),
"mamifero",
Map.of("onivoro","homem",
"herbivoro","vaca"
)
),
"invertebrado",
Map.of(
"inseto",
Map.of(
"hematofago", "pulga",
"herbivoro", "lagarta"
),
"anelideo",
Map.of(
"hematofago","sanguessuga",
"onivoro","minhoca"
)
)
);
与使用 json
文件相比,这还可以让您在进行时进行类型检查,尽管为了真正的类型安全,您需要引入名为 Phylum
的 类、Class
和 Order
来包装你的字符串,而不是留下“字符串类型”的值。
我试图在不使用大量 if/elses 的情况下解决 This problem in BeeCrowd。
它包括从使用中读取 3 个输入,例如:
vertebrado
ave
carnivoro
并输出这些输入对应的单词。
aguia
我 python 比 java 了解更多,这将是我在 python 中的解决方案(有效):
dict = {
"vertebrado": {
"ave": {
"carnivoro": "aguia",
"onivoro": "pomba"
},
"mamifero": {
"onivoro": "homem",
"herbivoro": "vaca"
}
},
"invertebrado": {
"inseto": {
"hematofago": "pulga",
"herbivoro": "lagarta"
},
"anelideo": {
"hematofago": "sanguessuga",
"onivoro": "minhoca"
}
}
}
word1 = input()
word2 = input()
word3 = input()
word = dict[word1][word2][word3]
print(word)
但是我正在努力在 Java 中编写一个好的解决方案。这是我当前的 Java 解决方案(也有效):
import java.util.Scanner;
import java.util.HashMap;
public class Main {
public static void main (String[]args) {
Scanner scanner = new Scanner(System.in);
String word1 = new String(scanner.nextLine());
String word2 = new String(scanner.nextLine());
String word3 = new String(scanner.nextLine());
HashMap<String, HashMap<String, HashMap<String, String>>> dict = new HashMap();
dict.put("vertebrado", new HashMap<String, HashMap<String, String>>());
dict.put("invertebrado", new HashMap<String, HashMap<String, String>>());
dict.get("vertebrado").put("ave", new HashMap<String, String>());
dict.get("vertebrado").put("mamifero", new HashMap<String, String>());
dict.get("invertebrado").put("inseto", new HashMap<String, String>());
dict.get("invertebrado").put("anelideo", new HashMap<String, String>());
dict.get("vertebrado").get("ave").put("carnivoro", "aguia");
dict.get("vertebrado").get("ave").put("onivoro", "pomba");
dict.get("vertebrado").get("mamifero").put("onivoro", "homem");
dict.get("vertebrado").get("mamifero").put("herbivoro", "vaca");
dict.get("invertebrado").get("inseto").put("hematofago", "pulga");
dict.get("invertebrado").get("inseto").put("herbivoro", "lagarta");
dict.get("invertebrado").get("anelideo").put("hematofago", "sanguessuga");
dict.get("invertebrado").get("anelideo").put("onivoro", "minhoca");
String word = dict.get(word1).get(word2).get(word3);
System.out.println(word);
}
}
该解决方案的明显问题是,以这种方式填充字典是不切实际的。代码已经很大了。如果dict
有很多值,有很多“深度”,维护起来会很麻烦。
有什么办法可以减少吗? IE,在几行中声明和初始化 dict
?
按照 MC Emperor 的建议使用 Map.of():
Map<String, Map<String, Map<String, String>>> dict = Map.of(
"vertebrado",
Map.of(
"ave",
Map.of(
"carnivoro", "aguia",
"onivoro", "pomba"
),
"mamifero",
Map.of("onivoro","homem",
"herbivoro","vaca"
)
),
"invertebrado",
Map.of(
"inseto",
Map.of(
"hematofago", "pulga",
"herbivoro", "lagarta"
),
"anelideo",
Map.of(
"hematofago","sanguessuga",
"onivoro","minhoca"
)
)
);
与使用 json
文件相比,这还可以让您在进行时进行类型检查,尽管为了真正的类型安全,您需要引入名为 Phylum
的 类、Class
和 Order
来包装你的字符串,而不是留下“字符串类型”的值。