Is this possible to do using plain BiFunction in method definition

Viewed 2230

I had the following problem: a method should take in a bi-function that takes 2 arguments - one is of type Collection<T> and the other is T; the actual function could be actually Collection::remove or Collection::add, or a more complex operation; the actual function is used for more than a dozen of collection and there are several types of values and collections in the function.

Initially there was no genericity - there were just Collection<String>s and elements were Strings so declaring the argument as BiFunction<Collection<String>, String, Boolean> worked just well:

List<String> idCodes;
void visitElement(Element e, 
                  BiFunction<Collection<String>, String, Boolean> elementOp) {
    elementOp.apply(idCodes, e.getIdCode());
}

However then I added other types of collections too, and found that I could no longer find out how to use BiFunction generically:

List<String> idCodes;
List<Integer> weights;

void visitElement(Element e, 
                  BiFunction<...> elementOp) {
    elementOp.apply(idCodes, e.getIdCode());
    elementOp.apply(weights, e.getWeight());
}

but failed - I could just get compile errors in one place or another no matter what I used for type parameters it would fail.

One of my attempts was

<T> void visitElement(Element e, 
                      BiFunction<Collection<T>, T, Boolean> elementOp) 

would fail not when passing in Collection::add but when actually applying the function to Collection<String> and String; or Collection<Integer> and int.

Then I made another interface:

interface ElementOp {
    <T> boolean apply(Collection<T> collection, T item);
}

And not so surprisingly this now works exactly as I wanted. Therefore my question is:

Do I really have to use

interface ElementOp {
    <T> boolean apply(Collection<T> collection, T item);
}

void visitElement(Element e, 
                  ElementOp elementOp) {
    elementOp.apply(idCodes, e.getIdCode());
    elementOp.apply(weights, e.getWeight());
}

or would it be somehow possible to use BiFunction for this case?

P.S. I am using Eclipse 4.3 Mars Java compiler, so it might just be that this might not work because of a some bug there.

2 Answers
Related