i have a following code:
@Override
public String parsePrice(Document document) {
Elements metaElements = document.getElementsByTag("meta");
for (Element tag : metaElements) {
String content = tag.attr("content");
String item = tag.attr("itemprop");
if ("price".equals(item)) {
return content.equals("0") ? "Free" : content;
}
}
return "Information not available";
}
It will return a price like "7,49$". I want to replace this code with java 8 features. I'm newbie with streams, but tried:
metaElements.stream().filter(tag -> "price".equals(tag.attr("itemprop")))
.findFirst().orElse(null);
But it returns <meta itemprop="price" content="7,49$">
I can't filter like this (missing return statement):
metaElements.stream().filter(tag -> {
String content = tag.attr("content");
String item = tag.attr("itemprop");
if ("price".equals(item)) {
return content.equals("0") ? "Free" : content;
}
}).findFirst().orElse(null);
How to fix it?