Java Streams() vs Heap.poll() help needed
Anonymous User
50

I got wrong answer using approach 2 while converting heap elements to a list. Rest of the approach is same. Can someone please tell me what is the difference between the two? I used to think both are same.

Question https://leetcode.com/problems/top-k-frequent-words/

Solution 1:

List<String> ans = new ArrayList();
while (!heap.isEmpty()) ans.add(heap.poll());

Solution 2:

List<String> ans = heap.stream().collect(Collectors.toList());

Complete Code: https://editor.mergely.com/FhTbQDIg/

Comments (1)