Java Collection Framework树形图

The TreeMap class is a implementation of Map interface based on red-black tree. It provides an efficient way of storing key-value pairs in sorted order.

TreeMap类是基于红黑树的Map接口的实现。 它提供了一种有效的方式来按排序顺序存储键值对。

It is similar to HashMap class except it is sorted in the ascending order of its keys. It is not suitable for thread-safe operations due to its unsynchronized nature.

它与HashMap类相似,不同之处在于它按其键的升序排序。 由于其不同步的性质,因此不适合线程安全操作。

It implements NavigableMap interface and extends AbstractMap class. Declaration of this class is given below.

它实现了NavigableMap接口并扩展了AbstractMap类。 此类的声明在下面给出。

TreeMap声明 (TreeMap Declaration )

public class TreeMap<K,V>extends AbstractMap<K,V>implements NavigableMap<K,V>, Cloneable, Serializable

重要事项: (Important Points:)

  • It contains only unique elements.

    它仅包含唯一元素。

  • It cannot have a null key but can have multiple null values.

    它不能具有null键,但可以具有多个null值。

  • It is non synchronized.

    它是不同步的。

  • It maintains ascending order.

    它保持升序。

TreeMap构造函数 (TreeMap Constructors)

ConstructorDescription
TreeMap()It creates a new, empty tree map, using the natural ordering of its keys.
TreeMap(Comparator<? super K> comparator)It created a new, empty tree map, ordered according to the given comparator.
TreeMap(Map<? extends K,? extends V> m)It creates a new tree map containing the same mappings as the given map.
TreeMap(SortedMap<K,? extends V> m)It creates a new tree map containing sortedamp.
建设者 描述
TreeMap() 它使用其键的自然顺序创建一个新的空树形图。
TreeMap(Comparator <?super K>比较器) 它创建了一个新的空树图,并根据给定的比较器排序。
TreeMap(Map <?扩展K ,?扩展V> m) 它创建一个新的树图,其中包含与给定图相同的映射。
TreeMap(SortedMap <K ,?扩展V> m) 它创建一个包含sortedamp的新树图。

示例:创建TreeMap (Example : Creating TreeMap)

Lets take an example to understand the creation of treeMap which is initially empty.

让我们举个例子来了解treeMap的创建,它最初是空的。

class Demo
{
  public static void main(String args[])
  {
    // Creating TreeMap
    TreeMap<String,Integer> treeMap = new TreeMap<String,Integer>();
    // Displaying TreeMap
    System.out.println(treeMap);
  }
}

{}

{}

TreeMap方法 (TreeMap Methods)

This table contains the methods of TreeMap that can be used to manipulate objects.

该表包含可用于操作对象的TreeMap方法。

Map.Entry<K,V> ceilingEntry(K key)It returns the key-value pair having the least key, greater than or equal to the specified key, or null if there is no such key.
K ceilingKey(K key)It returns the least key, greater than the specified key or null if there is no such key.
void clear()It removes all the key-value pairs from a map.
Object clone()It returns a shallow copy of TreeMap instance.
Comparator<? super K> comparator()It returns the comparator that arranges the key in order, or null if the map uses the natural ordering.
NavigableSet<K> descendingKeySet()It returns a reverse order NavigableSet view of the keys contained in the map.
NavigableMap<K,V> descendingMap()It returns the specified key-value pairs in descending order.
Map.Entry firstEntry()It returns the key-value pair having the least key.
Map.Entry<K,V> floorEntry(K key)It returns the greatest key, less than or equal to the specified key, or null if there is no such key.
void forEach(BiConsumer<? super K,? super V> action)It performs the given action for each entry in the map until all entries have been processed or the action throws an exception.
SortedMap<K,V> headMap(K toKey)It returns the key-value pairs whose keys are strictly less than toKey.
NavigableMap<K,V> headMap(K toKey, boolean inclusive)It returns the key-value pairs whose keys are less than (or equal to if inclusive is true) toKey.
Map.Entry<K,V> higherEntry(K key)It returns the least key strictly greater than the given key, or null if there is no such key.
K higherKey(K key)It is used to return true if this map contains a mapping for the specified key.
Set keySet()It returns the collection of keys exist in the map.
Map.Entry<K,V> lastEntry()It returns the key-value pair having the greatest key, or null if there is no such key.
Map.Entry<K,V> lowerEntry(K key)It returns a key-value mapping associated with the greatest key strictly less than the given key, or null if there is no such key.
K lowerKey(K key)It returns the greatest key strictly less than the given key, or null if there is no such key.
NavigableSet navigableKeySet() It returns a NavigableSet view of the keys contained in this map.
Map.Entry<K,V> pollFirstEntry()It removes and returns a key-value mapping associated with the least key in this map, or null if the map is empty.
Map.Entry<K,V> pollLastEntry()It removes and returns a key-value mapping associated with the greatest key in this map, or null if the map is empty.
V put(K key, V value)It inserts the specified value with the specified key in the map.
void putAll(Map<? extends K,? extends V> map)It is used to copy all the key-value pair from one map to another map.
V replace(K key, V value)It replaces the specified value for a specified key.
boolean replace(K key, V oldValue, V newValue)It replaces the old value with the new value for a specified key.
void replaceAll(BiFunction<? super K,? super V,? extends V> function)It replaces each entry's value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception.
NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive)It returns key-value pairs whose keys range from fromKey to toKey.
SortedMap<K,V> subMap(K fromKey, K toKey)It returns key-value pairs whose keys range from fromKey, inclusive, to toKey, exclusive.
SortedMap<K,V> tailMap(K fromKey)It returns key-value pairs whose keys are greater than or equal to fromKey.
NavigableMap<K,V> tailMap(K fromKey, boolean inclusive)It returns key-value pairs whose keys are greater than (or equal to, if inclusive is true) fromKey.
boolean containsKey(Object key)It returns true if the map contains a mapping for the specified key.
boolean containsValue(Object value)It returns true if the map maps one or more keys to the specified value.
K firstKey()It is used to return the first (lowest) key currently in this sorted map.
V get(Object key)It is used to return the value to which the map maps the specified key.
K lastKey()It is used to return the last (highest) key currently in the sorted map.
V remove(Object key)It removes the key-value pair of the specified key from the map.
int size()It returns the number of key-value pairs exists in the hashtable.
Collection values()It returns a collection view of the values contained in the map.
Map.Entry <K,V> ceilingEntry(K键) 它返回具有最小键值(大于或等于指定键)的键值对,如果没有这样的键,则返回null。
K ceilingKey(K键) 它返回最小的键,大于指定的键;如果没有这样的键,则返回null。
无效clear() 它将所有键值对从映射中删除。
对象clone() 它返回TreeMap实例的浅表副本。
比较器<? 超级K>比较器() 它返回按顺序排列键的比较器;如果映射使用自然顺序,则返回null。
NavigableSet <K> endingKeySet() 它返回映射中包含的键的逆序NavigableSet视图。
NavigableMap <K,V> endingMap() 它以降序返回指定的键值对。
Map.Entry firstEntry() 它返回键值最小的键值对。
Map.Entry <K,V> floorEntry(K键) 它返回最大的键,小于或等于指定的键;如果没有这样的键,则返回null。
void forEach(BiConsumer <?super K,?super V>操作) 它对映射中的每个条目执行给定的操作,直到所有条目都已处理或该操作引发异常为止。
SortedMap <K,V> headMap(K toKey) 它返回键值严格小于toKey的键值对。
NavigableMap <K,V> headMap(K toKey,包含布尔值) 它返回其键小于(或等于(如果包含)为true的话)toKey的键值对。
Map.Entry <K,V> upperEntry(K键) 它返回严格大于给定键的最小键;如果没有这样的键,则返回null。
高K键(K键) 如果此映射包含指定键的映射,则用于返回true。
设置keySet() 它返回映射中存在的键的集合。
Map.Entry <K,V> lastEntry() 它返回具有最大键的键值对,如果没有这样的键,则返回null。
Map.Entry <K,V> lowerEntry(K键) 它返回与最大键严格小于给定键的键-值映射关系;如果没有这样的键,则返回null。
K LowerKey(K键) 它返回严格小于给定键的最大键;如果没有这样的键,则返回null。
导航集 navigableKeySet() 它返回此映射中包含的键的NavigableSet视图。
Map.Entry <K,V> pollFirstEntry() 它删除并返回与此映射中最小键关联的键-值映射;如果映射为空,则返回null。
Map.Entry <K,V> pollLastEntry() 它删除并返回与此映射中最大键关联的键-值映射;如果映射为空,则返回null。
V put(K键,V值) 它在地图中插入具有指定键的指定值。
void putAll(Map <?扩展K ,?扩展V> map) 它用于将所有键值对从一个映射复制到另一个映射。
V替换(K键,V值) 它将指定的值替换为指定的键。
布尔值替换(K键,V oldValue,V newValue) 它用指定键的新值替换旧值。
void replaceAll(BiFunction <?super K,?super V,?extended V>函数) 它用在该条目上调用给定函数的结果替换每个条目的值,直到处理完所有条目或该函数引发异常为止。
NavigableMap <K,V>子映射(K fromKey,boolean fromInclusive,K toKey,boolean toInclusive) 它返回键-值对,其键的范围从fromKey到toKey。
SortedMap <K,V>子映射(K fromKey,K toKey) 它返回键-值对,其键的范围从fromKey(包括)到toKey(不包括)。
SortedMap <K,V> tailMap(K fromKey) 它返回键值大于或等于fromKey的键值对。
NavigableMap <K,V> tailMap(K fromKey,包含布尔值) 它从键返回大于(或等于,如果包含在内,则为真)的键/值对。
boolean containsKey(Object key) 如果映射包含指定键的映射,则返回true。
布尔containsValue(对象值) 如果映射将一个或多个键映射到指定值,则返回true。
K firstKey() 它用于返回此排序映射中当前的第一个(最低)键。
V get(对象键) 它用于返回映射将指定键映射到的值。
K lastKey() 它用于返回排序映射中当前的最后一个(最高)键。
V remove(对象键) 它从映射中删除指定键的键值对。
int size() 它返回哈希表中存在的键值对的数量。
集合值() 它返回映射中包含的值的集合视图。

示例:将元素添加到TreeMap (Example : Adding Elements to TreeMap)

After creating TreeMap, lets add elements to it. We used put method to insert elements which takes two arguments: first is key and second is value.

创建TreeMap之后,让我们向其中添加元素。 我们使用put方法插入带有两个参数的元素:第一个是键,第二个是值。

class Demo
{
  public static void main(String args[])
  {
    // Creating TreeMap
    TreeMap<String,Integer> treeMap = new TreeMap<String,Integer>();
    // Adding elements
    treeMap.put("a",100);
    treeMap.put("b",200);
    treeMap.put("c",300);
    treeMap.put("d",400);
    // Displaying TreeMap
    System.out.println(treeMap);
  }

{a=100, b=200, c=300, d=400}

{a = 100,b = 200,c = 300,d = 400}

获取LinkedHashMap中的第一个和最后一个密钥 (Get first and last key in LinkedHashMap)

We can get first and last element of the map using the firstEntry() and lastEntry() method. It returns a pair of key and value.

我们可以使用firstEntry()和lastEntry()方法获取地图的第一个和最后一个元素。 它返回一对键和值。

import java.util.*;

class Demo
{
  public static void main(String args[])
  {
    // Creating TreeMap
    TreeMap<String,Integer> treeMap = new TreeMap<String,Integer>();
    // Adding elements
    treeMap.put("a",100);
    treeMap.put("b",200);
    treeMap.put("c",300);
    treeMap.put("d",400);
    // Displaying TreeMap
    System.out.println(treeMap);
    // First Element
    System.out.println("First Element: "+treeMap.firstEntry());
    // Last Element
    System.out.println("Last Element: "+treeMap.lastEntry());
    
  }
}

{a=100, b=200, c=300, d=400} First Element: a=100 Last Element: d=400

{a = 100,b = 200,c = 300,d = 400}第一个元素:a = 100最后一个元素:d = 400

示例:查找TreeMap的HeadMap和TailMap (Example: Find HeadMap and TailMap of the TreeMap)

We can get head elements and tail elements of the TreeMap by using the headMap() and tailMap() methods. These methods return a map containing the elements.

我们可以使用headMap()和tailMap()方法获取TreeMap的head元素和tail元素。 这些方法返回包含元素的映射。

import java.util.*;

class Demo
{
  public static void main(String args[])
  {
    // Creating TreeMap
    TreeMap<String,Integer> treeMap = new TreeMap<String,Integer>();
    // Adding elements
    treeMap.put("a",100);
    treeMap.put("b",200);
    treeMap.put("c",300);
    treeMap.put("d",400);
    // Displaying TreeMap
    System.out.println(treeMap);
    // Head Map
    System.out.println("Head Map: "+treeMap.headMap("c"));
    // Tail Map
    System.out.println("Tail Map: "+treeMap.tailMap("c"));
    
  }
}

{a=100, b=200, c=300, d=400} Head Map: {a=100, b=200} Tail Map: {c=300, d=400}

{a = 100,b = 200,c = 300,d = 400}头部图:{a = 100,b = 200}尾部图:{c = 300,d = 400}

示例:迭代TreeMap (Example: Iterating TreeMap)

In this example, we are creating TreeMap to store data. It uses tree to store data and data is always in sorted order and iterating the map using loop. See the below example.

在此示例中,我们将创建TreeMap来存储数据。 它使用树来存储数据,并且数据始终按排序顺序,并使用循环来迭代地图。 请参见以下示例。

import java.util.*;

class Demo
{
  public static void main(String args[])
  {
    TreeMap<String,Integer> tm = new TreeMap<String,Integer>();
    tm.put("a",100);
    tm.put("b",200);
    tm.put("c",300);
    tm.put("d",400);

    Set<Map.Entry<String,Integer>> st = tm.entrySet();
    for(Map.Entry<String,Integer> me:st)
    {
      System.out.print(me.getKey()+":");
      System.out.println(me.getValue());
    }
  }
}

a:100 b:200 c:300 d:400

a:100 b:200 c:300 d:400

翻译自: https://www.studytonight.com/java/treemap-in-collection-framework.php

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值