将带条件的循环转换为流

我正在尝试将我几个月前制作的常规循环转换为 java 8streams我对流的了解不多,因为我几天前才开始使用 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());

}


小唯快跑啊
浏览 121回答 2
2回答

犯罪嫌疑人X

我把它变成了一个带有处理条件的方法的类。将其设为类的原因是 temp、annotations 和 shouldAdd 变量,它们必须通过 doStuff 方法访问。您还需要稍微清理一下……将 doStuff 命名为适当的东西,等等。可能有更好的方法来做到这一点,但它使用流来处理流可以完成的事情。public class AnnotationBuilder {&nbsp; &nbsp; private StringBuilder temp = new StringBuilder();&nbsp; &nbsp; private List<SmaliAnnotation> annotations = new ArrayList<>();&nbsp; &nbsp; private boolean shouldAdd;&nbsp; &nbsp; private AnnotationBuilder() {&nbsp; &nbsp; &nbsp; &nbsp; // no-op&nbsp; &nbsp; }&nbsp; &nbsp; public static List<SmaliAnnotation> getAnnotations(List<String> lines, boolean hasPadding) {&nbsp; &nbsp; &nbsp; &nbsp; return new AnnotationBuilder().build(lines, hasPadding);&nbsp; &nbsp; }&nbsp; &nbsp; private List<SmaliAnnotation> build(List<String> lines, boolean hasPadding) {&nbsp; &nbsp; &nbsp; &nbsp; lines.stream().map(line -> hasPadding ? line.trim() : line).filter(line -> !line.isEmpty()).forEach(line -> doStuff(line));&nbsp; &nbsp; &nbsp; &nbsp; return annotations;&nbsp; &nbsp; }&nbsp; &nbsp; private void doStuff(final String cleanLine) {&nbsp; &nbsp; &nbsp; &nbsp; if (cleanLine.startsWith(".annotation")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; shouldAdd = true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (shouldAdd) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp.append(cleanLine).append("\n");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (cleanLine.equalsIgnoreCase(".end annotation")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; shouldAdd = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; annotations.add(new SmaliAnnotation(temp.toString()));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp.setLength(0);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

慕斯709654

创建助手类,如下所示:class Helper {&nbsp; &nbsp; StringBuilder temp = new StringBuilder();&nbsp; &nbsp; boolean shouldAdd = false;&nbsp; &nbsp; String checkStart(String line) {&nbsp; &nbsp; &nbsp; &nbsp;if (line.startsWith(".annotation")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; shouldAdd = true;&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;if (shouldAdd) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp.append(line).append("\n");&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;return line;&nbsp; &nbsp;}&nbsp; &nbsp;SmaliAnnotation createAnnotation(String trim) {&nbsp; &nbsp; &nbsp; &nbsp; shouldAdd = false;&nbsp; &nbsp; &nbsp; &nbsp; SmaliAnnotation res = new SmaliAnnotation(temp.toString());&nbsp; &nbsp; &nbsp; &nbsp; temp.setLength(0);&nbsp; &nbsp; &nbsp; &nbsp; return res;&nbsp; &nbsp; }}然后你可以写StringBuilder temp = new StringBuilder();Helper helper = new Helper();return lines.stream()&nbsp; &nbsp; &nbsp; &nbsp; .filter(str -> str != null && !str.isEmpty())&nbsp; &nbsp; &nbsp; &nbsp; .map(line -> hasPadding ? line.trim() : line)&nbsp; &nbsp; &nbsp; &nbsp; .map(helper::checkStart)&nbsp; &nbsp; &nbsp; &nbsp; .filter(trim->trim.equalsIgnoreCase(".end annotation"))&nbsp; &nbsp; &nbsp; &nbsp; .map(helper::createAnnotation)&nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toList());您可以最小化辅助类并尝试内联该方法:class Helper {&nbsp; &nbsp; boolean shouldAdd = false;}StringBuilder temp = new StringBuilder&nbsp; &nbsp; Helper helper = new Helper();return lines.stream()&nbsp; &nbsp; &nbsp; &nbsp; .filter(str -> str != null && !str.isEmpty())&nbsp; &nbsp; &nbsp; &nbsp; .map(line -> hasPadding ? line.trim() : line)&nbsp; &nbsp; &nbsp; &nbsp; .map((String line) -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (line.startsWith(".annotation")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; helper.shouldAdd = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (helper.shouldAdd) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp.append(line).append("\n");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return line;&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; .filter(trim->trim.equalsIgnoreCase(".end annotation"))&nbsp; &nbsp; &nbsp; &nbsp; .map((String trim) -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; helper.shouldAdd = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SmaliAnnotation res = new SmaliAnnotation(temp.toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp.setLength(0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return res;&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toList());注意我什至没有尝试编译这段代码。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java