Using a collector in a generic multiinterface - java

I have a class with a generic type:

class MyClass<K extends InterfaceA & InterfaceB>{...}

      

Where InterfaceA

and InterfaceB

have methodA

and methodB

respectively. Both methods have a return typeString

This class has one List<K>

, which I process as follows:

public void setList(List<K> list){
    Map<String,K> map = list
        .stream()
        .collect(Collectors.toMap(K::methodB, p -> p))
}

      

This fails and I believe it only happens because the exception is not propagated. However, if I use K::methodA

, everything works.

If I change the interface declaration K

as such:

class MyClass<K extends InterfaceB & InterfaceA>{...}

      

then the opposite is true; where it K::methodB

works, but K::methodA

not. It looks like the collector can only see the first interface declared to be shared.

I'm not sure if this is the problem with util.Function

or if the picker has a type issue.

How can this be fixed so that any number of interfaces are visible to the collector process?

+3


source to share





All Articles