Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PriorityQueue Comparator type conflict with Arrays.sort()

Tags:

java

My Comparator is currently typed to JobSet. I'm unsure why it has <capture - I've never seen that before. Can someone shed some light on what's going on here?

The comparator...

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

The queue...

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

like image 342
Webnet Avatar asked Feb 17 '23 12:02

Webnet


1 Answers

queue.toArray() returns a Object[], which your provided Comparator can't handle.

You will need to use the alternate toArray(), that takes an array of the expected type:

JobSet[] queueArray = queue.toArray(new JobSet[]{});
Arrays.sort(queueArray, queue.comparator());
return queueArray;
like image 189
Keppil Avatar answered Feb 24 '23 04:02

Keppil