Java TreeMap Introduction and use __java

Source: Internet
Author: User
Tags int size shallow copy

Summary

In this chapter, we learn about TreeMap.
We first have a general understanding of TreeMap, and then learn its source, and finally through the example to learn to use TreeMap. The content includes:
The 1th part TreeMap Introduction
Part 2nd TREEMAP Data structure
3rd Part TREEMAP Traversal mode

Reprint please indicate the source: http://www.cnblogs.com/skywang12345/admin/EditPosts.aspx?postid=3310928

The 1th part TreeMap Introduction

TreeMap Introduction

TreeMap is an ordered set of Key-value , which is achieved through a red-black tree.
TreeMap inherits from Abstractmap, so it is a map, that is, a Key-value collection.
TREEMAP implements the Navigablemap interface, which means it supports a range of navigation methods. such as returning an ordered key collection.
TREEMAP implements the Cloneable interface, meaning it can be cloned .
TREEMAP implements the Java.io.Serializable interface, which means it supports serialization .

TreeMap is based on the red-black Tree (Red-black) . The mapping is sorted according to the natural order of its keys , or by the Comparator provided when the mapping is created , depending on the construction method used.
The time complexity of TreeMap's basic operations ContainsKey, get, put, and remove is log (n).
In addition, the TreeMap is not synchronized . Its iterator method returns an iterator that is Fail-fastl .

Constructor for TreeMap

The default constructor. Using this constructor, the elements in the TreeMap are arranged in a natural order.
TreeMap ()

///created TREEMAP contains Map
TreeMap (map< extends K,? extends v> CopyFrom)

//Specify Tree comparators
TreeMap (comparator<. Super k> Comparator)

//created TreeSet contains CopyFrom TreeMap
(sortedmap<k,? Extends V> CopyFrom)

API for TreeMap

 Map.entry<k,v> ceilingentry (K key) returns a key-value mapping relationship that is associated with a minimum key greater than or equal to the given key, or null if no such key exists.
 K Ceilingkey (k key) returns the minimum key that is greater than or equal to the given key, or null if no such key exists.
 void Clear () Removes all mapping relationships from this map.
 Object Clone () returns a shallow copy of this TREEMAP instance. comparator<?
 Super K> Comparator () returns a comparer that sorts the keys in this map, or null if the mapping uses the natural order of the keys.
 Boolean ContainsKey (Object key) returns True if this map contains a mapping relationship for the specified key.
 Boolean Containsvalue (Object value) returns TRUE if this map maps one or more keys for the specified value.
 Navigableset<k> Descendingkeyset () returns the reverse Navigableset view of the key contained in this map.
 Navigablemap<k,v> Descendingmap () returns a reverse view of the mapping relationship contained in this map.
 Set<map.entry<k,v>> EntrySet () returns the Set view of the mapping relationship contained in this map.
 Map.entry<k,v> Firstentry () returns a key-value mapping relationship that is associated with the smallest key in this map, or null if the mapping is null.
 K Firstkey () returns the current first (lowest) key in this map.
 Map.entry<k,v> floorentry (K key) returns a key-value mapping relationship that is associated with the largest key that is less than or equal to the given key, or null if no such key exists. K Floorkey (k key) returns a maximum of less than or equal to the given keyA large key, or null if no such key exists.
 The V get (Object key) returns the value mapped by the specified key, or null if the mapping does not contain any mapping relationships for that key.
 Sortedmap<k,v> Headmap (K tokey) returns a partial view of this map whose key values are strictly less than tokey.
 Navigablemap<k,v> Headmap (K Tokey, Boolean inclusive) returns a partial view of this map whose key is less than (or equal to, if inclusive is true) Tokey.
 Map.entry<k,v> higherentry (K key) returns a key-value mapping relationship, which is associated with a minimum key that is strictly greater than the given key, or null if no such key exists.
 K Higherkey (k key) returns the minimum key that is strictly greater than the given key, or null if no such key exists.
 Set<k> keyset () returns a Set view of the keys contained in this map.
 Map.entry<k,v> Lastentry () returns the key-value mapping relationship associated with the largest key in this map, or null if the mapping is null.
 K Lastkey () returns the current last (highest) key in the map.
 Map.entry<k,v> lowerentry (K key) returns a key-value mapping relationship, which is associated with a maximum key that is strictly less than the given key, or null if no such key exists.
 K Lowerkey (k key) returns the maximum key that is strictly less than the given key, or null if no such key exists.
 Navigableset<k> Navigablekeyset () returns the Navigableset view of the key contained in this map.
 Map.entry<k,v> pollfirstentry () removes and returns the key-value mapping relationship associated with the minimum key in this map, or null if the mapping is empty. Map.entry<k,v> PolllasTentry () removes and returns the key-value mapping relationship associated with the largest key in this map, or null if the mapping is null.
 V Put (K key, V value) associates the specified value with the specified key in this map.
 void Putall (map&lt. Extends K,? extends v> map) copies all mapping relationships in the specified mappings to this map.
 V Remove (Object key) If the mapping relationship exists for the key in this TreeMap, it is deleted.
 int size () returns the number of key-value mapping relationships in this map. Navigablemap<k,v> SubMap (k Fromkey, Boolean frominclusive, K Tokey, Boolean toinclusive) returns a partial view of this map whose key range
 From Fromkey to Tokey.
 Sortedmap<k,v> SubMap (k Fromkey, K tokey) returns a partial view of this map whose key values range from Fromkey (including) to Tokey (not included).
 Sortedmap<k,v> Tailmap (K fromkey) returns a partial view of this map with a key greater than or equal to Fromkey. 
 Navigablemap<k,v> Tailmap (K Fromkey, Boolean inclusive) returns a partial view of this map whose key is greater than (or equal to, if inclusive is true) Fromkey. Collection<v> values () returns the Collection view of the values contained in this map.


Part 2nd TREEMAP data Structure

the inheritance relationship of TreeMap

Java.lang.Object
   ↳     java.util.abstractmap<k, v>
         ↳ java.util.treemap<k     , v>

Public Class treemap<k,v>
    extends abstractmap<k,v>
    implements Navigablemap<k,v>, Cloneable, java.io.Serializable {}

TreeMap and map relate to the following diagram:

As you can see from the diagram:
The TreeMap implementation inherits from Abstractmap and implements the Navigablemap interface.
The essence of TreeMap is R-b tree (red and black), which contains several important member variables: root, size, comparator.
Root is the node of the red and black number. It is the entry type, entry is a red-black number of nodes, it contains the red and black number of the 6 basic components: Key (keys), value (value), left (the child), right (children), parent (parent node), color (colors). The entry node is sorted according to key, and the entry node contains the contents of value.
When the red-black number is sorted, it is sorted according to the key in the entry, and the key comparison in entry is judged according to the comparator comparator.
The size is the number of nodes in the red-black number.

About the red-black number of the specific algorithm, please refer to the "Red and Black tree (i) principle and algorithm detailed introduction."

3rd part treemap traversal mode

3.1 Traversal TreeMap key value pairs

Step one: Gets the set collection of the TreeMap "key value pairs" based on EntrySet ().
Step two: Iterate through the "first step" of the collection through the iterator iterator.

Suppose the map is a
string type for the key in the TreeMap object/map, and value is an integer
integ = null;
Iterator iter = Map.entryset (). iterator ();
while (Iter.hasnext ()) {
    Map.entry Entry = (map.entry) iter.next ();
    Gets the key
    key = (String) entry.getkey ();
        Gets the value
    Integ = (Integer) entry.getvalue ();

3.2 Traversing the TreeMap key

Step one: Gets the set collection of the "keys" of the TreeMap according to Keyset ().
Step two: Iterate through the "first step" of the collection through the iterator iterator.

Suppose the map is a
string type for the key in the TreeMap object/map, and value is the integer type
string key = null;
Integer integ = null;
Iterator iter = Map.keyset (). iterator ();
while (Iter.hasnext ()) {
        //get key
    key = (String) iter.next ();
        Gets the value
    Integ = (Integer) map.get (key) according to key;

3.3 Traversing the TreeMap value

Step one: Gets the collection of TreeMap's "values" based on value ().
Step two: Iterate through the "first step" of the collection through the iterator iterator.

Suppose the map is a
string type for the key in the TreeMap object/map, and value is an integer of integer
value = null;
Collection C = map.values ();
Iterator iter= c.iterator ();
while (Iter.hasnext ()) {
    value = (Integer) iter.next ();
}


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.