You cannot do exactly what you want - explicit parameters are not allowed in method references.
But you could ...
... create a method that returns a boolean and passes a call to getAttribute("style") :
public boolean getAttribute(final T t) { return t.getAttribute("style"); }
This will allow you to use the ref method:
int a = (int) blogImagesList.stream() .map(this::getAttribute) .filter(s -> s.contains(imageSrc)) .count();
... or you can define a variable to hold the function:
final Function<T, R> mapper = t -> t.getAttribute("style");
This will allow you to simply pass the variable
int a = (int) blogImagesList.stream() .map(mapper) .filter(s -> s.contains(imageSrc)) .count();
... or you could curry and combine the above two approaches (this is certainly terribly overwhelming)
public Function<T,R> toAttributeExtractor(String attrName) { return t -> t.getAttribute(attrName); }
Then you need to call toAttributeExtractor to get Function and pass it to map :
final Function<T, R> mapper = toAttributeExtractor("style"); int a = (int) blogImagesList.stream() .map(mapper) .filter(s -> s.contains(imageSrc)) .count();
Although realistic, just using a lambda would be easier (as you do on the next line):
int a = (int) blogImagesList.stream() .map(t -> t.getAttribute("style")) .filter(s -> s.contains(imageSrc)) .count();
Boris the Spider
source share