title: How do I implement a function to generate perfect numbers using java functional interface?
body: <p>I am trying to implement a function that returns a list of perfect numbers up to the parameter x. I have to implement it using the java functional interface and classes that support the functional-style (stream, filter, map...).</p>
  <p>My function should do the following</p>
  <ul>
  <li><p>Takes a parameter x as the limit and returns a list of perfect numbers</p>
  </li>
  <li><p>In the outermost part, the code would filter a list up to x, using lambda to compare whether n is equal to the result of reducing the innermost filter (foldLeft/reducing from left)</p>
  </li>
  <li><p>The innermost part has a filter with a list up to (n - 1). This filter has a lambda function that checks whether n%x == 0.</p>
  </li>
  </ul>
  <p>Where I am having most trouble is implementing the reduction (foldl) with the result from the innermost filter.</p>
  <p>Here is my code:</p>
  <pre><code>    public static List&lt;Integer&gt; generatePerfectList(int x) {
  
          List&lt;Integer&gt; perfectList = new ArrayList&lt;Integer&gt;();
          
          perfectList = IntStream.range(0, x)
                  // I am also getting an error &quot;Incompatible operand types int and OptionalInt&quot; in the following line
                  .filter(n -&gt; (n == (IntStream.range(0, n-1).filter(x -&gt; (0 == (n % x))).reduce(Integer::sum))))
                  .boxed().collect(Collectors.toList());
          
          return perfectList;
      }
  
  </code></pre>