简体   繁体   English

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

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

My question is regarding lambda with JAVA8 and use of the filters.我的问题是关于 JAVA8 的 lambda 和过滤器的使用。 It is done via Selenium of Java for testing for different response codes.它是通过 Selenium of Java 完成的,用于测试不同的响应代码。

How can I use Lambda in greatest possible way to transform following function with Streams?如何以最大可能的方式使用 Lambda 来使用 Streams 转换以下函数?

The code which I wanted to refactor is as follows into Streams , lambda of Java 8:我想重构的代码如下为 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"));
            }
        }

    }

What I have for now is:我现在所拥有的是:

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

I was trying to do something along the lines of filtering out everything that is not 500,403,401,400,404 and only keeping the mapping or a Pair of that like (linkString, responseCode), but I am having a bit of troubles exactly how I could do it properly with Lambda?我试图做一些事情,过滤掉不是 500,403,401,400,404 的所有内容,只保留映射或一对类似(linkString,responseCode),但我在如何正确地使用它时遇到了一些麻烦拉姆达?

EDIT1: I didn't meant to put everything via stream, just to make as much use of it as I can in this example EDIT1:我并不打算通过流放置所有内容,只是为了在此示例中尽可能多地使用它

It's fairly simple if you take it piece by piece, so:如果你一块一块地拿它是相当简单的,所以:

// 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());

Putting it together without the comments so you can read it easier:把它放在一起,不加注释,这样你就可以更容易阅读:

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 is a class you'll need to create to hold the link and the status code. LinkWithCode是您需要创建的一个类来保存链接和状态代码。 This assumes getResponseCode isn't a heavyweight operation as it's happening twice.这假设getResponseCode不是重量级操作,因为它发生了两次。

Caveat: I haven't tested this or put it in an IDE so you might have some tidy up to do.警告:我尚未对此进行测试或将其放入 IDE 中,因此您可能需要进行一些整理工作。

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

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