3

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 are of return type String

In this class there is List<K> which I process in the following way:

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

This fails silently and I believe this is just because the exception is not propagated. However, if I use K::methodA everything works.

If I swap the interface declaration of K as such:

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

then the opposite is true; where K::methodB works and k::MethodA does not. It appears that the collector can only see the first interface declared for the generic.

I'm not sure if this is an issue with util.Function or if there is a type issue with the collector.

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

Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
RBI
  • 803
  • 2
  • 14
  • 25
  • @MikeSamuel Both `methodA` and `methodB` return `String`, hence the resulting map's key type being `String`. I will update my question to clarify. Thanks – RBI Jul 10 '15 at 16:36
  • What if you replace `K::methodB` with `p -> p.methodB()`? Any difference? – biziclop Jul 10 '15 at 16:44
  • @biziclop works! So its something in the way the `::` syntax is interpreted? any ideas why? – RBI Jul 10 '15 at 16:48
  • @RBI Not yet, it was just a guess. :) – biziclop Jul 10 '15 at 16:50
  • @biziclop I don't know why I didn't try that earlier. If you want to post an answer that we can expand on the accept is yours! – RBI Jul 10 '15 at 16:58
  • 2
    @RBI Having done some investigation, I think I'll mark this as a duplicate instead. – biziclop Jul 10 '15 at 17:01

0 Answers0