import java.util.Map;
import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;


public class TopKFrequent {
  public static void main(String[] args) {
    System.out.println(topKFrequent(new int[]{1, 1, 1, 1, 2, 2, 2, 3, 3, 4}, 3));
  }

  public static List<Integer> topKFrequent(int[] nums, int k) {
    List<Integer> myList = new ArrayList<>();
    HashMap<Integer, Integer> map = new HashMap<>();

    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.<Integer, Integer>comparingByValue().reversed())
      .limit(k)
      .forEach((entry) -> myList.add(entry.getKey()));

    return myList;
  }
}

@@ -1,24 +1,49 @@
+import java.util.Arrays;%0A
 import java.util.Map;%0Aim
@@ -62,32 +62,39 @@
 il.List;%0Aimport 
+static 
 java.util.ArrayL
@@ -87,25 +87,143 @@
 va.util.
-ArrayList
+Comparator.reverseOrder;%0Aimport static java.util.Map.Entry.comparingByValue;%0Aimport static java.util.stream.Collectors.counting
 ;%0Aimport
@@ -215,32 +215,39 @@
 ounting;%0Aimport 
+static 
 java.util.HashMa
@@ -244,17 +244,87 @@
 til.
-HashMap;%0A
+stream.Collectors.groupingBy;%0Aimport static java.util.stream.Collectors.toList;
 %0A%0Apu
@@ -551,228 +551,110 @@
     
-List%3CInteger%3E myList = new ArrayList%3C%3E();%0A    HashMap%3CInteger, Integer%3E map = new HashMap%3C%3E();%0A%0A    for (int n : 
+return Arrays%0A      .stream(
 nums)
- %7B
 %0A      
-if (!map.containsKey(n)) map.put(n, 1);%0A      else map.put(n, map.get(n) + 1);%0A    %7D%0A%0A    map
+.boxed()%0A      .collect(groupingBy(x -%3E x, counting()))%0A      
 .ent
@@ -660,16 +660,23 @@
 trySet()
+%0A      
 .stream(
@@ -695,36 +695,8 @@
 ted(
-Map.Entry.%3CInteger, Integer%3E
 comp
@@ -712,19 +712,22 @@
 lue(
-).
 reverse
-d(
+Order()
 ))%0A 
@@ -752,74 +752,55 @@
    .
-forEach((entry) -%3E myList.add(e
+map(Map.E
 ntry
-.
+::
 getKey
-()));%0A%0A    return my
+)%0A      .collect(to
 List
+())
 ;%0A  

use stream syntax for everything