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 187 views
1 answer 95 views
1 answer 132 views
2 answers 229 views
229 views asked Nov 6, 2021 by avibootz
1 answer 166 views
1 answer 206 views
...