title: Java: Bounded heterogeneous collections when items are not related
body: <p>If i have heterogeneous collection for which I know exactly the types i'm going to place is there a way to enforce this.</p>
  
  <p>For example take this scenario say i have a map that has a String key and value which can be on of three unrelated types. Now I know that I will only put ClassA and ClassB or java.lang.String </p>
  
  <p>for example here is the code</p>
  
  <pre><code>public HetroCollection
  {
      public Map&lt;String,Object&gt; values;
  }
  
  public ClassA
  {
  }
  
  public ClassB
  {
  }
  
  public static void Main(String args[])
  {
     HetroCollection collection = new HetroCollection();
     collection.values.add("first", new ClassA());
     collections.values.add("second", new ClassB());
     collections.values.add("third" , "someString");
     //BAD want to stop random adds
     collections.values.("fourth" , new SomeRandomClass());
  }
  </code></pre>
  
  <p>The Options I have thought of are:</p>
  
  <ul>
  <li><p>have the classes implement a common interface and use Generics on the Map (Problem with this is if this also involves library classes either JDK or third party then changing class is not an option</p></li>
  <li><p>hide the Map and provide put Methods which are paratemized like</p>
  
  <p>put(String key , ClassA value);
  put(String key , ClassB value);
  put(String key, String value);
  get(String key);</p></li>
  <li><p>Rethink design and not use heterogeneous collection (not sure how I would represent this any other way)</p></li>
  </ul>
  
  <p>Looking for the best practice answer for this.</p>