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.
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. I do not know how to achieve this with streams. this is my attempt to making java streams. 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. 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.
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:
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.