Skip to content
Advertisement

Can Functional Interfaces with generic type be stored in a static map?

I have used a static map to store a series of Functional Interfaces which associated with three classes A, B and C all inherited from a parent class. The code runs smoothly as follow:

JavaScript

However, there is a warning “Raw use of parameterized class ‘BiConsumer'” on the usage of Biconsumer in the static map. I tried various ways to add a parameterized type to ‘BiConsumer’ but unsuccessfully. Such as:

JavaScript

So my question is: Can Functional Interfaces with generic type be stored in a static map? If the answer is no, using Functional Interfaces directly, is it the only way to use Functional Interfaces in a static map? Are there more proper implementations? Thank you very much for any help.

Advertisement

Answer

Can Functional Interfaces with generic type be stored in a static map?

Yes, of course. List is easier for sake of demonstration:

JavaScript

However, there is a warning “Raw use of parameterized class ‘BiConsumer'”

Just use a wildcard instead of a particular parameter.

JavaScript
Advertisement