title: How to avoid "incompatible parameter types in lambda expression" when adding to an ArrayList?
body: <p>I have the following code</p>
  
  <pre><code>public static List&lt;Integer&gt; topKFrequent(int[] nums, int k) {
    List&lt;Integer&gt; myList = new ArrayList&lt;&gt;();
    HashMap&lt;Integer, Integer&gt; map = new HashMap&lt;&gt;();
  
    for (int n : nums) {
      if (!map.containsKey(n)) map.put(n, 1);
      else map.put(n, map.get(n) + 1);
    }
  
    map.entrySet().stream()
      .sorted(Map.Entry.&lt;Integer, Integer&gt;comparingByValue().reversed())
      .limit(k)
      .forEach((key, value) -&gt; myList.add(key));
  
    return myList;
  }
  </code></pre>
  
  <p>The <code>forEach</code> throws the error</p>
  
  <pre><code>Error:(20, 16) java: incompatible types: incompatible parameter types in lambda expression
  </code></pre>
  
  <p>How can I fix/avoid this error? I'm not quite sure how to apply the answer here that explains the problem: <a href="https://stackoverflow.com/questions/22588518/lambda-expression-and-generic-method">Lambda Expression and generic method</a></p>
  
  <p>Edit:</p>
  
  <p>Given the answer, the correction is to replace the lambda inside the forEach with</p>
  
  <pre><code>.forEach((entry) -&gt; myList.add(entry.getKey()));
  </code></pre>