user148095 user148095 - 2 years ago 114
Java Question

Java 8 stream api control output

I have the following code.

List<String> parseAttribValueByTag(String tag, String attrib, List<String> attribName) throws IOException {

List<String> keys = new ArrayList<>();

Document doc = Jsoup.connect(url).get();
Elements inputs = + "[" + attrib + "]");
for (String item : attribName) {
.filter(input -> input.attr("name").contains("__VIEWSTATE"))
return keys;

It gives me the following output

Optional[<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="5ue/NxnSLQ2akzQo5R8wBEZ,,."

I would like to control the output so it only gives me
__VIEWSTATE "5ue/NxnSLQ2akzQo5R8wBEZ,,."

I've tried using
.map(input -> input.attr("value"))
and it gives me

but when I add an additional
.map(input -> input.attr("name"))
to also give me the name I get the following error "Cannot resolve method 'attr(java.lang.String)' ". What is it I'm doing wrong in the code? Is there a way around it?

Answer Source

If you add a .map(input -> input.attr("value")), you convert your Stream to a Stream<String> (assuming that attr returns a String), and String doesn't have an attr method, so the second map call doesn't pass compilation (hence the compilation error - Cannot resolve method 'attr(java.lang.String)').

You could use a single map operation to obtain both attributes. For example :

        .filter(input -> input.attr("name").contains("__VIEWSTATE"))
        .map(input -> input.attr("name") + " " + input.attr("value"))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download