簡體   English   中英

將具有條件的循環轉換為流

[英]Converting loops with conditions into streams

我想一個普通環路我已經在幾個月前提出轉換成Java 8 streams我沒有關於流多少知識,因為我用java 8幾天前剛剛起步。

這是我想重新創建為流的常規循環

public static List<SmaliAnnotation> getAnnotations(List<String> lines, boolean hasPadding) {
    StringBuilder temp = new StringBuilder();
    List<SmaliAnnotation> annotations = new ArrayList<>();
    boolean shouldAdd = false;
    for (String line : lines) {
        String trim = hasPadding ? line.trim() : line;
        if (trim.isEmpty()) continue;
        if (trim.startsWith(".annotation")) {
            shouldAdd = true;
        }
        if (shouldAdd) {
            temp.append(line).append("\n");
        }
        if (trim.equalsIgnoreCase(".end annotation")) {
            shouldAdd = false;
            annotations.add(new SmaliAnnotation(temp.toString()));
            temp.setLength(0);
        }
    }
    return annotations;
}

我已經開始將其轉換為Java 8流,但是我被困在shouldAdd部分。 我不知道如何使用流來實現這一目標。 這是我制作Java流的嘗試。 我沒有得到的是如何從原始循環中設置布爾部分。

public static List<SmaliAnnotation> getAnnotations(List<String> lines, boolean hasPadding) {
    StringBuilder temp = new StringBuilder();
    boolean shouldAdd = false;
    return lines.stream()
            .filter(str -> str != null && !str.isEmpty())
            .map(line -> hasPadding ? line.trim() : line)
            .map(SmaliAnnotation::new)
            .collect(Collectors.toList());
}

我將其轉換為帶有處理條件的方法的類。 將其設為類的原因是temp,Annotations和shouldAdd變量,必須通過doStuff方法進行訪問。 您需要稍微清理一下……將doStuff命名為適當的名稱,等等。也許有更好的方法可以做到這一點,但是它使用流來完成流的處理。

public class AnnotationBuilder {
    private StringBuilder temp = new StringBuilder();
    private List<SmaliAnnotation> annotations = new ArrayList<>();
    private boolean shouldAdd;

    private AnnotationBuilder() {
        // no-op
    }

    public static List<SmaliAnnotation> getAnnotations(List<String> lines, boolean hasPadding) {
        return new AnnotationBuilder().build(lines, hasPadding);
    }

    private List<SmaliAnnotation> build(List<String> lines, boolean hasPadding) {
        lines.stream().map(line -> hasPadding ? line.trim() : line).filter(line -> !line.isEmpty()).forEach(line -> doStuff(line));
        return annotations;
    }

    private void doStuff(final String cleanLine) {
        if (cleanLine.startsWith(".annotation")) {
            shouldAdd = true;
        }
        if (shouldAdd) {
            temp.append(cleanLine).append("\n");
        }
        if (cleanLine.equalsIgnoreCase(".end annotation")) {
            shouldAdd = false;
            annotations.add(new SmaliAnnotation(temp.toString()));
            temp.setLength(0);
        }
    }
}

創建助手類,如下所示:

class Helper {
    StringBuilder temp = new StringBuilder();
    boolean shouldAdd = false;

    String checkStart(String line) {
       if (line.startsWith(".annotation")) {
          shouldAdd = true;
       }
       if (shouldAdd) {
          temp.append(line).append("\n");
       }
       return line;
   }

   SmaliAnnotation createAnnotation(String trim) {
        shouldAdd = false;
        SmaliAnnotation res = new SmaliAnnotation(temp.toString());
        temp.setLength(0);
        return res;
    }
}

然后你可以寫

StringBuilder temp = new StringBuilder();
Helper helper = new Helper();
return lines.stream()
        .filter(str -> str != null && !str.isEmpty())
        .map(line -> hasPadding ? line.trim() : line)
        .map(helper::checkStart)
        .filter(trim->trim.equalsIgnoreCase(".end annotation"))
        .map(helper::createAnnotation)
        .collect(Collectors.toList());

您可以最小化helper類,並嘗試內聯該方法:

class Helper {
    boolean shouldAdd = false;
}

StringBuilder temp = new StringBuilder    Helper helper = new Helper();
return lines.stream()
        .filter(str -> str != null && !str.isEmpty())
        .map(line -> hasPadding ? line.trim() : line)
        .map((String line) -> {
           if (line.startsWith(".annotation")) {
              helper.shouldAdd = true;
           }
           if (helper.shouldAdd) {
              temp.append(line).append("\n");
           }
           return line;
        })
        .filter(trim->trim.equalsIgnoreCase(".end annotation"))
        .map((String trim) -> {
            helper.shouldAdd = false;
            SmaliAnnotation res = new SmaliAnnotation(temp.toString());
            temp.setLength(0);
            return res;
        })
        .collect(Collectors.toList());

注意我什至沒有嘗試編譯此代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM