5:37 pm - Thursday August 17, 2017

PriorityQueue example program to add a range of numbers using Add() method

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

BACK

 

This Program is used PriorityQueue to add a range of numbers using Add() method.

Example Program


import java.util.PriorityQueue;

public class PriorityQ {

public static void main(String[] args) {

PriorityQueue PQ = new PriorityQueue();

PQ.add(100);

PQ.add(12); // adds data based on data type

PQ.add(56);

PQ.add(89);

PQ.add(103);

System.out.println("The default way:");

for (Integer number : PQ) {

System.out.println(number);

}

}

}

 

Output

    The default way:

     12

     89

     56

     100

     103

 

Inserts the specified element into this priority queue.

Specified by:

add  in interface Collection<E>

Specified by:

add in interface Queue<E>

Overrides:

add in class AbstractQueue<E>

Parameters:

e – the element to add

Returns:

true (as specified by Collection.add(E))

Throws:

ClassCastException – if the specified element cannot be compared with  elements currently in this priority queue according to the priority queue’s ordering

NullPointerException – if the specified element is null

BACK

Filed in: Java Examples

Comments are closed.