title: java efficient concatenation of multiple lists
body: <p>I often find myself writing code that builds a List out of multiple sublists. As a simple example suppose you have a List that needs to be validated to a List. One way to write this (imperatively) would be:</p>
  
  <pre><code>import static com.google.common.collect.Lists.newArrayList;
  
  import java.util.List;
  
  public class Product {
  
      private String name;
      private int height;
  
      public static List&lt;String&gt; validateList(List&lt;Product&gt; products) {
          List&lt;String&gt; result = newArrayList();
          valideListSize(products, result);
          for (Product product : products) {
              product.validate(result);
          }
          return result;
      }
  
      private static void valideListSize(List&lt;Product&gt; products,
              List&lt;String&gt; result) {
          if (products.size() &gt; 1000) {
              result.add("List too large");
          }
      }
  
      private void validate(List&lt;String&gt; result) {
          if (name.length() &gt; 30) {
              result.add("Name contains too many characters");
          }
          if (height &gt; 40) {
              result.add("Product too high");
          }
      }
  
  }
  </code></pre>
  
  <p>However for simplicity/maintenance/reuse I don't like to couple the logic of validating a single Product to adding the list of validation results to another existing list. I also think mutating a parameter can lead to more hard to debug code. So I prefer a more functional style. Currently I would typically write like this (using newArrayList from Google Guava):</p>
  
  <pre><code>import static com.google.common.collect.Lists.newArrayList;
  import static java.util.Collections.emptyList;
  import static java.util.Collections.singletonList;
  
  import java.util.List;
  
  public class Product {
  
      private String name;
      private int height;
  
      public static List&lt;String&gt; validateList(List&lt;Product&gt; products) {
          List&lt;String&gt; result = newArrayList();
          result.addAll(valideListSize(products));
          for (Product product : products) {
              result.addAll(product.validate());
          }
          return result;
      }
  
      private static List&lt;String&gt; valideListSize(List&lt;Product&gt; products) {
          if (products.size() &gt; 1000) {
              return singletonList("List too large");
          }
          return emptyList();
      }
  
      private List&lt;String&gt; validate() {
          List&lt;String&gt; result = newArrayList();
          if (name.length() &gt; 30) {
              result.add("Name contains too many characters");
          }
          if (height &gt; 40) {
              result.add("Product too high");
          }
          return result;
      }
  
  }
  </code></pre>
  
  <p>This creates many small arraylists with a very short lifespan, many of which will typically be empty (no validation errors).
  Will this kind of code be inefficient in practice or is it ok to use this style in a production environment?
  Is there a more efficient way (perhaps using Guava) of doing what I want while still keeping the code clean and easy to read and conform to Java best practices?</p>
  
  <p>What would you as a professional Java developer do?</p>
  
  <p>EDIT: see my answer on first comment why I prefer the second way. I prefer code maintainability/readability over premature optimization, but because I see this kind of pattern a lot in daily programming I am wondering if there is a simple way that is at least as clean.
  What about using some kind of joined list view, e.g. Iterables.concat? Or does that also create many intermediate classes?</p>