3

I am trying to get HashMap store a String and a ArrayList<String>. Basicly what I want is for the first String to be a Name, and the arraylist to contain every country the user have been to.

Example:
{Andrew, [USA, China]}
{Lola, [Sweden, Denmark]}

The problem I meet is whenever the user puts in a country, the country will be stored under both names:

"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.");
    }

Does someone knows why the HashMap is not storing the key/values the way I want too?

1
  • 1
    You need to provide the rest of your code. Commented Nov 14, 2014 at 0:44

2 Answers 2

1

Your code is nota completed so I can't know what is test an thearchive. But let's create an example:

HashMap<String, ArrayList<String>> list = new HashMap<String, ArrayList<String>>();
ArrayList<String> anotherlist = new ArrayList<String>();
anotherlist.add("Brazil");
anotherlist.add("USA");
list.put("Augusto", anotherlist);

I hope it helps.

Sign up to request clarification or add additional context in comments.

1 Comment

The problem with that is that I need to take name and countries from the user through System.in.
1

Usually your problem is due to re-using the same ArrayList for all people, so that all folks share the same Countries, all held by the one single ArrayList. A solution is to create a new ArrayList for each person -- meaning new ArrayList<String> needs to be inside some loop where you create your traveler's name String.

So for example, you could change your code to this:

ArrayList<String> namelist = new ArrayList<>();
ArrayList<String> test = new ArrayList<>();

// ArrayList<String> archive = new ArrayList<>();

HashMap<String, ArrayList<String>> arkivet = new HashMap<>();

(input.equals("New Country")){
    System.out.println("Name of the Person? ");
    String name = in.nextLine();

    archive = ArrayList<String>(); // **********
    arkivet.put(name, archive);  // ************

    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.");
    }

4 Comments

Thanks, will try the code to see where it gets me :)
What does these two lines of code do? "archive = = ArrayList<String>(); // **********", "arkivet.put(name, archive); // ************" Because I just get illegal start of expression when running the program on those lines
@Hablo: typo. This happens because I'm working with code snippets that I can't compile, so I can't have a compiler notify me of when my fat fingers are farking up.
@Hablo: but regardless, don't copy my code, copy my ideas. Re-initialize the ArrayList that holds the country names inside of the loop where you create the traveler.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.