title: Casting Map<Integer, Set<Object>>
body: <p>I have a method that takes a <code>Map&lt;Integer, Set&lt;Object&gt;&gt;</code> as parameter. I need to call it from two different locations, using a <code>Map&lt;Integer, Set&lt;String&gt;&gt;</code> and a <code>Map&lt;Integer, Set&lt;Integer&gt;&gt;</code> as parameter.</p>
  
  <p>The compiler complaint so I changed the method parameter signature to <code>Map&lt;Integer, ?&gt;</code>, and now I can call it, but have different problems. The method is basically as follows:</p>
  
  <pre><code>private void methodA (Map&lt;Integer, ?&gt; inOutMap, Integer key, Object value) {
  
          Set&lt;Object&gt; list = new HashSet&lt;Object&gt;();
  
          if (!inOutMap.containsKey(key)) {
              list.add(value);
          } else {
              list = (Set&lt;Object&gt;) (Set&lt;?&gt;) inOutMap.get(key); //I wrote the cast, but looks quite ugly
              list.add(value);
          }
  
          inOutMap.put(key, list); //compiler error
          //The method put(Integer, capture#4-of ?) in the type Map&lt;Integer,capture#4-of ?&gt; is not applicable for the arguments (Integer, Set&lt;Object&gt;)
      }
  </code></pre>
  
  <p>Is there any way of solving the compiler error? This is, casting <code>list</code> to <code>?</code>.</p>
  
  <p>My second question is conceptual. Is there any better way of doing this besides writting two different methods with different parameters signature?</p>