title: Java 8 : How to find kth smallest, efficiently
body: <p>In java 8 how do I find the Kth smallest efficiently? Kth smallest is </p>
  
  <p><a href="http://www.geeksforgeeks.org/kth-smallestlargest-element-unsorted-array/" rel="nofollow noreferrer">http://www.geeksforgeeks.org/kth-smallestlargest-element-unsorted-array/</a> </p>
  
  <p>I've attempted below. If there is a <strong>more efficient solution</strong> which provides securities of Java-8 such as I should not have to check size before accessing indices in case of an empty array etc. I believe that my own solution is safe against empty array.</p>
  
  <p>A <strong>provable time complexity comparison</strong> of my Java-8 approach and a better Java-8 approach will be appreciated if there exists one. Thank you.</p>
  
  <pre><code>package lab.rat.jerry.kthsmallest;
  
  import static java.util.stream.Collectors.toList;
  
  import java.util.Arrays;
  import java.util.List;
  
  public class KthSmallest {
  
      // Inputs
      static int k = 3;
  
      static Integer[] myIntArray = {2, 3, 1, 4, -2, 0, -3, 0, -1, 5 };
  
      public static void main(String[] args) {
  
          List&lt;Integer&gt; list = Arrays.asList(myIntArray);
  
          System.out.println(list
              .stream()
              .sorted()
              .distinct()
              .limit(k)
              .skip(k-1)
              .collect(toList())
              );
      }
  }
  </code></pre>