📜  ConcurrentNavigableMap接口

📅  最后修改于: 2020-11-15 04:01:01             🧑  作者: Mango


java.util.concurrent.ConcurrentNavigableMap接口是ConcurrentMap接口的子接口,并且支持NavigableMap操作,并对其递归的子地图和近似匹配进行递归。

ConcurrentMap方法

Sr.No. Method & Description
1

NavigableSet descendingKeySet()

Returns a reverse order NavigableSet view of the keys contained in this map.

2

ConcurrentNavigableMap descendingMap()

Returns a reverse order view of the mappings contained in this map.

3

ConcurrentNavigableMap headMap(K toKey)

Returns a view of the portion of this map whose keys are strictly less than toKey.

4

ConcurrentNavigableMap 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 keySet()

Returns a NavigableSet view of the keys contained in this map.

6

NavigableSet navigableKeySet()

Returns a NavigableSet view of the keys contained in this map.

7

ConcurrentNavigableMap 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 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 tailMap(K fromKey)

Returns a view of the portion of this map whose keys are greater than or equal to fromKey.

10

ConcurrentNavigableMap 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.

下面的TestThread程序显示ConcurrentNavigableMap的用法。

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

public class TestThread {

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

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

这将产生以下结果。

输出

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}