0

In the below code contains_All hashmap is putting the key and value for each iteration. After for loop I am clicking on next button again putting the name and state in hashmap so my hashmap should contain all the values for every page. But when i am printing the hashmap I am getting 4 lists

  1. The key and value of 1 page.
  2. The key value of 2 pages along with 1 page 3.The key value of 3 page along with 1 and 2
  3. The key value of 4 pages along with previous all

NOw when I am iterating the map I am getting too many values as it is printing 1, 2,3 and4 But I want only the 4 which contains all

public static validate()

{ 
get value();
    while (utils.isElementDisplayed(next)) {

            utils.waitForElement(next, 20);
            utils.click(next);
            getValue();

        }

public getValue{
for (int i = 0; i < allRows.size(); i++) {

            names = ds_name.get(i).getText();
            seName = ds_server.get(i).getText();
            state = ds_state.get(i).getText();
            String names_seName = name_ds.concat("_" + serverName_ds);

            containsAll.put(names_seName, state);

        }

        System.out.println(containsAll);
        Iterator it = containsAll.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry ent = (Map.Entry) it.next();

            System.out.println("-----------------------------------------------------------");
            System.out.println(ent.getKey() + "= " + ent.getValue());
            Reporter.log("-----------------------------------------------------------");
            Reporter.log(ent.getKey() + "=  " + ent.getValue());

        }
}
  • Well first thing that comes to mind is that you keep filling the `containsAll` hashmap without ever removing thing out of it. So first fill it with page 1, then add page 2, and so on. Of course it will keep all the previous values. Solution: clean out the hashmap or make a new one per page. – Fullslack Jul 27 '20 at 07:34
  • @Fullslack.dev how can i remove the things and get only the last one which contains all the values. I dont want to make new Hahmaps for all pages –  Jul 27 '20 at 07:37
  • If you only use the hashmap inside the `for loop`, I would move the declaration inside there as well. On _clearing_ or _removing values_ in hashmap I would take a look at this: https://stackoverflow.com/questions/2811537/is-java-hashmap-clear-and-remove-memory-effective – Fullslack Jul 27 '20 at 07:40
  • @Fullslack.dev I cleared the hashmap but now i get the value of 4 page only But i want the value of all pages together –  Jul 27 '20 at 07:55
  • Oké, I hope I understand the question now (the formulation is terrible with double 3. and a reference to 4. that doesn't exist). So you want to see this printed right `The key value of 4 pages along with previous all`. In order to do that, return to your original code (forget my previous comments) and move the `Iterator it = containsAll.entrySet().iterator();` and subsequent code block out of the `for loop`. Let me know if that works. – Fullslack Jul 27 '20 at 08:54
  • @Fullslack.dev yes it worked. Thankyou so much :-) –  Jul 28 '20 at 12:19
  • Glad it did. Could you accept the answer I provided please? – Fullslack Jul 28 '20 at 12:37

1 Answers1

0

In order to display The key value of 4 pages along with previous all you need to move the displaying code block outside the for loop. In this case it is the block of code starting with Iterator it = containsAll.entrySet().iterator();.

Complete code sample:

public static validate() { 
    get value();
    while (utils.isElementDisplayed(next)) {
        utils.waitForElement(next, 20);
        utils.click(next);
        getValue();
    }

public getValue {
    for (int i = 0; i < allRows.size(); i++) {

        names = ds_name.get(i).getText();
        seName = ds_server.get(i).getText();
        state = ds_state.get(i).getText();
        String names_seName = name_ds.concat("_" + serverName_ds);

        containsAll.put(names_seName, state);
        System.out.println(containsAll);
    }
    
    Iterator it = containsAll.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry ent = (Map.Entry) it.next();
        System.out.println("-----------------------------------------------------------");
        System.out.println(ent.getKey() + "= " + ent.getValue());
        Reporter.log("-----------------------------------------------------------");
        Reporter.log(ent.getKey() + "=  " + ent.getValue());
    }
}

The reason this works and the original code doesn't is because you fill the hashmap and print it as well. In doing so you print page 1, then page 1 & 2, then page 1,2 & 3 and finally page 1,2,3 & 4. Moving the print outside the for loop only prints the finished hashmap.

Fullslack
  • 290
  • 1
  • 2
  • 11