I am trying to implement Max heap with key value pair in java. As we know in C++ we can do that in this way..
priority_queue<pair<int, int> > pq;
pq.push(make_pair(10, 200));
pq.push(make_pair(20, 100));
pq.push(make_pair(15, 400));
But I am unable to do this in java, please help me to do so.
In Java
you can use a HashMap
for that:
import javafx.util.Pair;
import java.util.PriorityQueue;
public class YourClass{
public static void main (String[] args){
int n = 3;
PriorityQueue<Pair<Integer,Integer> > pq = new PriorityQueue<Pair<Integer,Integer>>(n, Comparator.comparing(Pair::getKey));
/*Adding elements to HashMap*/
pq.add(new Pair <> (10, 200));
pq.add(new Pair <> (20, 100));
pq.add(new Pair <> (15, 400));
System.out.println(l.poll().getValue());
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.