Java并发性ConcurrentNavigableMap接口


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

并发映射方法

Sr.No. 方法和描述
1 NavigableSet descendingKeySet() 返回此映射中包含的键的逆序NavigableSet视图。
2 ConcurrentNavigableMap descendingMap() 返回此映射中包含的映射的逆序视图。
3 ConcurrentNavigableMap headMap(K toKey) 返回此映射的关键字严格小于toKey的部分的视图。
4 ConcurrentNavigableMap headMap(K toKey, boolean inclusive) 返回此映射关键字小于(或等于,如果包含为真)toKey的部分的视图。
5 NavigableSet keySet() 返回此映射中包含的键的NavigableSet视图。
6 NavigableSet navigableKeySet() 返回此映射中包含的键的NavigableSet视图。
7 ConcurrentNavigableMap subMap(K fromKey,boolean fromInclusive,K toKey,boolean toInclusive) 返回此映射的部分视图,其键的范围从fromKey到toKey。
8 ConcurrentNavigableMap subMap(K fromKey,K toKey) 返回此映射的部分视图,其键范围从fromKey(包含)到toKey(不包括)。
9 ConcurrentNavigableMap tailMap(K fromKey) 返回此映射的关键字大于或等于fromKey的部分的视图。
10 ConcurrentNavigableMap tailMap(K fromKey, boolean inclusive) 返回此映射关键字大于(或等于,如果包含为真)fromKey的部分的视图。

以下TestThread程序显示了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"));
   }  
}

这将产生以下结果。

输出

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}