繁体   English   中英

在特定情况下使用 Java Stream 映射和过滤响应代码

[英]Using Java Stream in specific case with mapping and filtering response codes

我的问题是关于 JAVA8 的 lambda 和过滤器的使用。 它是通过 Selenium of Java 完成的,用于测试不同的响应代码。

如何以最大可能的方式使用 Lambda 来使用 Streams 转换以下函数?

我想重构的代码如下为 Streams ,Java 8 的 lambda:

        for (int i = 0; i < links.size(); i++) {
        if (!(links.get(i).getAttribute("href") == null) && !(links.get(i).getAttribute("href").equals(""))) {
            // Find HTTP Status-Code
            try {
                statusCode = getResponseCode(links.get(i).getAttribute("href").trim());
            } catch (IOException e) {
                e.printStackTrace();
            }
            // Check broken link
            if (statusCode== 404) {
                System.out.println("Broken of Link# "+i+" "+links.get(i).getAttribute("href"));
            }
            else if (statusCode== 400) {
                System.out.println("Bad Request# "+i+" "+links.get(i).getAttribute("href"));
            }
            else if (statusCode== 401) {
                System.out.println("Unauthorized# "+i+" "+links.get(i).getAttribute("href"));
            }
            else if (statusCode== 403) {
                System.out.println("Forbidden# "+i+" "+links.get(i).getAttribute("href"));
            }
            else if (statusCode== 500) {
                System.out.println("Internal server error# "+i+" "+links.get(i).getAttribute("href"));
            }
        }

    }

我现在所拥有的是:

List<AbstractMap.SimpleImmutableEntry<String,Integer>> variablename =
    links.stream().map(WebElement::getAttribute("href"));

我试图做一些事情,过滤掉不是 500,403,401,400,404 的所有内容,只保留映射或一对类似(linkString,responseCode),但我在如何正确地使用它时遇到了一些麻烦拉姆达?

EDIT1:我并不打算通过流放置所有内容,只是为了在此示例中尽可能多地使用它

如果你一块一块地拿它是相当简单的,所以:

// create a set of codes you want to include
Set<Integer> acceptableCodes = new HashSet<>(Arrays.asList(404, 400, 401, 403, 500)); 

// for (int i = 0; i < links.size(); i++) {
links.stream()

// convert to the href value as that's all we need later on
.map(link -> link.getAttribute("href"))

// filter out anything without a href
//   if (!(links.get(i).getAttribute("href") == null) && !(links.get(i).getAttribute("href").equals(""))) {
.filter(href -> href != null)
.filter(href -> !href.equals(""))

// filter out non-matching status codes
.filter(href -> acceptableCodes.contains(getResponseCode(href))
.map(link -> new LinkWithCode(href , getResponseCode(href))
.collect(toList());

把它放在一起,不加注释,这样你就可以更容易阅读:

Set<Integer> acceptableCodes = new HashSet<>(Arrays.asList(404, 400, 401, 403, 500)); 
links.stream()
    .map(link -> link.getAttribute("href"))
    .filter(href -> href != null)
    .filter(href -> !href.equals(""))
    .filter(href -> acceptableCodes.contains(getResponseCode(href))
    .map(link -> new LinkWithCode(href , getResponseCode(href))
    .collect(toList());

LinkWithCode是您需要创建的一个类来保存链接和状态代码。 这假设getResponseCode不是重量级操作,因为它发生了两次。

警告:我尚未对此进行测试或将其放入 IDE 中,因此您可能需要进行一些整理工作。

暂无
暂无

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

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