繁体   English   中英

有没有办法在这里使用 Java 流执行两个映射操作?

[英]Is there a way to perform two mapping operations using Java streams here?

我正在尝试自动化在线游戏,但我被卡住了。

我正在尝试做的事情:我正在尝试分析我的报告。 第一步涉及获取父行。 第二步是根据报表是否被阅读,以及报表是否是今天的日期来过滤父行。

this.driver.findElements(this.reportRow).stream()
            .filter(reportElement -> reportElement.findElement(this.unreadReports).isDisplayed()
                    && reportElement.findElement(this.todaysReport).getText().contains(this.bountyDate))
            .map(reportRow -> {
                String villageName = reportRow.findElement(this.unreadReports).getText().replace("mwldjt raids ", "");
                String bounty = reportRow.findElement(this.bountyElement).getText();
                System.out.println("Village: " + villageName + " Bounty: " + bounty);
                return new String[]{bounty, villageName};
            })
            .forEach(entry -> System.out.println("Entry:" + entry[0] + ", " + entry[1])

这是我要解析的 HTML。

<tr>
    <td class="sel">
        <input class="check" name="n1" type="checkbox" value="14180678">
    </td>
    <td class="sub newMessage">
        <a href="berichte.php?id=14180678&amp;t=1&amp;s=0&amp;page=1&amp;toggleState=0">
            <img alt="unread" class="messageStatus messageStatusUnread" src="img/x.gif">
        </a>
        <img alt="Won as attacker with losses." class="iReport iReport2 " src="img/x.gif">
        <a class="reportInfoIcon" href="build.php?id=39&amp;tt=2&amp;bid=14180678"><img alt="16/100" class="reportInfo carry half"
                                                                                        src="img/x.gif"></a>
        <div class="">
            <a href="berichte.php?id=14180678%7C5fa6b3d7&amp;t=1&amp;s=1">mwldjt raids Natars 36|64</a>
        </div>
        <div class="clear"></div>
    </td>
    <td class="dat">
        24/04/20, 04:08 am
    </td>
</tr>

我正在尝试在 LinkedHashmap 中获取赏金和村庄名称。 我现在已经用打印方法替换了 foreach 块中的“put”。

问题:

我需要执行两个操作——从父行中的两个元素获取数据。 我一直在努力了解如何做到这一点,但现在我认为我需要帮助。 感谢您抽出宝贵时间。

LinkedHashMap进行put操作的forEach可以转换为使用

.collect(Collectors.toMap(<key-fn>, <value-fn>, <merge-fn>, LinkedHashMap::new)

所以你分片的代码可以重构

LinkedHashMap<String, String> output =  this.driver.findElements(this.reportRow).stream()
            .filter(reportElement -> reportElement.findElement(this.unreadReports).isDisplayed()
                    && reportElement.findElement(this.todaysReport).getText().contains(this.bountyDate))
            .map(reportRow -> {
                String villageName = reportRow.findElement(this.unreadReports).getText().replace("mwldjt raids ", "");
                String bounty = reportRow.findElement(this.bountyElement).getText();
                System.out.println("Village: " + villageName + " Bounty: " + bounty);
                return new String[]{bounty, villageName};
            })
            .collect(Collectors.toMap(e -> e[0], e -> e[1], (a,b) -> b, LinkedHashMap::new);

当然,获取村庄名称和赏金的操作可以提取出来并直接放在toMap Collector 中,格式如下:

LinkedHashMap<String, String> output =  this.driver.findElements(this.reportRow).stream()
            .filter(reportElement -> isValidReportElement(reportElement))
            .collect(Collectors.toMap(reportElement -> getVillageName(reportElement), 
                    reportElement -> getBounty(reportElement), (a,b) -> b, LinkedHashMap::new);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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