Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

max heap java

import java.util.PriorityQueue;

public class MaxHeapWithPriorityQueue {

    public static void main(String args[]) {
        // create priority queue
        PriorityQueue<Integer> prq = new PriorityQueue<>(Comparator.reverseOrder());

        // insert values in the queue
        prq.add(6);
        prq.add(9);
        prq.add(5);
        prq.add(64);
        prq.add(6);

        //print values
        while (!prq.isEmpty()) {
            System.out.print(prq.poll()+" ");
        }
    }

}
Comment

min max heap java

// min heap: PriorityQueue implementation from the JDK
PriorityQueue<Integer> prq = new PriorityQueue<>();

// max heap: PriorityQueue implementation WITH CUSTOM COMPARATOR PASSED
// Method 1: Using Collections (recommended)
PriorityQueue<Integer> prq = new PriorityQueue<>(Collections.reverseOrder());
// Method 2: Using Lambda function (may cause Integer Overflow)
PriorityQueue<Integer> prq = new PriorityQueue<>((a, b) -> b - a);
Comment

PREVIOUS NEXT
Code Example
Java :: intellij 
Java :: primitive data types in java 
Java :: android @Parcelize not resolving 
Java :: java Program for Sum of the digits of a given number 
Java :: scanner.hasnext() 
Java :: how to right align in java 
Java :: spring swagger 
Java :: java delete column from csv 
Java :: list of integers in java 
Java :: jdbc interface 
Java :: java system.out.println 
Java :: java integer division tofloat 
Java :: each character in string java 
Java :: how to format a double in java to 2 decimal places 
Java :: random number generator java with range 
Java :: java convert edittext to double 
Java :: como ordenar un arraylist alfabeticamente en java 
Java :: java printf 
Java :: set look and feel system default java 
Java :: java.lang.arrayindexoutofboundsexception: index 3 out of bounds for length 3 
Java :: largest rectangle in histogram leetcode 
Java :: how to change tablayout current view position in android 
Java :: HOW TO PARSE a string into a number in java 
Java :: android dialog box example 
Java :: Java repeat last 3 letters of string 
Java :: how to initialize an empty array in java 
Java :: java split int 
Java :: spring boot get request body 
Java :: java program to Check given String is contians number or not 
Java :: sort a list with custom comparator java 
ADD CONTENT
Topic
Content
Source link
Name
1+9 =