Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A few options:</p> <p>1) One constructor that takes both maps and is safe when passed a null.</p> <pre><code>public MyClass( Map&lt;String, Integer&gt; map1, Map&lt;String, String&gt; map2 ) { if ( map1 != null ) { this.map1 = map1; } if ( map2 != null ) { this.map2 = map2; } } </code></pre> <p>2) Setters for each map</p> <pre><code>public MyClass { private Map&lt;String, Integer&gt; map1; private Map&lt;String, String&gt; map2; public void setMap1( Map&lt;String, Integer&gt; map1 ) { this.map1 = map1; } public void setMap1( Map&lt;String, String&gt; map2 ) { this.map2 = map2; } } </code></pre> <p>3) A builder that allows you to differentiate between the maps and constructs the object properly (calling the setters)</p> <pre><code>public MyClass { private Map&lt;String, Integer&gt; map1; private Map&lt;String, String&gt; map2; // pretend you don't want people to be able to swap out the map after construction so you protect the setter here. protected void setMap1( Map&lt;String, Integer&gt; map1 ) { this.map1 = map1; } protected void setMap1( Map&lt;String, String&gt; map2 ) { this.map2 = map2; } // getters for the maps and other properties public static Builder builder() { return new Builder(); } public static class Builder { private Map&lt;String, Integer&gt; map1; private Map&lt;String, String&gt; map2; public Builder withMap1( Map&lt;String, Integer&gt; map ) { map1 = map; return this; } public Builder withMap2( Map&lt;String, String&gt; map ) { map2 = map; return this; } public MyClass build() { MyClass c = new MyClass(); // possibly conditional code that inspects the maps for specific values or validity c.setMap1( map1 ); c.setMap2( map2 ); // initialization of other fields return c; } } public static void main( String[] args ) { // sample usage MyClass instance1 = MyClass.builder().withMap1(myMap1).build(); MyClass instance2 = MyClass.builder().withMap2(myMap2).build(); MyClass instance3 = MyClass.builder().withMap1(myMap1).withMap2(myMap2).build(); } } </code></pre> <p>4) Static factory (as pointed out by Evgeniy Dorofeev below)</p> <pre><code>public MyClass { private Map&lt;String, Integer&gt; map1; private Map&lt;String, String&gt; map2; // other properties private MyClass() {} public static MyClass withMap1(Map&lt;String, Integer&gt; map ) { MyClass c = new MyClass(); c.map1 = map; return c; } public static MyClass withMap2(Map&lt;String, String&gt; map ) { MyClass c = new MyClass(); c.map2 = map; return c; } // getters and setters } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload