Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Difference Between PriorityQueue and TreeSet
Next article icon

Difference Between PriorityQueue and TreeSet

Last Updated : 28 Dec, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report

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 PriorityQueue import 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());     } } 

Output
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));     } } 

Differences between PriorityQueue and TreeSet

                   PriorityQueue

                                TreeSet

PriorityQueue uses the Queue underlying data structureTreeSet uses the Set underlying data structure.
PriorityQueue allows the duplicate elementsTreeSet doesn't allow the duplicate elements
In PriorityQueue, apart from the root rest of the elements may or may not follow any order.In TreeSet all the elements remain in the sorted order.
Using PriorityQueue, we can retrieve largest or smallest element in O(1) time.TreeSet doesn't provide a way to retrieve largest or smallest element in O(1) time, but since they are in sorted order it gets the first or last element in O(1) time.
PriorityQueue comes in JDK 1.5.TreeSet comes in JDK 1.4.

Next Article
Difference Between PriorityQueue and TreeSet

P

prashant_srivastava
Improve
Article Tags :
  • Java
  • Technical Scripter
  • Difference Between
  • Technical Scripter 2020
  • Java-Collections
  • java-priority-queue
  • java-treeset
Practice Tags :
  • Java
  • Java-Collections

Similar Reads

    Difference between Circular Queue and Priority Queue
    Queues are fundamental data structures that are used to store and manage a collection of elements. While both circular queues and priority queues are types of queues, they have distinct characteristics and applications. This article will explore the key differences between circular queues and priori
    4 min read
    Difference Between TreeSet and SortedSet in Java
    TreeSet is one of the implementations of the Navigable sub-interface. It is underlying data structure is a red-black tree. The elements are stored in ascending order and more methods are available in TreeSet compare to SortedSet. We can also change the sorting parameter using a Comparator. For examp
    3 min read
    Difference Between heapq and PriorityQueue in Python
    In this article, we are going to see the difference between heapq and PriorityQueue in Python. Differences between PriorityQueue and heapqPython queue PriorityQueue is thread-safe, but heapq doesn't guarantee thread safety.PriorityQueue implements locking to ensure thread safety, thus it is slower t
    3 min read
    Difference between TreeMap and TreeSet in Java
    TreeSet is mainly an implementation of SortedSet in java where duplication is not allowed and objects are stored in sorted and ascending order. Some important features of the TreeSet are: In TreeSet duplicate values are not allowed because it implements the SortedSet interface.Objects in a TreeSet a
    2 min read
    Difference between FCFS and Priority CPU scheduling
    1. First Come First Served (FCFS) : First Come First Served (FCFS) is the simplest type of algorithm. It is a non-preemptive algorithm i.e. the process cannot be interrupted once it starts executing. The FCFS is implemented with the help of a FIFO queue. The processes are put into the ready queue in
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences