Please corrrect my code

public class TopKMostFrequentNumberInStream {

public static void main(String[] args) {
	Scanner sc  = new Scanner(System.in);
	int t=sc.nextInt();
	while(t-->0) {
		int n=sc.nextInt();
		int k=sc.nextInt();
		int[] arr = new int[n];
		for(int i=0;i<n;i++) {
			arr[i]=sc.nextInt();
		}
		
		topK(arr,k);
	}
}


public static void topK(int[] nums, int k) {
	
	HashMap<Integer, Integer> map = new HashMap<>();
	PriorityQueue<Pair> heap = new PriorityQueue<>();
	
	for(int i=0;i<nums.length;i++) {
		
		map.put(nums[i], map.getOrDefault(nums[i], 0)+1);
		System.out.println(map);
		
		ArrayList<Integer> keys = new ArrayList<>(map.keySet());

// System.out.println(keys);
int j=0;
for(j=0;j<k && j<keys.size();j++) {
Pair np = new Pair(keys.get(j), map.get(keys.get(j)));
heap.add(np);
}
System.out.print(heap);
for(;j<keys.size();j++) {
Pair check = new Pair(keys.get(j), map.get(keys.get(j)));
System.out.println("Heap peek = " + heap.peek());
if(heap.peek().compareTo(check) >=0) {
heap.remove();
heap.add(check);
}
}
System.out.print(heap);
while(!heap.isEmpty()) {
System.out.print(heap.remove().data + " ");
}
System.out.println();
// System.out.println(heap);
}
}

private static class Pair implements Comparable<Pair>{
	int data;
	int freq;
	
	Pair(int data, int freq){
		this.data=data;
		this.freq=freq;
	}

	@Override
	public int compareTo(Pair o) {
		
		if(o.freq>this.freq) {
			return 1;
		}else if(o.freq<this.freq) {
			return -1;
		}else {
			if(o.data>this.data) {
				return -1;
			}else if(o.data<this.data) {
				return 1;
			}else {
				return -1;
			}
		}
	}
	
	public String toString() {
		return this.data + " -> " + this.freq;
	}
	
}

@Ritik488 reviewing it bro!

@Ritik488 Bro you are misunderstanding the question basically ya code is giving top k frequent elements, that is another question but this includes “in a stream”. I am dry running the sample for you.

In this problem you are supposed to output atmost k integers on every iteration from the input you have taken so far.
Let us dry run the sample input given in the question for better understanding

Sample Input:
1
5 2
5 1 3 5 2
Sample Output:
5 1 5 1 3 5 1 5 1

Let us say store the input in an array named A

No of testcases = 1
Given n = 5 (We will get 5 integer elements in our array)
k = 2 (We have to output at max 2 integers on every iteration)

Iteration 1 :
Input : 5
A = {5}
Output : 5
Explanation : Since there is only one integer with frequency = 1 , we output it

Iteration 2 :
Input : 1
A = {1,5}
Output : 1 5
Explanation : Since there are two elements in A , we output them in sorted order

Iteration 3 :
Input : 3
A = {1,3,5}
Output : 1 3
Explanation : We output k = 2 elements from the array. Since frequency of all elements is the same , we output them in sorted order and 1 and 3 being the smallest gets printed

Iteration 4 :
Input : 5
A = {1,3,5,5}
Output : 5 1
Explanation : 5 has the highest frequency in the array so it gets printed first . Then the remaining elements having the same frequency get printed in the sorted order. Since we only have to print k=2 elements , only 5 and 1 get printed.

Iteration 5 :
Input : 2
A = {1,2,3,5,5}
Output : 5 1
Explanation : 5 still has the highest frequency and 1 being the smallest of the remaining elements gets to be printed .

Final Output : 5 1 5 1 3 5 1 5 1

I hope this clears your doubt and helps you understand the question.
So bro try to solve this question by the approach I told!
Hit like if you get it.