Why it’s hard for programmers to write a program to flatten a list?
As nobody seems to have posted a Java solution yet:<p><pre><code>  import java.util.List;
  import java.util.stream.Collectors;
  import java.util.stream.Stream;
  import static java.util.Arrays.*;
  
  public class Flatten {
  
      public static void main(String[] args) {
          List&lt;Object&gt; nested = asList(1, asList(2, 3), asList(4, asList(5,6)));
          List&lt;?&gt; flat = flatten(nested);
          System.out.println(nested);
          System.out.println(flat);
      }
  
      public static List&lt;?&gt; flatten(List&lt;?&gt; nested) {
          return nested.stream()
                  .flatMap(x -&gt; x instanceof List ? flatten((List&lt;?&gt;) x).stream() : Stream.of(x))
                  .collect(Collectors.toList());
      }
  }

</code></pre>
I would say it&#x27;s a bit of an odd question if you mainly target Java as it&#x27;s very uncommon to encounter such a construct there, alone because the type system can&#x27;t handle it properly.