[英]Cannot resolve method startsWith by using anyMatch
Can some one tell me please what is wrong here. 有人可以告诉我这是怎么回事。 My code is fine until I added this portion of code if (!tmp.isEmpty()) { return e.isEmpty(); 我的代码很好,直到我添加了这部分代码,如果(!tmp.isEmpty()){return e.isEmpty(); } The error is: Cannot resolve method startsWith(java.lang.String) 错误是:无法解析方法startsWith(java.lang.String)
@Test
public void TestData() {
ArrayList<String> rootOpts = new ArrayList<String>();
rootOpts.add("aa");
rootOpts.add("bb");
rootOpts.add("ac");
ArrayList<String> allSiblings = new ArrayList<String>();
allSiblings.add("aa");
allSiblings.add("ac");
allSiblings.add("abc");
System.out.println("allMatch " + rootOpts.stream()
.map((e) -> {
System.out.println("e = " + e);
List<String> tmp = new ArrayList<String>();
tmp.addAll(allSiblings);
String[] CHs = {"ab","aa","ac"};
for (String chh : CHs) {
tmp.remove(chh);
}
if (!tmp.isEmpty()) {
return e.isEmpty();
}
return e;
})
.anyMatch(v -> v.startsWith("a")));
}
I am trying to rewrite the following code below(this code is contained in a method that is supposed to return a boolean value true or false): 我正在尝试重写以下代码(此代码包含在应该返回布尔值true或false的方法中):
for (Option e : rootOpts) {
List<String> tmp = new ArrayList<String>();
tmp.addAll(allSiblings);
if (e.getData() != null && !e.getData().getString().isEmpty()) {
String[] chs = {"ab","aa","ac"};
for (String ch : chs) {
tmp.remove(ch);
}
} else {
return false;
}
if (!tmp.isEmpty()) {
return false;
}
}
return true;
Thank you for your help guys 谢谢您的帮助
e.isEmpty()
returns a boolean
. e.isEmpty()
返回一个boolean
。 In the following method anyMatch
you want to invoke the method startsWith
on this boolean
but this method does not exist on boolean
. 在下面的方法anyMatch
要调用的方法startsWith
这个boolean
但是这种方法不上不存在boolean
。 So change your code to: 因此,将您的代码更改为:
if (!tmp.isEmpty()) {
return ""; //or whatever string makes sense
}
e
is of type String
while e.isEmpty()
is of type Boolean
. e
是String
类型,而e.isEmpty()
是Boolean
类型。
Therefore the return type of your function is Object
. 因此,函数的返回类型为Object
。
Finally, Object
does not have a startsWith
function, contrary to the original String
type that was returned which is why the compiler is complaining. 最后, Object
没有一个startsWith
函数,这与返回的原始String
类型相反,这就是编译器抱怨的原因。
Look at the return type of isEmpty()
- it's boolean
. 查看isEmpty()
的返回类型- boolean
。 How are you planning to do startsWith
on a boolean true/false
? 您打算如何在boolean true/false
上执行startsWith
? :) Stream predicts that it's possible to get boolean, and thus it cannot let you do startsWith
on it. :) Stream预测有可能获得布尔值,因此它不能让您对它进行startsWith
。
if (!tmp.isEmpty()) {
return e.isEmpty();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.