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
java generics java-8 java-stream


source to share


No one has answered this question yet

See similar questions:

20
LambdaConversionException with generics: JVM error?

or similar:

6170
Is Java "pass-by-reference" or "pass-by-value"?
3799
How do I read / convert an InputStream to a string in Java?
3324
How to generate random integers in a specific range in Java?
3073
How to efficiently iterate over each entry in a Java map?
3044
Making a memory leak with Java
2956
What is the difference between public, secure, batch and private in Java?
2936
When to use LinkedList over ArrayList in Java?
2853
How can I convert String to int in Java?
1027
How do I create a generic array in Java?
874
Java 8 List <V> to Map <K, V>



All Articles
Loading...
X
Show
Funny
Dev
Pics