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(); } The error is: Cannot resolve method 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):
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
. In the following method anyMatch
you want to invoke the method startsWith
on this boolean
but this method does not exist on 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
.
Therefore the return type of your function is 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.
Look at the return type of isEmpty()
- it's boolean
. How are you planning to do startsWith
on a boolean true/false
? :) Stream predicts that it's possible to get boolean, and thus it cannot let you do startsWith
on it.
if (!tmp.isEmpty()) {
return e.isEmpty();
}
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.