title: Prime number in java 8
body: <p>I was trying to write a simple prime number program in Java 8. Below is the program. I wanted to reduce the code in <code>isPrime()</code> as well. Is there something that filters the elements from <code>2</code> to <code>n/2</code>, and then apply filter for <code>n%i == 0</code> which would make <code>isPrime</code> irrelevant?</p>
  
  <pre><code>import static java.util.stream.Collectors.toList;
  
  import java.util.Arrays;
  import java.util.List;
  import java.util.function.Predicate;
  
  public class Stream1 {
      public static void main(String[] args) {
          List&lt;Integer&gt; numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 20);
          // Prime number 
          System.out.println(numbers.stream()
                               .filter(Stream1::isPrime)
                               .collect(toList()));
      }
  
      public static boolean isPrime(int number) {
          for (int i = 2; i &lt;= number / 2; i++) {
              if (number % i == 0) {
                  return false;
              }
          }
          return true;
      }
  }
  </code></pre>