Java usage priority queue

Priority queue priorityQueue achieve Queue interface each of which can sort the elements can be put basic type of package or custom class for basic types of packaging, the priority queue element default sort order is ascending, but for custom class, the class need a custom comparison

priorityQueue internal implementation
PriorityQueue the elements uses a heap sort, first sort is specified smallest element. Heap sort can only guarantee the root is the largest (smallest), the entire stack is not orderly.
The method iterator () iterator may be provided only in order to traverse the entire array. We can only guarantee the first element of the array is the smallest 

PriorityQueue the iterator () does not guarantee any particular order traverse the queue elements. To traverse a specific order, turn first queue into an array, and then sorting traversal. Arrays.sort (pq.toArray ())

Common methods:

PEEK () // Returns the head of the queue element 
poll () // Returns the head of the queue elements, the first element of the queue team 
the Add () // additive element 
size () // returns the number of elements in the queue 
isEmpty () // whether the queue is empty, returns true null, not empty return false

Queue holds the basic data types of packaging:

Top k k small problems before seeking

class Main {
    public static void main(String[] args) {
        int[] array = {9,8,7,6,5,4,3,2,1};
        System.out.println(Solution(array,4));

    }
    public static ArrayList<Integer> Solution(int[] array, int k) {
        ArrayList<Integer> result = new ArrayList<>();
        int length = array.length;
        if (k > length || k <= 0) {
            return result;
        }
        PriorityQueue<Integer> maxheap = new PriorityQueue<Integer>(k, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {

                return o2.compareTo(o1);
            }
        });
        for (int i = 0; i < length; i++) {
            if (maxheap.size() != k){
            maxheap.offer(array[i]);
            }
            else if(array[i] < maxheap.peek()){
                    maxheap.poll();
                    maxheap.offer(array[i]);
                }

        }


        for(Integer num : maxheap){
            result.add(num);
        }
        return result;
        }

}

Queue saved custom class

The first written (at priorityQueue configuration, new Comparator)

class Main {
    public static void main(String[] args) {
        PriorityQueue<person> priorityQueue = new PriorityQueue<person>(10, new Comparator<person>() {
          @Override
            public int compare(person o1, person o2) {
                if (o1.getAge() != o2.getAge()){
                    return o1.getAge() - o2.getAge();
                }
                else if(o1.getAge() == o2.getAge() && o1.getTall()!=o2.getTall()){
                    return o1.getTall() - o2.getTall();
                }
                else {
                    return o2.getName().length() - o1.getName().length();
                }

            }
        });

        priorityQueue.add(new person(11,160,"Nick"));
        priorityQueue.add(new person(16,172,"John"));
        priorityQueue.add(new person(18,180,"Mike"));
        priorityQueue.add(new person(22,183,"Jordan"));
        priorityQueue.add(new person(16,172,"Alice"));
        person p;
        while (!priorityQueue.isEmpty()){
            p = priorityQueue.poll();
            System.out.println(p.toString());
        }
    }
    
}

class person{
    private int age;
    private int tall;
    private String name;
    
    public person(int age, int tall, String name) {
        this.age = age;
        this.tall = tall;
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public int getTall() {
        return tall;
    }
    
    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return "person{" +
                "age=" + age +
                ", tall=" + tall +
                ", name='" + name + '\'' +
                '}';
    }
}

The second writing, when constructing the incoming Comparator PriorityQueue

class MyQueue{
    public static Comparator<person> cperson = new Comparator<person>() {
        @Override
        public int compare(person o1, person o2) {
            if (o1.getAge() != o2.getAge()){
                return o1.getAge() - o2.getAge();
            }
            else if(o1.getAge() == o2.getAge() && o1.getTall()!=o2.getTall()){
                return o1.getTall() - o2.getTall();
            }
            else {
                return o1.getName().length() - o2.getName().length();
            }

        }
    };

    public static void main(String[] args) {
        Queue<person> priorityQueue = new PriorityQueue<>(100,cperson);
        priorityQueue.add(new person(11,160,"Nick"));
        priorityQueue.add(new person(16,172,"John"));
        priorityQueue.add(new person(18,180,"Mike"));
        priorityQueue.add(new person(22,183,"Jordan"));
        person p;
        while (!priorityQueue.isEmpty()){
            p = priorityQueue.poll();
            System.out.println(p.toString());
        }
    }
    
}

class person{
    private int age;
    private int tall;
    private String name;


    public person(int age, int tall, String name) {
        this.age = age;
        this.tall = tall;
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public int getTall() {
        return tall;
    }

    public String getName() {
        return name;
    }
    
    @Override
    public String toString() {
        return "person{" +
                "age=" + age +
                ", tall=" + tall +
                ", name='" + name + '\'' +
                '}';
    }
}

 Output:

 

Priority queue priorityQueue achieve Queue interface each of which can sort the elements can be put basic type of package or custom class for basic types of packaging, the priority queue element default sort order is ascending, but for custom class, the class need a custom comparison

priorityQueue internal implementation
PriorityQueue the elements uses a heap sort, first sort is specified smallest element. Heap sort can only guarantee the root is the largest (smallest), the entire stack is not orderly.
The method iterator () iterator may be provided only in order to traverse the entire array. We can only guarantee the first element of the array is the smallest 

PriorityQueue the iterator () does not guarantee any particular order traverse the queue elements. To traverse a specific order, turn first queue into an array, and then sorting traversal. Arrays.sort (pq.toArray ())

Guess you like

Origin www.cnblogs.com/hetaoyuan/p/12294576.html