0

i have a list of urls and i am opening each line in a browser and then collecting some values into HashMap. When i print the values collected in HashMap, i see that it repeats every previous value. what am i doing wrong?

here is what the url look like:



and the is what the HashMap print looks like:


  • test1111 | file available
  • test2222 | file available
  • test1111 | file available
  • test2222 | file available
  • test3333 | file not available
  • test1111 | file available
  • test2222 | file available
  • test4444 | file available
  • test3333 | file not available
  • test1111 | file available
  • test2222 | file available
  • test4444 | file available
  • test5555 | file available
  • test3333 | file not available
  • test1111 | file available
  • test2222 | file available
  • test4444 | file available
  • test5555 | file available
  • test6666 | file available
  • test3333 | file not available
  • test7777 | file not available

here is my entire code:

public class Access_file{   
    
    public static void readFile() throws IOException {
        HashMap<String, String> values = new HashMap<String, String>();
        CSVReader reader = null;        
        System.setProperty("webdriver.chrome.driver", "chromedriver");
        WebDriver driver = new ChromeDriver();

        try  {  
            reader = new CSVReader(new FileReader("x_url.csv"), ',');
            String[] nextLine;
            while((nextLine = reader.readNext()) != null) {
                for(String line : nextLine) {
                    driver.get(line);
                    String[] url_split = line.split("/");
                     String test_url = url_split[4];
                     String availability = driver.findElement(By.cssSelector("#mySelector']")).getText();
                     values.put(test_url, availability);                     
                 }  
                for(String i : values.keySet()) {
                     System.out.println(i + "|"+ values.get(i));
                 } 
        }                       
    }
        catch (NoSuchElementException e) {
            e.printStackTrace();
        }
        finally {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
e-Tester
  • 53
  • 5

2 Answers2

0

You have:

for(String i : values.keySet()) {
  System.out.println(i + "|" + values.get(i));
}

which seems to be a for statement to do exactly what your output shows: for every element of the var values, it is printing a number, a vertical bar, and the value. You do that after every line you read. What did you expect it to do that it's not doing?

arcy
  • 12,845
  • 12
  • 58
  • 103
0

The problem is not the HashMap: as per Map's contract, it keeps only one copy of each key.

The problem is the point in your program when you print the map: you do it inside the loop that keeps adding new entries to the map. That is why older entries get printed multiple times.

Move the for loop outside the while loop to fix this problem.

A very useful habit for avoiding such problems in the future is keeping your indentation tidy. This way you would immediately see that your for loop is at the wrong level of nesting, which would help you fix the problem, or even avoid introducing it in the first place.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523