Java 8 Stream - 如何用要查找的项目列表返回替换字符串内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24011597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java 8 Stream - How to return replace a strings contents with a list of items to find
提问by SteveG
I wish to replace the code below using java8 .stream() or .foreach(). However I am having trouble doing this.
我希望使用 java8 .stream() 或 .foreach() 替换下面的代码。但是,我在这样做时遇到了麻烦。
Its probably very easy, but I'm finding the functional way of thinking a struggle :)
它可能很容易,但我正在寻找一种思考斗争的功能性方式:)
I can iterate, no problem but the but returning the modified string is the issue due to mutability issues.
我可以迭代,没问题,但是由于可变性问题,返回修改后的字符串是问题。
Anyone have any ideas ?
谁有想法 ?
List<String> toRemove = Arrays.asList("1", "2", "3");
String text = "Hello 1 2 3";
for(String item : toRemove){
text = text.replaceAll(item,EMPTY);
}
Thanks !
谢谢 !
采纳答案by Steve K
Wow, you guys like doing things the hard way. this is what filter() and collect() are for.
哇,你们喜欢用艰难的方式做事。这就是 filter() 和 collect() 的用途。
List<String> toRemove = Arrays.asList("1", "2", "3");
String text = "Hello 1 2 3";
text = Pattern.compile("").splitAsStream(text)
.filter(s -> !toRemove.contains(s))
.collect(Collectors.joining());
System.out.println("\"" + text + "\"");
outputs (as the original code did)
输出(如原始代码所做的那样)
"Hello "
Of course, if your search strings are longer than one character, the previous method works better. If you have a tokenized string, though, split and join is easier.
当然,如果您的搜索字符串长度超过一个字符,则前一种方法效果更好。但是,如果您有一个标记化的字符串,拆分和连接会更容易。
List<String> toRemove = Arrays.asList("12", "23", "34");
String text = "Hello 12 23 34 55";
String delimiter = " ";
text = Pattern.compile(delimiter).splitAsStream(text)
.filter(s -> !toRemove.contains(s))
.collect(Collectors.joining(delimiter));
System.out.println("\"" + text + "\"");
outputs
产出
"Hello 55"
回答by Michael
If I understood you correctly, you want to do something like this:
如果我理解正确,你想做这样的事情:
toRemove.forEach(removeString -> {
text = text.replaceAll(removeString, "");
});
The only problem is, that you can't. :(
唯一的问题是,你不能。:(
You can read about it here: http://javarevisited.blogspot.co.il/2014/02/10-example-of-lambda-expressions-in-java8.html
你可以在这里阅读:http: //javarevisited.blogspot.co.il/2014/02/10-example-of-lambda-expressions-in-java8.html
Section 6: One restriction with lambda expression is that, you can only reference either final or effectively final local variables, which means you cannot modified a variable declared in the outer scope inside a lambda.
第 6 节: One restriction with lambda expression is that, you can only reference either final or effectively final local variables, which means you cannot modified a variable declared in the outer scope inside a lambda.
EDIT
编辑
You can do something very ugly. Like this:
你可以做一些非常丑陋的事情。像这样:
private static String text;
public void main (String[] args) {
text = "Hello 1 2 3";
List<String> toRemove = Arrays.asList("1", "2", "3");
toRemove.forEach(removeString -> replaceTextWithEmptyString(removeString));
}
private static void replaceTextWithEmptyString(String whatToReplace) {
text = text.replaceAll(whatToReplace, "");
}
回答by Holger
Since you can't use the stream to modify the text
variable you have to coerce the operation into one Function
which you can apply to the text
to get the final result:
由于您不能使用流来修改text
变量,因此您必须将操作强制转换为Function
可以应用于 的操作text
以获得最终结果:
List<String> toRemove = Arrays.asList("1", "2", "3");
String text = "Hello 1 2 3";
text=toRemove.stream()
.map(toRem-> (Function<String,String>)s->s.replaceAll(toRem, ""))
.reduce(Function.identity(), Function::andThen)
.apply(text);
回答by yuba
text = toRemove.stream()
.reduce(text, (str, toRem) -> str.replaceAll(toRem, ""));
would work for you.
会为你工作。