Java nested wildcard generic won't compile

前端 未结 3 1647
南旧
南旧 2020-12-07 01:20

I have a problem with bounded nested wildcards in Java generics.

Here\'s a common case:

public void doSomething(Set set) {}
         


        
相关标签:
3条回答
  • 2020-12-07 01:48

    So the problem is, doSomething could be implemented as:

    public void doSomething(Map<String, Set<? extends Number>> map) {
        Set<Float> set = ...;
        map.put("xyz", set);
    }
    

    You need to decide what you actually mean.

    Probably something like:

    public void doSomething(Map<String, ? extends Set<? extends Number>> map) {}
    
    0 讨论(0)
  • 2020-12-07 01:54

    this will work for you:

    public void doSomething(Map<String, ? extends Set<? extends Number>> map) {}
    
    0 讨论(0)
  • 2020-12-07 02:04

    To make code to work Create HashMap as:

    Map<String, Set<? extents Number>> map = new HashMap<String, Set<? extents Number>>();
    
    0 讨论(0)
提交回复
热议问题