The PriorityQueue Comparator parameter conflicts with Arrays.sort ()

My comparator is currently printed on JobSet . I'm not sure why he has <capture - I have not seen this before. Can someone shed light on what is happening here?

Comparator ...

 public class JobSetComparator implements Comparator<JobSet> { @Override public int compare(JobSet o1, JobSet o2) { return Integer.compare(o1.getHighestPriority().getValue(), o2.getHighestPriority().getValue()); } } 

Turn...

 protected JobSetQueue queue = new JobSetQueue(0, new JobSetComparator()); public JobSetQueue getQueue() { return queue; } public JobSet[] getPrioritizedQueue() { return Arrays.sort(queue.toArray(), queue.comparator()); } 

enter image description here

+4
source share
1 answer

queue.toArray() returns a Object[] that your provided Comparator cannot handle.

You will need an alternative toArray() that takes an array of the expected type:

 JobSet[] queueArray = queue.toArray(new JobSet[]{}); Arrays.sort(queueArray, queue.comparator()); return queueArray; 
+8
source

Source: https://habr.com/ru/post/1481468/


All Articles