Priority Queue using TreeMap

PHOTO EMBED

Wed Jun 05 2024 18:51:51 GMT+0000 (Coordinated Universal Time)

Saved by @chatgpt #java

//Priority Queue using TreeMap

import java.util.*;
public class PriorityQueueDemo<K, V>{
    private TreeMap<K, V> tm;
    
    public PriorityQueueDemo(){
        tm = new TreeMap();
    }
    
    public void insert(K key, V value){
        tm.put(key, value);
    }
    
    
    public K getMinKey(){
        return tm.firstEntry().getKey();
    }
    
    public V getMinValue(){
        return tm.firstEntry().getValue();
    }
    
    public static void main(String[] args){
        PriorityQueueDemo<Integer, String> p = new PriorityQueueDemo<>();
        p.insert(5,"hello");
        p.insert(2,"java");
        p.insert(1, "programming");
        p.insert(3, "welcome");
        
        System.out.println("Minimum Key: "+p.getMinKey());
        System.out.println("Minimum Value: "+p.getMinValue());
    }
}
content_copyCOPY