import java.util.*; public class Priority4 { static int byFour(int n) { if(n % 4 == 0) return -1; else return 0; } public static void main(String[] args) { Random rand = new Random(); PriorityQueue p = new PriorityQueue<>(new Comparator() { public int compare(Integer a, Integer b) { int diff = 0; if(diff == 0) diff = byFour(a) - byFour(b); if(diff == 0) diff = a - b; return diff; } }); for(int i=0;i<10;i++) { p.add(rand.nextInt(1000)); } System.out.println("==="); for(int i=0;i<10;i++) { System.out.println(p.remove()); } } }