简体   繁体   中英

My arraylist is only outputting the last value

I created a HashMap to store a text file with the columns of information. I compared the key to a specific name and stored the values of the HashMap into an ArrayList. When I try to println my ArrayList, it only outputs the last value and leaves out all the other values that match that key.

This isn't my entire code just my two loops that read in the text file, stores into the HashMap and then into the ArrayList. I know it has something to do with my loops.

Did some editing and got it to output, but all my values are displayed multiple times.

My output looks like this.
North America: [ Anguilla, Anguilla, Antigua and Barbuda, Antigua and Barbuda, Antigua and Barbuda, Aruba, Aruba, Aruba,

HashMap<String, String> both = new HashMap<String, String>();
    ArrayList<String> sort = new ArrayList<String>();
    //ArrayList<String> sort2 = new ArrayList<String>();



    // We need a try catch block so we can handle any potential IO errors
    try {
    try {
        inputStream = new BufferedReader(new FileReader(filePath));
        String lineContent = null;
    // Loop will iterate over each line within the file.
    // It will stop when no new lines are found.
    while ((lineContent = inputStream.readLine()) != null) {
        String column[]= lineContent.split(","); 
        both.put(column[0], column[1]);


        Set set = both.entrySet(); 
        //Get an iterator 
        Iterator i = set.iterator(); 
        // Display elements 

        while(i.hasNext()) { 
        Map.Entry me = (Map.Entry)i.next();

        if(me.getKey().equals("North America"))
        {   
            String value= (String) me.getValue();
            sort.add(value);

        }

    }


    }
    System.out.println("North America:");
    System.out.println(sort);
    System.out.println("\n");


    }

Map keys need to be unique. Your code is working according to spec.

if you need to have many values for a key, you may use

 Map<key,List<T>>

here T is String (not only list you can use any collection)

Some things seems wrong with your code :

  • you are iterating on the Map EntrySet to get just one value (you could just use the following code :

     if (both.containsKey("North America")) sort.add(both.get("North America")); 
  • it seems that you can have "North America" more than one time in your input file, but you are storing it in a Map , so each time you store a new value for "North America" in your Map , it will overwrite the current value

  • I don't know what the type of sort is, but what is printed by System.out.print(sort); is dependent of the toString() implementation of this type, and the fact that you use print() instead of println() may also create problems depending on how you run your program (some shells may not print the last value for instance).

If you want more help, you may want to provide us with the following things :

  • sample of the input file

  • declaration of sort

  • sample of output

  • what you want to obtain.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM