Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

min priority queue in java

import java.util.PriorityQueue;
import java.util.Comparator;
public class Main
{
    public static void main(String[] args) {
        PriorityQueue<Integer> nums = new PriorityQueue<>(new CustomComparator());
        nums.offer(21);
        nums.offer(1);
        nums.offer(8);
        nums.offer(2);
        nums.offer(-4);
        System.out.println(nums.peek());
    }
}
class CustomComparator implements Comparator<Integer>{
    @Override
    public int compare(Integer n1, Integer n2){
        int val = n1.compareTo(n2);
        if(val > 0)
           return -1;
        else if(val < 0)
            return 1;
        else
            return 0;
    }
}
Comment

min priority queue in java

PriorityQueue<Integer> pq = new PriorityQueue<Integer>(defaultSize, new Comparator<Integer>() {
    public int compare(Integer lhs, Integer rhs) {
        if (lhs < rhs) return +1;
        if (lhs.equals(rhs)) return 0;
        return -1;
    }
});
Comment

min priority queue in java

public class Sample {
    public static void main(String[] args) {
        PriorityQueue<Integer> q = new PriorityQueue<Integer>(new Comparator<Integer>() {

            public int compare(Integer lhs, Integer rhs) {
                if(lhs<rhs) return +1;
                if(lhs>rhs) return -1;
                return 0;
            }
        });
        q.add(13);
        q.add(4);q.add(14);q.add(-4);q.add(1);
        while (!q.isEmpty()) {
            System.out.println(q.poll());
        }
    }

}
Comment

min priority queue in java

// There is overflow problem when using simple lambda as comparator, as pointed out by Фима Гирин.
// PriorityQueue<Integer> pq = new PriorityQueue<>((x, y) -> y - x);

PriorityQueue<Integer> pq =new PriorityQueue<>((x, y) -> Integer.compare(y, x));

pq.add(10);
pq.add(5);
System.out.println(pq.peek());
Comment

PREVIOUS NEXT
Code Example
Java :: h2 database spring boot 
Java :: java arraylist to array 
Java :: java print variable type 
Java :: java datetime now 
Java :: java check two dates same day 
Java :: initialize arraylist in 1 line in java 
Java :: java separate the numbers from string 
Java :: jframe border 
Java :: find minimum number in array java 
Java :: how to change label text color javafx 
Java :: get request java 
Java :: android java xml combo box 
Java :: Android dark theme programmatically 
Java :: java get year 
Java :: regex pattern for date validation 
Java :: select photo from camera android 
Java :: java stream limit items 
Java :: java put a char array into a string 
Java :: java scanner string nextline after nextint 
Java :: keytool error: java.io.FileNotFoundException: c:UsersUSER_NAMEupload-keystore.jks (The system cannot find the path specified) 
Java :: java 8 map foreach 
Java :: set ImageView size programmatically android 
Java :: all possible valid IP addresses leetcode 
Java :: java download image file 
Java :: how to convert char to uppercase java 
Java :: java get JComboBox value 
Java :: date minus date java 
Java :: terminate a frame java 
Java :: how to move a marker on google maps in android studio 
Java :: 64 bit integer java 
ADD CONTENT
Topic
Content
Source link
Name
2+5 =