Menu
Topics Index
...
`

StudentsDetails


If you need explanation Read this topic

If you need Answer Take test on this topic

StudentsDetails
What will be the output of following program?
import java.util.*;

public class StudentsPriority {
    public static void main(String[] args) {
        PriorityQueue<Student1> pq = new PriorityQueue<Student1>();
        pq.add(new Student1(15, "Pramod"));
        pq.add(new Student1(5, "Soumya"));
        pq.add(new Student1(2, "Santosh"));
        pq.add(new Student1(13, "Nyamath"));
        pq.peek().print();
        pq.element().print();
        pq.poll().print();
        pq.element().print();
    }            
}

class Student1 implements Comparable<Student1> {
    int id;
    String name;
    public Student1(int id, String name) {
        this.id = id;
        this.name = name;
    }
    public int compareTo(Student1 other) {
        return id - other.id;
    }
    public void print() {
        System.out.print(id + "-" + name + ",");
    }
}

A. 2-Santosh,2-Santosh,2-Santosh,5-Soumya,
B. 15-Pramod,15-Pramod,15-Pramod,13-Nyamath,
C. 15-Pramod,15-Pramod,15-Pramod,5-Soumya,
D. Some other output
E. Compilation Error or Runtime Error
Topic: Java Comparators

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Ans is A...here we have the  PriorityQueue  concept...here Queue will work based on FIFO formula sometimes we may need to insert elements according to the Priority so here PriorityQueue will helpful...PQ wont allow the null values, we can’t create PriorityQueue of Objects that are non-comparable PQ introduced in the java1.5v..PQ is unbounded...it will grows if we keep adding the elements..

Coming to the Program here we create the instance for the PQ which stores the Instances of the Student class..
after creation we created the Objects for the Student class and Added to the PQ.. it will arrange the Objects based on the return value of the  compareTo() in student class..here we are customizing the Sorting order..here we implements the compareTo() as Ascending order..
here we call the peek() which returns the head of the Q but didnot remove...with that object we are calling print() so here it will return the santosh obejct and following details get printed using the print() in Student class...

element() this is also returns the head of the Q..if not present throws Exception...it wont remove..

poll() removes and returns the head of the Q..here santosh will removed and get printed.
again elements() which will returns the  sowmay() bcz header santosh is removed by calling poll()..now sowmya is the header..so sowmya details get printed...
.

Posted by Uday Kumar    2014-12-04 12:29:08


Answer is A....The elements of the priority queue are ordered according to their natural ordering, or by a Comparator provided at queue construction time, depending on which constructor is used.
Order in which the added elemnts are stored would be 2,5,13,15
peek() would retrieve the element but not remove the element,so 2 would be printed
element() Retrieves, but does not remove, the head of this queue.so 2 would be printed
poll() will remove the element from the head....so 2  would be retrieved and removed
element() will now retrieve the next element which is 5

Posted by Shashanka Mogaliraju    2014-12-04 12:36:06


ans a.
The Queue interface basically orders the element in FIFO(First In First Out)manner.
In Java, the PriorityQueue class is implemented as a priority heap.

PriorityQueue class is an unbounded priority queue based on a priority heap.Following are the important points about PriorityQueue:

--The elements of the priority queue are ordered according to their natural ordering, or by a Comparator provided at queue construction time, depending on which constructor is used.

--A priority queue does not permit null elements.

--A priority queue relying on natural ordering also does not permit insertion of non-comparable objects.

Methods of Queue Interface :

public boolean add(object);
public boolean offer(object);
public remove();
public poll();
public element();
public peek();

The add(E e) method is used to insert the specified element into a priority queue.

The peek() method is used to retrieve the head of this queue.But it does not remove it

The poll() method is used to retrieve and remove the head of this queue, or returns null if this queue is empty.

coming to this program 4 elements get added.
here comparator used with key value sorting is done based on the value.

2,5,13,15 order.
thn peek called it prints 1st element 2-santosh get printed by callin print().

thn element() called it prints current objects data so 2-santosh printd.

thn poll() called 2-santosh printd and element is removed from the queue.

thn element called nw 5-soumya is 1st object.bcz we removed 2-santosh in previous function call.

Posted by Maheshwari Natarajan    2014-12-04 13:18:21


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Maheshwari Natarajan,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Ramya Reddy Addulla. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2014-12-05 06:58:47


© meritcampus 2019

All Rights Reserved.

Open In App