📜  PriorityQueue 和 TreeSet 的区别

📅  最后修改于: 2021-09-14 02:21:55             🧑  作者: Mango

PriorityQueue 和 TreeSet 都是在 Collection Framework 中定义的类。在本文中,我们将了解 PriorityQueue 和 TreeSet 之间的区别。 PriorityQueue 是 Queue 接口的实现,TreeSet 是 Set 接口的实现。它们之间存在一些差异。所以我们试图列出 PriorityQueue 和 TreeSet 之间的区别。

1. PriorityQueue 当应该根据优先级处理对象时,使用 PriorityQueue。众所周知,队列遵循先进先出算法,但有时需要根据优先级处理队列中的元素,这就是PriorityQueue发挥作用的时候。 PriorityQueue 基于优先级堆。优先级队列的元素按照自然顺序排序,或者由队列构建时提供的 Comparator 排序,具体取决于使用的构造函数。

PriorityQueue 演示:

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


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 ts = new TreeSet();
  
        // 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));
    }
}


输出
For
For
Geeks

2. TreeSet TreeSet 是Java中SortedSet 接口最重要的实现之一,它使用Tree进行存储。无论是否提供显式比较器,元素的顺序都由使用其自然顺序的集合维护。如果要正确实现Set 接口,这必须与 equals 一致。它也可以由在集合创建时提供的 Comparator 进行排序,具体取决于使用的构造函数。 TreeSet通过继承AbstractSet 类实现了NavigableSet 接口

树集演示:

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 ts = new TreeSet();
  
        // 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 的区别

                   PriorityQueue

                                TreeSet

PriorityQueue uses the Queue underlying data structure TreeSet uses the Set underlying data structure.
PriorityQueue allows the duplicate elements TreeSet 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.