JAVA多线程-ConcurrentNavigableMap

java.util.concurrent.ConcurrentNavigableMap接口是 java.util.NavigableMap的一个子接口支持并发访问 ,也为其子map提供了并发访问。子map是可以通过多个方法返回的map例如, headMap()subMap() tailMap().

这里只解释 ConcurrentNavigableMap中新加的方法,其他的方法可以参考NavigableMap 

headMap()

headMapT toKey)方法返回小于给定键的键的映射视图。

如果改变原来的map,这些变化也会影响head map.

下面例子说明了headMap()方法的用法:

package com.ck.thread;



import static org.junit.jupiter.api.Assertions.*;



import java.util.concurrent.ConcurrentNavigableMap;

import java.util.concurrent.ConcurrentSkipListMap;



import org.junit.jupiter.api.Test;



class ConcurrentNavigableMapTest {

        @Test

    void test() {

        ConcurrentNavigableMap map = new ConcurrentSkipListMap();

        map.put("1", "one");

        map.put("2", "two");

        map.put("3", "three");

        ConcurrentNavigableMap headMap = map.headMap("2");

        Set<String> keySet = headMap.keySet();

        for(String key : keySet) {

            System.out.println("key:" + key +" , value:" + headMap.get(key));

        }

    }

}

运行结果:

key:1 , value:one

 headMap将指向只包含key为1的ConcurrentNavigableMap  , 因为只有这个键严格小于“2”

可以查看JavaDoc了解更多的关于整个方法的细节,以及它的重载版本如何工作。

tailMap()

tailMap(T fromKey)方法将返回包含或者大于fromKey的map视图 .

如果改变原来的map,这些变化也会影响tail map

下面例子说明了tailMap ()方法的用法

package com.ck.thread;



import static org.junit.jupiter.api.Assertions.*;


import java.util.Set;

import java.util.concurrent.ConcurrentNavigableMap;

import java.util.concurrent.ConcurrentSkipListMap;

import org.junit.jupiter.api.Test;

class ConcurrentNavigableMapTest {

    @Test

    void test() {

        ConcurrentNavigableMap map = new ConcurrentSkipListMap();

        map.put("1", "one");

        map.put("2", "two");

        map.put("3", "three");

        ConcurrentNavigableMap headMap = map.tailMap("2");

        Set<String> keySet = headMap.keySet();

        for(String key : keySet) {

            System.out.println("key:" + key +" , value:" + headMap.get(key));

        }

    }

}

运行结果:

key:2 , value:two

key:3 , value:three

tailMap包含了 keys "2" "3" ,因为这两个key大于等于给定的key”2”.

可以查看JavaDoc了解更多的关于整个方法的细节,以及它的重载版本如何工作。

subMap()

 subMap()方法返回了一个从from (including)to (excluding)map试图,并且key大于等于from小于to,下面是个例子:

package com.ck.thread;



import static org.junit.jupiter.api.Assertions.*;



import java.util.Set;

import java.util.concurrent.ConcurrentNavigableMap;

import java.util.concurrent.ConcurrentSkipListMap;


import org.junit.jupiter.api.Test;

class ConcurrentNavigableMapTest {

    @Test

    void test() {

        ConcurrentNavigableMap map = new ConcurrentSkipListMap();

        map.put("1", "one");

        map.put("2", "two");

        map.put("3", "three");

        ConcurrentNavigableMap headMap = map.subMap("2", "3");

        Set<String> keySet = headMap.keySet();

        for(String key : keySet) {

            System.out.println("key:" + key +" , value:" + headMap.get(key));

        }

    }


}

运行结果:

key:2 , value:two

返回的submap contains 只有 key"2", 因为大于等于,小样 "3".

更多方法

ConcurrentNavigableMap接口包含了更多的可用方法,例如 :

  • descendingKeySet()
  • descendingMap()
  • navigableKeySet()

更多的方法信息可以参考官方的javaDoc.

参考翻译:http://tutorials.jenkov.com/java-util-concurrent/concurrentnavigablemap.html

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值