Difference Between PriorityQueue and TreeSet - GeeksforGeeks

28 Dec, 2020
The PriorityQueue and TreeSet both are the classes defined inside the Collection Framework. In this article, we will learn the differences between PriorityQueue and TreeSet. PriorityQueue is an implementation of Queue interface and the TreeSet is the implementation of the Set interface. There are some differences exists between them. So we have tried to list out the differences between PriorityQueue and TreeSet.
1. PriorityQueue: A PriorityQueue is used when the objects are supposed to be processed based on the priority. It is known that a Queue follows the First-In-First-Out algorithm, but sometimes the elements of the queue are needed to be processed according to the priority, that’s when the PriorityQueue comes into play. The PriorityQueue is based on the priority heap. The elements of the priority queue are ordered according to the natural ordering, or by a Comparator provided at queue construction time, depending on which constructor is used.  
PriorityQueue Demo:
Java
// Java program to demonstrate the// working of PriorityQueueimport java.util.*;  class PriorityQueueDemo {      // Main Method    public static void main(String args[])    {        // Creating empty priority queue        PriorityQueue<String> pQueue            = new PriorityQueue<>();          // Adding elements to the pQueue using add()        pQueue.add("Geeks");        pQueue.add("For");        pQueue.add("Geeks");          // Printing the top element of PriorityQueue        System.out.println(pQueue.peek());          // Printing the top element and removing it        // from the PriorityQueue container        System.out.println(pQueue.poll());          // Printing the top element again        System.out.println(pQueue.peek());    }}
For
For
Geeks

2. TreeSet: TreeSet is one of the most important implementations of the SortedSet interface in Java that uses a Tree for storage. The ordering of the elements is maintained by a set using their natural ordering whether or not an explicit comparator is provided. This must be consistent with equals if it is to correctly implement the Set interface. It can also be ordered by a Comparator provided at set creation time, depending on which constructor is used. The TreeSet implements a NavigableSet interface by inheriting AbstractSet class.
TreeSet Demo:
Java
// Java code to demonstrate// the working of TreeSet  import java.util.*;class TreeSetDemo {      public static void main(String[] args)    {        // Creating an empty TreeSet        TreeSet<String> ts = new TreeSet<String>();          // Elements are added using add() method        ts.add("Geek");        ts.add("For");        ts.add("Geeks");          System.out.println("Tree Set is " + ts);          String check = "Geeks";          // Check if the above string exists in        // the treeset or not        System.out.println("Contains " + check + " "                           + ts.contains(check));          // Print the first element in        // the TreeSet        System.out.println("First Value " + ts.first());          // Print the last element in        // the TreeSet        System.out.println("Last Value " + ts.last());          String val = "Geek";          // Find the values just greater        // and smaller than the above string        System.out.println("Higher " + ts.higher(val));        System.out.println("Lower " + ts.lower(val));    }}
                   PriorityQueue
                                TreeSet
Java-Collections
java-priority-queue
java-treeset
Technical Scripter 2020
Difference Between
Java
Technical Scripter
Java
Java-Collections
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Difference Between Method Overloading and Method Overriding in Java
Difference between Prim's and Kruskal's algorithm for MST
Difference between Internal and External fragmentation
Difference between Compile-time and Run-time Polymorphism in Java
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Object Oriented Programming (OOPs) Concept in Java
Arrays.sort() in Java with examples