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?

Matt
  • 501
  • 4
  • 14
  • 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. – Hovercraft Full Of Eels Oct 07 '17 at 19:39
  • you might be interested to see this post https://stackoverflow.com/questions/13670991/interview-can-we-instantiate-abstract-class – Ravi Oct 07 '17 at 19:43

0 Answers0