Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A quick driver of this showed it to work. Not responsible for all test cases. :-)</p> <pre><code>List&lt;String&gt; combineListsHorizontally(List&lt;String&gt; a, List&lt;String&gt; b) { assert a.size() == b.size(); // just avoids some checks List&lt;String&gt; result = new ArrayList&lt;String&gt;(a.size()); Iterator&lt;String&gt; itera = a.iterator(); Iterator&lt;String&gt; iterb = b.iterator(); for(int i = 0; i &lt; a.size(); i++) { String combined = itera.next() + iterb.next(); result.add(combined); } return result; } </code></pre> <p>If you need something generic, you would need to know they ahve a way that they can be joined</p> <pre><code> List&lt;E&gt; combineListsHorizontally(List&lt;E&gt; a, List&lt;E&gt; b) { assert a.size() == b.size(); // just avoids some checks List&lt;E&gt; result = new ArrayList&lt;E&gt;(a.size()); Iterator&lt;E&gt; itera = a.iterator(); Iterator&lt;E&gt; iterb = b.iterator(); for(int i = 0; i &lt; a.size(); i++) { E combined = new MagicCombiner&lt;E&gt;(a,b).get(); // define this line yourself result.add(combined); } return result; } </code></pre> <p>///////////////// EDIT - here's a working example based off @Brents (superior) example. Props to him for illustrating this pattern better than I did.</p> <pre><code>import java.util.*; /** * Compile: "javac BinaryListUtils" * Usage: "java BinaryListUtils" C:\Documents and Settings\user\My Documents&gt;javac BinaryListUtils.java C:\Documents and Settings\user\My Documents&gt;java BinaryListUtils APPLEBANANA PEARORANGE C:\Documents and Settings\user\My Documents&gt; */ // general use transformer class BinaryListUtils { // tranformer static interface BinaryTransformer&lt;X, Y, Z&gt; { Z transform(X a, Y b); } // implementation for your problem static class ConcatTransformer implements BinaryTransformer&lt;String, String, String&gt; { public String transform(String a, String b) { return a + b; } } public static &lt;X, Y, Z&gt; List&lt;Z&gt; collect(List&lt;X&gt; aList, List&lt;Y&gt; bList, BinaryTransformer&lt;X, Y, Z&gt; t) { List&lt;Z&gt; ret = new ArrayList&lt;Z&gt;(aList.size()); Iterator&lt;X&gt; aIter = aList.iterator(); Iterator&lt;Y&gt; bIter = bList.iterator(); while(aIter.hasNext()) { ret.add(t.transform(aIter.next(), bIter.next())); } return ret; } public static void main(String[] args) { List&lt;String&gt; aList = new ArrayList&lt;String&gt;(); List&lt;String&gt; bList = new ArrayList&lt;String&gt;(); aList.add("APPLE"); aList.add("PEAR"); bList.add("BANANA"); bList.add("ORANGE"); ConcatTransformer ct = new ConcatTransformer(); List&lt;String&gt; cList = BinaryListUtils.collect(aList,bList,ct); for(String s : cList) System.out.println(s); } } </code></pre>
 

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