In java, how can I work with objects which can be either String or List<String>?
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/9259906/maps-with-multiple-types-of-values-in-java">Maps with multiple types of values in java</a>  </p>
</blockquote>



<p>I have an odd question. Maybe I'm going about this the wrong way, but let's see where this question goes :)</p>

<p>I would like a Map container that contains either Strings or lists of Strings. I want to enforce this rule during construction of this object so that you can't create a map with values that aren't either of those.</p>

<p>e.g.</p>

<pre><code>class Record {
  public Record(String key, Map&lt;String,Object&gt; attrs) {
    // check that attrs only contains Objects which are Strings or List&lt;Strings&gt;
  }
}
</code></pre>

<p>Other ways I have thought of to solve the problem might be...</p>

<p>1)</p>

<pre><code>class Record {
  public Record(String key, Map&lt;String,String&gt; attrs, Map&lt;String,List&lt;String&gt;&gt; multiAttrs) {
    // ...
  }
}
</code></pre>

<p>2)</p>

<pre><code>class Record {
  public Record(String key, Map&lt;String,Value&gt; attrs) {
    // ...
  }
}

class Value {
  // Create some funky class that encapsulates lists.
  // Perhaps returning the only element in the list if the size is 1,
  // but returning the list otherwise
}
</code></pre>

<p>I am not immediately excited at the alternatives, but I'm just putting it there as stuff I've already considered. Really I want the distinction between Strings and List to be transparent to the user of the class.</p>
