键值对Map-07

举报
kwan的解忧杂货铺 发表于 2024/08/08 20:54:32 2024/08/08
【摘要】 ConcurrentNavigableMap 是一个接口,ConcurrentSkipListMap 是 ConcurrentNavigableMap 的一个实现类 1.headMapheadMap(T toKey)方法返回小于给定键的键的映射视图。//ConcurrentSkipListMappublic ConcurrentNavigableMap<K,V> headMap(K toKe...

ConcurrentNavigableMap 是一个接口,ConcurrentSkipListMap 是 ConcurrentNavigableMap 的一个实现类

1.headMap

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

//ConcurrentSkipListMap
public ConcurrentNavigableMap<K,V> headMap(K toKey,  boolean inclusive) {
    if (toKey == null) throw new NullPointerException();
    return new SubMap<K,V> (this, null, false, toKey, inclusive, false);
}
SubMap(ConcurrentSkipListMap<K,V> map,
             K fromKey, boolean fromInclusive,
             K toKey, boolean toInclusive,
             boolean isDescending) {
          Comparator<? super K> cmp = map.comparator;
          if (fromKey != null && toKey != null &&
              cpr(cmp, fromKey, toKey) > 0)
              throw new IllegalArgumentException("inconsistent range");
          this.m = map;
          this.lo = fromKey;
          this.hi = toKey;
          this.loInclusive = fromInclusive;
          this.hiInclusive = toInclusive;
          this.isDescending = isDescending;
}
public static void main(String[] args) {
      ConcurrentNavigableMap map = new ConcurrentSkipListMap();
      map.put("1", "one");
      map.put("2", "two");
      map.put("3", "three");
      map.put("4", "three");
      map.put("5", "three");
      map.put("6", "three");
      map.put("7", "three");
      ConcurrentNavigableMap headMap = map.headMap("5");
      Set<String> keySet = headMap.keySet();
      for (String key : keySet) {
          System.out.println("key:" + key + " , value:" + headMap.get(key));
      }
}
//输出 1,2,3,4

2.tailMap

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

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

public ConcurrentNavigableMap<K,V> tailMap(K fromKey,
                                               boolean inclusive) {
        if (fromKey == null)
            throw new NullPointerException();
        return new SubMap<K,V>
            (this, fromKey, inclusive, null, false, false);
    }
public static void main(String[] args) {
      ConcurrentNavigableMap map = new ConcurrentSkipListMap();
      map.put("1", "one");
      map.put("2", "two");
      map.put("3", "three");
      map.put("4", "three");
      map.put("5", "three");
      map.put("6", "three");
      map.put("7", "three");
      ConcurrentNavigableMap headMap = map.tailMap("5");
      Set<String> keySet = headMap.keySet();
      for (String key : keySet) {
          System.out.println("key:" + key + " , value:" + headMap.get(key));
      }
  }
//返回5,6,7

3.subMap

subMap()方法返回了一个从 from (including)到 to (excluding)的 map 试图,并且 key 大于等于 from 小于 to

public ConcurrentNavigableMap<K,V> subMap(K fromKey, K toKey) {
        return subMap(fromKey, true, toKey, false);
    }
public static void main(String[] args) {
      ConcurrentNavigableMap map = new ConcurrentSkipListMap();
      map.put("1", "one");
      map.put("5", "three");
      map.put("2", "two");
      map.put("3", "three");
      map.put("4", "three");
      map.put("6", "three");
      map.put("7", "three");
      ConcurrentNavigableMap headMap = map.subMap("3", "5");
      Set<String> keySet = headMap.keySet();
      for (String key : keySet) {
          System.out.println("key:" + key + " , value:" + headMap.get(key));
      }
  }
//输出 3,4

4.descendingKeySet

倒序排列 key

public static void main(String[] args) {
      ConcurrentNavigableMap map = new ConcurrentSkipListMap();
      map.put("1", "one");
      map.put("5", "three");
      map.put("2", "two");
      map.put("3", "three");
      map.put("4", "three");
      map.put("6", "three");
      map.put("7", "three");
      final NavigableSet navigableSet = map.descendingKeySet();
      System.out.println(JSON.toJSONString(navigableSet));
  }

5.navigableKeySet

正序排列 key

public static void main(String[] args) {
      ConcurrentNavigableMap map = new ConcurrentSkipListMap();
      map.put("1", "one");
      map.put("5", "three");
      map.put("2", "two");
      map.put("3", "three");
      map.put("4", "three");
      map.put("6", "three");
      map.put("7", "three");
      final NavigableSet navigableSet = map.navigableKeySet();
      System.out.println(JSON.toJSONString(navigableSet));
  }

6.descendingMap

倒序排列 map

public static void main(String[] args) {
      ConcurrentNavigableMap map = new ConcurrentSkipListMap();
      map.put("1", "one");
      map.put("5", "three");
      map.put("2", "two");
      map.put("3", "three");
      map.put("4", "three");
      map.put("6", "three");
      map.put("7", "three");
      ConcurrentNavigableMap<Integer, String> descendingMap = map.descendingMap();
      System.out.println(JSON.toJSONString(descendingMap));
}

7.ceilingEntry

获取指定 key 的 map

public static void main(String[] args) {
      ConcurrentNavigableMap map = new ConcurrentSkipListMap();
      map.put("1", "one");
      map.put("5", "three");
      map.put("2", "two");
      map.put("3", "three");
      map.put("4", "three");
      map.put("6", "three");
      map.put("7", "three");
      Map.Entry<String, String> ceilingEntry = map.ceilingEntry("4");
      System.out.println(JSON.toJSONString(ceilingEntry));
  }

8.ceilingKey

获取指定 key 的 key

public static void main(String[] args) {
      ConcurrentNavigableMap map = new ConcurrentSkipListMap();
      map.put("1", "one");
      map.put("5", "three");
      map.put("2", "two");
      map.put("3", "three");
      map.put("4", "three");
      map.put("6", "three");
      map.put("7", "three");
      String ceilingKey = (String) map.ceilingKey("4");
      System.out.println(JSON.toJSONString(ceilingKey));
  }

9.lastEntry

//取比当前key大一个的map值
  Map.Entry<Integer, String> higherEntry = concurrentNavigableMap.higherEntry(2);
  System.out.println(JSON.toJSONString(higherEntry));

  //取比当前key小一个的map值
  Map.Entry<Integer, String> lowerEntry = concurrentNavigableMap.lowerEntry(2);
  System.out.println(JSON.toJSONString(lowerEntry));

  //取最后一个map值
  Map.Entry<Integer, String> lastEntry = concurrentNavigableMap.lastEntry();
  System.out.println(JSON.toJSONString(lastEntry));
【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。