Я пытаюсь получить HashMap хранить строку и ArrayList<String>.
Основном то, что я хочу для первой строки должно быть имя, а ArrayList содержит все страны, пользователь уже был.HashMap <String, 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 не хранит ключ/значения по Кстати, я тоже хочу?
Необходимо предоставить остальную часть кода. – archie