Implementing Java Comparator

前端 未结 2 404
栀梦
栀梦 2020-12-09 17:32

I am trying to write an algorithm which utilizes a min-priority queue, so I looked around on google and found the PriorityQueue. It seems that in order to use it, though, I

2条回答
  •  天命终不由人
    2020-12-09 18:16

    As you can see here (the JSE Comparator JavaDoc page), the Comparator interface has a generic "parameter" describing the type for which this comparator is designed. The PriorityQueue is similar.

    So, if you create a PriorityQueue, you can create a Comparator as follows:

    public class distComparator implements Comparator {    
        @Override
        public int compare(Node1 x, Node1 y){
            return x.dist - y.dist;
        }
    }
    

提交回复
热议问题