Java Concurrency 简明教程

ConcurrentNavigableMap Interface

一个 java.util.concurrent.ConcurrentNavigableMap 接口是 ConcurrentMap 接口的子接口,并且支持 NavigableMap 操作,以及对其可导航子映射和近似匹配进行递归操作。

A java.util.concurrent.ConcurrentNavigableMap interface is a subinterface of ConcurrentMap interface, and supports NavigableMap operations, and recursively so for its navigable sub-maps, and approximate matches.

ConcurrentMap Methods

Sr.No.

Method & Description

1

NavigableSet<K> descendingKeySet() Returns a reverse order NavigableSet view of the keys contained in this map.

2

ConcurrentNavigableMap<K,V> descendingMap() Returns a reverse order view of the mappings contained in this map.

3

ConcurrentNavigableMap<K,V> headMap(K toKey) Returns a view of the portion of this map whose keys are strictly less than toKey.

4

ConcurrentNavigableMap<K,V> headMap(K toKey, boolean inclusive) Returns a view of the portion of this map whose keys are less than (or equal to, if inclusive is true) toKey.

5

NavigableSet<K> keySet() Returns a NavigableSet view of the keys contained in this map.

6

NavigableSet<K> navigableKeySet() Returns a NavigableSet view of the keys contained in this map.

7

ConcurrentNavigableMap<K,V> subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) Returns a view of the portion of this map whose keys range from fromKey to toKey.

8

ConcurrentNavigableMap<K,V> subMap(K fromKey, K toKey) Returns a view of the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive.

9

ConcurrentNavigableMap<K,V> tailMap(K fromKey) Returns a view of the portion of this map whose keys are greater than or equal to fromKey.

10

ConcurrentNavigableMap<K,V> tailMap(K fromKey, boolean inclusive) Returns a view of the portion of this map whose keys are greater than (or equal to, if inclusive is true) fromKey.

Example

以下 TestThread 程序演示了 ConcurrentNavigableMap 的用法。

The following TestThread program shows usage of ConcurrentNavigableMap.

import java.util.concurrent.ConcurrentNavigableMap;
import java.util.concurrent.ConcurrentSkipListMap;

public class TestThread {

   public static void main(final String[] arguments) {
      ConcurrentNavigableMap<String,String> map =
         new ConcurrentSkipListMap<String, String>();

      map.put("1", "One");
      map.put("2", "Two");
      map.put("3", "Three");
      map.put("5", "Five");
      map.put("6", "Six");

      System.out.println("Initial ConcurrentHashMap: "+map);
      System.out.println("HeadMap(\"2\") of ConcurrentHashMap: "+map.headMap("2"));
      System.out.println("TailMap(\"2\") of ConcurrentHashMap: "+map.tailMap("2"));
      System.out.println(
         "SubMap(\"2\", \"4\") of ConcurrentHashMap: "+map.subMap("2","4"));
   }
}

这将产生以下结果。

This will produce the following result.

Output

Initial ConcurrentHashMap: {1 = One, 2 = Two, 3 = Three, 5 = Five, 6 = Six}
HeadMap("2") of ConcurrentHashMap: {1 = One}
TailMap("2") of ConcurrentHashMap: {2 = Two, 3 = Three, 5 = Five, 6 = Six}
SubMap("2", "4") of ConcurrentHashMap: {2 = Two, 3 = Three}