title: Java/Groovy: Create and read a list of two objects
body: <p>How do I define and iterate over a list of two objects (Foo, Bar)?</p>
  
  <p>What I tried:</p>
  
  <pre><code>// create
  def fooBar = new ArrayList&lt;Foo, Bar&gt;()
  fooBar.add(new Foo())
  fooBar.add(new Bar())
  fooBar.add(new Foo()) // &lt;&lt; shouldn't be allowed or overwrite first foo
  
  def fooBars = new ArrayList&lt;List&lt;Foo, Bar&gt;&gt;()
  fooBars &lt;&lt; fooBar
  
  // read
  fooBars.each { fooBar -&gt;
    def foo = fooBar[0]
    def bar = fooBar[1]
    // do something with foo and bar
  }
  </code></pre>
  
  <p>What I want:</p>
  
  <pre><code>// pseudo code
  def fooBar = new ???&lt;Foo, Bar&gt;()
  fooBar.set(new Foo())
  fooBar.set(new Bar())
  
  def fooBars = new ArrayList&lt;???&gt;()
  fooBars &lt;&lt; fooBar
  
  fooBars.each { fooBar -&gt;
    def foo = fooBar[???] // 0, 'foo', ..?
    def bar = fooBar[???]
    // do something with foo and bar
  }
  </code></pre>
  
  <p>If creating my own FooBar class is the solution I'm fine, I just hope there is a more simple solution.</p>