将具有条件的循环转换为流

时间:2018-11-05 14:00:38

标签: java loops java-stream

我正在尝试将几个月前制作的常规循环转换为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());
}

2 个答案:

答案 0 :(得分:0)

我将其转换为带有处理条件的方法的类。将其设为类的原因是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);
        }
    }
}

答案 1 :(得分: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());

您可以最小化助手类并尝试内联该方法:

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

请注意,我什至没有尝试编译此代码。