HashMap – 迭代并且输出被打印了太多次

huangapple go评论76阅读模式
英文:

Hashmap - iterating and getting the ouput printed too many times

问题

在下面的代码中,containsAll 哈希映射在每次迭代中存储键和值。在 for 循环之后,我会点击“下一页”按钮,然后再次将名称和状态放入哈希映射中,因此我的哈希映射应该包含每个页面的所有值。但是,当我打印哈希映射时,我得到了4个列表:

  1. 第1页的键和值。
  2. 第1页和第2页的键和值。
  3. 第1页、第2页和第3页的键和值。
  4. 前面所有页面以及第4页的键和值。

现在,当我迭代这个映射时,我得到了太多的值,因为它打印了1、2、3和4。但我只想要包含所有值的第4个列表。

public static validate() { 
    getValue();
    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());
    }
}
英文:

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 &lt; 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(&quot;_&quot; + 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(&quot;-----------------------------------------------------------&quot;);
			System.out.println(ent.getKey() + &quot;= &quot; + ent.getValue());
			Reporter.log(&quot;-----------------------------------------------------------&quot;);
			Reporter.log(ent.getKey() + &quot;=  &quot; + ent.getValue());

		}
}

答案1

得分: 0

为了显示“同时显示4页的关键值以及之前的所有内容”,您需要将_displaying_代码块移出_for loop_。在这种情况下,需要移动以Iterator it = containsAll.entrySet().iterator();开头的代码块。

完整的代码示例:

public static validate() { 
    获取值();
	while (utils.isElementDisplayed(next)) {
	    utils.waitForElement(next, 20);
		utils.click(next);
		获取值();
    }
}

public 获取值() {
    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());
    }
}

之所以这样可以工作而原始代码不行,是因为您填充了哈希映射并打印了它。这样做会先打印“第一页”,然后是“第1页和第2页”,然后是“第1页、第2页和第3页”,最后是“第1页、第2页、第3页和第4页”。将打印移到_for loop_之外,只会打印最终的哈希映射。

英文:

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 &lt; 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(&quot;_&quot; + 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(&quot;-----------------------------------------------------------&quot;);
	    System.out.println(ent.getKey() + &quot;= &quot; + ent.getValue());
		Reporter.log(&quot;-----------------------------------------------------------&quot;);
		Reporter.log(ent.getKey() + &quot;=  &quot; + 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.

huangapple
  • 本文由 发表于 2020年7月27日 15:27:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63110574.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定