简体   繁体   English

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

[英]Converting loops with conditions into streams

I'm trying to convert a regular loop I have made a few months ago into java 8 streams I do not have much knowledge about stream since I just started using java 8 a few days ago. 我想一个普通环路我已经在几个月前提出转换成Java 8 streams我没有关于流多少知识,因为我用java 8几天前刚刚起步。

Here is my regular loop that I wanted to recreate into streams 这是我想重新创建为流的常规循环

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;
}

I have started to convert it into java 8 streams but I am stuck at the shouldAdd part. 我已经开始将其转换为Java 8流,但是我被困在shouldAdd部分。 I do not know how to achieve this with streams. 我不知道如何使用流来实现这一目标。 this is my attempt to making java streams. 这是我制作Java流的尝试。 What I don't get is how I could set the boolean part from my original loop. 我没有得到的是如何从原始循环中设置布尔部分。

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

I turned this into a class with a method to handle the conditionals. 我将其转换为带有处理条件的方法的类。 The reason for making this a class is the temp, annotations, and shouldAdd variables, which have to be accessed across the doStuff method. 将其设为类的原因是temp,Annotations和shouldAdd变量,必须通过doStuff方法进行访问。 You need to clean this up a bit still... name doStuff something appropriate, etc. There may be a better way to do this, but it uses streams for what can be done with streams. 您需要稍微清理一下……将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);
        }
    }
}

Create helper class, something like this: 创建助手类,如下所示:

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;
    }
}

then you can write 然后你可以写

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

You can minimize the helper class and try to inline that methods: 您可以最小化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());

Note I did not even try to compile this code. 注意我什至没有尝试编译此代码。

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

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