[英]Process the result multiple times using Java Stream
How do I convert to Java Stream the for-loop
part in the following codes? 如何在以下代码中将
for-loop
部分转换为Java Stream?
Map<String, String> attributes = new HashMap() {{
put("header", "Hello, world!");
put("font", "Courier New");
put("fontSize", "14px");
put("color", "#0000ff");
put("content", "hello, world" +
"");
}};
Path templatePath = Paths.get("C:/workspace/spring-boot-demos/something.tpl");
List<String> lines = Files.readAllLines(templatePath).stream()
.filter(Objects::nonNull)
.map(line -> {
String parsedLine = "";
for (int i = 0; i < attributes.size(); i++) {
if (parsedLine.equals("")) parsedLine = this.parse(attributes, line);
else parsedLine = this.parse(attributes, parsedLine);
}
return parsedLine;
})
.collect(Collectors.toList());
public String parse(Map<String, String> attributes, String line) {
return attributes.entrySet().stream()
.filter(attributeMap -> line.indexOf("{{" + attributeMap.getKey() + "}}") != -1)
.map(attributeMap -> {
String attributeKey = attributeMap.getKey();
String attributeValue = attributeMap.getValue();
String newLine = line.replace("{{" + attributeKey + "}}", attributeValue);
return newLine;
})
.findFirst().orElse(line);
}
I find it difficult to implement it in Java 8 Stream
way so that I'm forced to do it old for-loop
construct. 我发现很难以
Java 8 Stream
方式实现它,因此我不得不执行旧的for-loop
构造。 The reason behind this is that for every line of string, there might one or more placeholder (using {{}}
delimiter). 其背后的原因是,对于字符串的每一行,可能会有一个或多个占位符(使用
{{}}
分隔符)。 I have the list ( attributes
, HashMap
) of the value to replace them. 我有值列表(
attributes
, HashMap
)来替换它们。 I want to make sure that before the loop of Stream
leave every line, all of the placeholders must replaced with the value from the Map
. 我想确保在
Stream
循环离开每一行之前,所有占位符都必须替换为Map
的值。 Without doing this, only the first placeholder is being replaced. 无需执行此操作,仅替换第一个占位符。
Thank you. 谢谢。
Update: This is the content of something.tpl
file: 更新:这是
something.tpl
文件的内容:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>HTML Generator</title>
<style>
body {
font-family: "{{font}}";
font-size: {{fontSize}};
color: {{color}};
}
</style>
</head>
<body>
<header>{{header}}</header> {{content}}
<main>{{content}}</main>
<h1>{{header}}</h1>
</body>
</html>
So you want to replace every token in the given List ? 因此,您要替换给定列表中的每个标记吗?
Here's how I would do it with streams : 这是我对流进行的操作:
The String[] pLine
may be was you were missing... 如果您丢失了
String[] pLine
可能是...
package so20190412;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Snippet {
public Snippet() {
}
public static void main(String[] args) {
Map<String, String> attributes = new HashMap<String, String> () {{
put("john", "doe");
put("bruce", "wayne");
}};
List<String> lines = Arrays.asList("My name is {{john}}, not {{bruce}}.", "Hello, {{john}}!", "How are you doing?");
new Snippet().replaceAll(attributes, lines).forEach(System.out::println);
}
public List<String> replaceAll(Map<String, String> attributes, List<String> lines) {
return lines.stream().map(l -> parse(attributes, l)).collect(Collectors.toList());
}
public String parse(Map<String, String> attributes, String line) {
String[] pLine = {line};
attributes.entrySet().stream()
.forEach(attr -> {
String attributeKey = attr.getKey();
String attributeValue = attr.getValue();
String newLine = pLine[0].replace("{{" + attributeKey + "}}", attributeValue);
pLine[0] = newLine;
});
return pLine[0];
}
}
Don't hesitate to ask if you don't understand some part. 不要犹豫,问一下您是否不了解某些内容。
HTH! HTH!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.