具有泛型类型的功能接口可以存储在静态映射中吗?

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

我使用静态映射来存储一系列功能接口,这些接口与三个 classA、B 和 C 相关联,它们都继承自父 class。代码运行流畅如下:

private static final Map<Class<?>, BiConsumer> maps = Maps.newConcurrentMap();

@PostConstruct
public void init() {
    maps.put(A.class, BiConsumerA);
    maps.put(B.class, BiConsumerB);
    maps.put(C.class, BiConsumerC);
}

private static BiConsumer<A, String> BiComsumerA = (a, string) -> {//do something}
private static BiConsumer<B, String> BiComsumerB = (b, string) -> {//do something}
private static BiConsumer<C, String> BiComsumerC = (c, string) -> {//do something}

public static<T extends Parent> void exec(T t, String string) {
    maps.get(t.getClass()).accept(t, map);
}

但是,在静态地图中使用Biconsumer 时有警告“Raw use of parameterized class 'BiConsumer'”。我尝试了多种方法将参数化类型添加到 'BiConsumer' 但没有成功。如:

private static final Map<Class<?>, BiConsumer<?, String>> maps = Maps.newConcurrentMap();
private static final Map<Class<?>, BiConsumer<? extends Parent, String>> maps = Maps.newConcurrentMap();
private static final Map<Class<?>, BiConsumer<T, String>> maps = Maps.newConcurrentMap();
private static final Map<Class<?>, BiConsumer<T extends Parent, String>> maps = Maps.newConcurrentMap();

所以我的问题是:具有泛型类型的功能接口可以存储在静态映射中吗?如果答案是否定的,直接使用Functional Interfaces,是否只能在静态映射中使用Functional Interfaces?有没有更合适的实现?非常感谢您的帮助。

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

当然可以。为了演示,列表更容易:

private static List<Consumer<?>> listOfConsumers = new ArrayList<>();

@PostConstruct
public void init2() {
    Consumer<String> stringConsumer = str -> System.out.println(str);
    Consumer<Integer> intConsumer = i -> System.out.println(i);
        
    listOfConsumers.add(stringConsumer);
    listOfConsumers.add(intConsumer);
}

However, there is a warning "Raw use of parameterized class 'BiConsumer'"

只需使用通配符而不是特定参数。

private static final Map<Class<?>, BiConsumer<?, String>> maps = Maps.newConcurrentMap();