英文:
Function working with stream of string does not work with a single string?
问题
在 MyService
内部,我有以下的指令,用于生成一个以分号分隔的用户电子邮件字符串:
users.stream()
.map(UserEntity::getEmail)
.filter(mailValidator.isValid())
.collect(Collectors.joining(";"));
MailValidator
通过 Spring 被注入到了 MyService
中。以下是它的定义:
@Service
public class MailValidator {
private final Pattern mailRegex = Pattern.compile(/** 一些正则表达式 */);
public Predicate<String> isValid() {
return mailRegex.asPredicate();
}
}
一切工作正常。但上述的 MailValidator
在该用例之外似乎没有用途。
更准确地说:我可以在字符串流上使用 MailValidator
。但我如何使其在一个 String
本身上工作呢?
英文:
Inside MyService
, I have the following instruction that produces a semicolon-delimited string of users' emails:
users.stream()
.map(UserEntity::getEmail)
.filter(mailValidator.isValid())
.collect(Collectors.joining(";"));
MailValidator
is injected to MyService
using Spring. This is its definition:
@Service
public class MailValidator {
private final Pattern mailRegex = Pattern.compile(/** some regular expression */);
public Predicate<String> isValid() {
return mailRegex.asPredicate();
}
}
Everything works fine. But the above MailValidator
is not useful outside that use-case, I think.
More precisely: I am able to use MailValidator
on a stream of strings. How can I make it work on a String
itself, though?
答案1
得分: 4
你的isValid()
方法返回一个实现了Predicate
接口的对象。如果你查看 Predicate 的Javadoc,它有一个test(T t)
方法。你所需要做的就是在输入字符串上调用该方法。最终的代码可能如下所示:
String myStringVariable = "test";
boolean isValid = mailValidator.isValid().test(myStringVariable);
英文:
Your isValid()
method returns some object that implements Predicate
interface. If you check Javadocs for Predicate it has a test(T t)
method. All you have to do is call that method on your input String. In the end it might look like so:
String myStringVariable = "test";
boolean isValid = mailValidator.isValid().test(myStringVariable);
答案2
得分: 3
以下是翻译好的内容:
你可以将签名更改为接受一个字符串参数并返回一个布尔值。
class MailValidator {
private final Pattern mailRegex = Pattern.compile("");
public boolean isValid(String s) {
return mailRegex.matcher(s).find();
}
}
并且你可以在流中使用方法引用来应用它:
users.stream()
.map(UserEntity::getEmail)
.filter(mailValidator::isValid)
或者如果你愿意在方法的名称上做出妥协,你可以让验证器实现 Predicate:
class MailValidator implements Predicate<String> {
private final Pattern mailRegex = Pattern.compile("");
@Override
public boolean test(String s) {
return mailRegex.matcher(s).find();
}
}
然后只需执行:
users.stream()
.map(UserEntity::getEmail)
.filter(mailValidator)
在这种情况下,你可以在其他上下文中调用:
new MailValidator().test("foo");
英文:
You can change the signature to String argument with a boolean return
class MailValidator {
private final Pattern mailRegex = Pattern.compile("");
public boolean isValid(String s) {
return mailRegex.matcher(s).find();
}
}
and use it in your stream with a method reference:
users.stream()
.map(UserEntity::getEmail)
.filter(mailValidator::isValid)
Or if you are willing to compromise on the name of the method, you can make the validator implement Predicate
class MailValidator implements Predicate<String> {
private final Pattern mailRegex = Pattern.compile("");
@Override
public boolean test(String s) {
return mailRegex.matcher(s).find();
}
}
then just do
users.stream()
.map(UserEntity::getEmail)
.filter(mailValidator)
Outside of this context, you would be able to call
new MailValidator().test("foo")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论