public static class Song implements Comparable<Song> {
int plays;
String genre;
int index;
public Song(int plays, String genre, int index) {
this.plays = plays;
this.genre = genre;
this.index = index;
}
@Override
public int compareTo(Song s) { // 내림차순
if(this.plays < s.plays) return 1;
else if (this.plays > s.plays) return -1;
return 0;
// return (this.필드 - s.필드) * -1; (오름차순)
}
@Override
public int compareTo(Song s) { // 오름차순
if(this.plays > s.plays) return 1;
else if (this.plays < s.plays) return -1;
return 0;
// return this.필드 - s.필드; (오름차순)
}
}
import java.util.PriorityQueue;
import java.util.Collections;
PriorityQueue<Integer> pQueue = new PriorityQueue<>(); PriorityQueue<Integer> pQueue = new PriorityQueue<>(Collections.reverseOrder());
PriorityQueue.add()
PriorityQueue.offer()
PriorityQueue.poll()
: 우선순위 큐 에서 첫번쨰 값 반환, 비어있다면 null
PriorityQueue.remove()
: 우선순위 큐 에서 첫번쨰 값 반환, 비어있다면 Exception
PriorityQueue.clear()
: 우선순위 큐 초기화
첫번째 값 반환, 제거 X
PriorityQueue.peek()
: 비어있다면 null 반환
PriorityQueue.element()
: 비어있다면 Exception