Priority queue ordering of elements

后端 未结 4 441
情话喂你
情话喂你 2020-12-18 00:31

How come the elements of priority queue are ordered according to natural order by default as it doesn\'t implement comparable interface.

From the docs, it says elem

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-18 01:13

    Actually PriorityQueue allows only those elements which implements Comparable or you need to provide Custom Comparator.

    Integer and String values are allowed in Comparator because they implement Comparable interface. e.g. public final class String implements java.io.Serializable, Comparable, CharSequence

    public final class Integer extends Number implements Comparable

    If you create your own class e.g Employee then it should implement Comparable or you should provide your custom Comparator.

    I hope this will resolve your queries!!!!

提交回复
热议问题