The difference and connection between TreeMap and TreeSet

Source: Internet
Author: User

TreeMap and TreeSet are two important members of the Java Collection Framework, where TreeMap is a common implementation class for the Map interface, and TreeSet is a common implementation class for the Set interface. Although the interface specifications implemented by TREEMAP and TreeSet are different, the TreeSet underlying is implemented through TREEMAP (as if the HashSet is implemented by HashMap), so the two are implemented exactly the same way. and the implementation of TREEMAP is the red-black tree algorithm

The relationship between TreeSet and TreeMap

  Similar to the hashset, most of the methods in the TreeSet City call TreeMap method directly to achieve.

Same point:

    • TreeMap and TreeSet are non-synchronous collections, so they cannot be shared across multiple threads, but can be synchronized using method Collections.synchroinzedmap ()
    • The running speed is slower than the hash set, their internal operation time for elements is O (Logn), while the Hashmap/hashset is O (1).
    • Both TreeMap and TreeSet are ordered collections, meaning that the values they store are well-sequenced.

Different points:

    • The main difference is that TreeSet and TreeMap implement set and map interfaces respectively.
    • TreeSet stores only one object, while TreeMap stores two objects key and value (only the Key object is ordered)
    • TreeSet cannot have duplicate objects, while treemap can exist in
    • The bottom of the TreeMap is implemented with red and black trees, and the data is inserted and sorted in order.

  

features of red and black trees:
    • Property 1: Each node is either red or black.
    • Property 2: The root node is always black.
    • Property 3: All leaf nodes are empty nodes (that is, null) and are black.
    • Property 4: The two child nodes of each red node are black. (There will be no two contiguous red nodes from each leaf to the root path)
    • Property 5: The path from either node to each leaf node in its subtree contains the same number of black nodes.

The difference and connection between TreeMap and TreeSet

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.