How to initialize PriorityQueue with list values in Java

1 Answer

0 votes
import java.util.List;
import java.util.Arrays;
import java.util.PriorityQueue;

public class MyClass {
    public static void main(String args[]) {
        List<Integer> lst = Arrays.asList(100, 88, 98, 80, 50, 12, 35, 70, 60, 97, 85, 89);
        
        PriorityQueue<Integer> pq = new PriorityQueue<>(lst);
 
        while (!pq.isEmpty()) {
            System.out.print(pq.poll() + " ");
        }
    }
}




/*
run:

12 35 50 60 70 80 85 88 89 97 98 100 

*/

 



answered May 13, 2022 by avibootz

Related questions

2 answers 299 views
1 answer 169 views
1 answer 214 views
2 answers 344 views
344 views asked Nov 6, 2021 by avibootz
1 answer 257 views
1 answer 305 views
...