1

So here is code which implements a min-heap :

PriorityQueue<String> minHeap = new PriorityQueue<>(k,new Comparator<String>(){
   public int compare(String s1, String s2){
      return Integer.compare(s1.length(), s2.length());
   }
});

I am confused about the compare function. It appears we are overriding the compare function in Comparator but how? Where can I learn about overriding methods upon object instantiation in java?

2
  • You are creating a new Comparator object via an anonymous inner class and overriding an interface method declaration. You need to study interfaces and such. Commented Oct 7, 2017 at 19:39
  • you might be interested to see this post stackoverflow.com/questions/13670991/… Commented Oct 7, 2017 at 19:43

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.