我想让HashMap存储一个字符串和一个ArrayList<String>
。基本上,我想让第一个字符串是名称,并且数组列表包含用户去过的每个国家。
Example:
{Andrew, [USA, China]}
{Lola, [Sweden, Denmark]}
我遇到的问题是,每当用户输入一个国家时,该国家将以两种名称存储:
"The HashMap contains: {Andrew=[USA, China, Sweden, Denmark], Lola=[USA, China, Sweden, Denmark]}"
ArrayList<String> namelist = new ArrayList<>();
ArrayList<String> test = new ArrayList<>();
ArrayList<String> archive = new ArrayList<>();
HashMap<String, ArrayList<String>> thearchive = new HashMap<>();
(input.equals("New Country")){
System.out.println("Name of the Person? ");
String name = in.nextLine();
if(namelist.contains(name)){
System.out.println("Which Country have you visited?");
String country = in.nextLine();
if (archive.contains(country)){
System.out.println("You've already visited this country.");
}
else {
test.add(country);
thearchive.put(name, country);
System.out.println("HashMap Contains: " + thearchive);
}
}
else{
System.out.println("The person does not exist please add first.");
}
有人知道为什么HashMap不能按照我想要的方式存储键/值对吗?