首页 > 其他分享 >Map---SortedMap&NavigableMap&TreeMap

Map---SortedMap&NavigableMap&TreeMap

时间:2023-11-08 16:34:55浏览次数:33  
标签:Map code map SortedMap TreeMap --- key NavigableMap

SortedMap

概述

A {@link Map} that further provides a <em>total ordering</em> on its keys.
The map is ordered according to the {@linkplain Comparable natural ordering} of its keys, or by a {@link Comparator} typically provided at sorted map creation time.
This order is reflected when iterating over the sorted map's collection views (returned by the {@code entrySet}, {@code keySet} and {@code values} methods).
Several additional operations are provided to take advantage of the ordering. (This interface is the map analogue of {@link SortedSet}.)

SortedMap提供key排序

SortedMap使用 元素key的Comparable自然排序 或 创建时的Comparator;

使用SortedMap的entrySet、keySet、values会返回已排序的内容;

 

All keys inserted into a sorted map must implement the {@code Comparable} interface (or be accepted by the specified comparator).
Furthermore, all such keys must be <em>mutually comparable</em>: {@code k1.compareTo(k2)} (or {@code comparator.compare(k1, k2)}) must not throw a {@code ClassCastException} for any keys {@code k1} and {@code k2} in the sorted map.
Attempts to violate this restriction will cause the offending method or constructor invocation to throw a {@code ClassCastException}.

所有的key必须实现Comparable接口 或 指定comparator;

所有的key必须是可比较的;

 

public interface SortedMap<K,V> extends Map<K,V> {
        
    }

  

NavigableMap

概述

A {@link SortedMap} extended with navigation methods returning the closest matches for given search targets.
Methods {@code lowerEntry}, {@code floorEntry}, {@code ceilingEntry}, and {@code higherEntry} return {@code Map.Entry} objects associated with keys respectively less than, less than or equal, greater than or equal, and greater than a given key, returning {@code null} if there is no such key.

NavigableMap对SortedMap做了扩展:返回接近搜索目标的内容

lowerEntry、floorEntry、ceilingEntry、higherEntry返回 比key<、<=、>=、>的Map.Entry对象;

Similarly, methods {@code lowerKey}, {@code floorKey}, {@code ceilingKey}, and {@code higherKey} return only the associated keys.
All of these methods are designed for locating, not traversing entries.

lowerKey、floorKey、ceilingKey、higherKey返回关联的key;

所有的这些方法被设计用来定位查找

 

A {@code NavigableMap} may be accessed and traversed in either ascending or descending key order.
The {@code descendingMap} method returns a view of the map with the senses of all relational and directional methods inverted.
The performance of ascending operations and views is likely to be faster than that of descending ones.

NavigableMap可以按key的 升序/降序 访问

descendingMap方法返回一个降序的view;

升序的操作效率高于降序操作;

 

This interface additionally defines methods {@code firstEntry}, {@code pollFirstEntry}, {@code lastEntry}, and {@code pollLastEntry} that return and/or remove the least and greatest mappings, if any exist, else returning {@code null}.

NavigableMap定义了firstEntry、pollFirstEntry、lastEntry、pollLastEntry;

 

public interface NavigableMap<K,V> extends SortedMap<K,V> {
        
    }

  

TreeMap

概述

  

 

A Red-Black tree based {@link NavigableMap} implementation.
The map is sorted according to the {@linkplain Comparable natural ordering} of its keys, or by a {@link Comparator} provided at map creation time, depending on which constructor is used.

TreeMap是基于NavigableMap的 红黑树实现;

TreeMap使用 元素key的Comparable自然排序 或 创建时指定的Comparator;

 

This implementation provides guaranteed log(n) time cost for the {@code containsKey}, {@code get}, {@code put} and {@code remove} operations. 

containsKey、get、Put、remove操作 花费log(n)时间复杂度

 

Note that the ordering maintained by a tree map, like any sorted map, and whether or not an explicit comparator is provided, must be <em>consistent with {@code equals}</em> if this sorted map is to correctly implement the {@code Map} interface. 

如果实现map接口,TreeMap的比较必须与equals一致;

This is so because the {@code Map} interface is defined in terms of the {@code equals} operation, but a sorted map performs all key comparisons using its {@code compareTo} (or {@code compare}) method, so two keys that are deemed equal by this method are, from the standpoint of the sorted map, equal. 

因为map使用equals比较,TreeMap使用Compare/CompareTo比较;

 

Note that this implementation is not synchronized.
If multiple threads access a map concurrently, and at least one of the threads modifies the map structurally, it <em>must</em> be synchronized externally.

TreeMap的实现是线程非同步的;

如果多个线程并发进行结构修改TreeMap,必须在外部做同步;

(A structural modification is any operation that adds or deletes one or more mappings; merely changing the value associated with an existing key is not a structural modification.) 

结构修改指 add、delete或其他操作;仅改变key的value不是结构修改;

If no such object exists, the map should be "wrapped" using the {@link Collections#synchronizedSortedMap Collections.synchronizedSortedMap} method.  

可以使用Collections.synchronizedSortedMap;

 

The iterators returned by the {@code iterator} method of the collections returned by all of this class's "collection view methods" are <em>fail-fast</em>: if the map is structurally modified at any time after the iterator is created, in any way except through the iterator's own {@code remove} method,

the iterator will throw a {@link ConcurrentModificationException}. 

TreeMap的iterator方法是fail-fast:如果iterator时操作remove(除了iterator的remove),将会抛出ConcurrentModificationException;

 

All {@code Map.Entry} pairs returned by methods in this class and its views represent snapshots of mappings at the time they were produced.
They do <strong>not</strong> support the {@code Entry.setValue} method.
(Note however that it is possible to change mappings in the associated map using {@code put}.)

方法返回的Map.Entry都是快照;

不支持Entry.setValue

(可以通过TreeMap的Put修改内容)

 

标签:Map,code,map,SortedMap,TreeMap,---,key,NavigableMap
From: https://www.cnblogs.com/anpeiyong/p/17817682.html

相关文章

  • 无涯教程-批处理 - Left String函数
    这用于从字符串的开头提取字符。@echooffsetstr=HelloLearnfkecho%str%setstr=%str:~0,5%echo%str%关于上述程序,要注意的关键是〜0,5用于指定需要显示的字符。在这种情况下,无涯教程说应该showcharacter0到5。上面的命令产生以下输出。HelloLearnfkHello参......
  • sniperoj-pwn100-shellcode-x86-64
    来源本题来自ctfi-wiki,是基本ROP中shellcode的一道例题查看保护程序只开启了位置无关保护PIE并且有可写可执行的段反汇编发现程序中直接输出了buf的地址,所以PIE就不起作用了可以看到,后面read函数还调用了buf,很显然就是要利用buf这个段了然后在汇编代码发现调用read......
  • TH系列新品16口ACC-5595反射内存交换机
    一、光纤网简介 在半实物仿真系统等实时系统中,要求系统各部分之间的数据传输具有很高的实时性,而传统的网络技术,如以太网、FDDI等在实时应用中存在以下缺点:(1)数据传输速率不高;(2)数据传输实时性差,传输延迟较大且延迟具有不可预测性;(3)网络通信需要借助多种网络协议来完成,通讯效率较低。......
  • Apipost-Helper:IDEA中的类postman工具
    今天给大家推荐一款IDEA插件:Apipost-Helper-2.0,写完代码IDEA内一键生成API文档,无需安装、打开任何其他软件;写完代码IDEA内一键调试,无需安装、打开任何其他软件;生成API目录树,双击即可快速定位API定义的代码…非常好用!主要包含以下功能:1、无侵入生成API文档编写完代码后,只需右键uploa......
  • GEPCI-5565PIORC-110000反射内存卡
    反射内存实时网的特点VMIC反射内存是一种通过局域网在互连的计算机间提供的数据传输的技术,强实时网络设计人员已经越来越多地采用这种技术。VMIC反射内存实时局域网的概念十分简单,就是设计一种网络内存板,在分布系统中实现内存至内存的通信,并且没有软件开销。每台结点机上插一块反射......
  • Apipost-Helper:IDEA中的类postman工具
    今天给大家推荐一款IDEA插件:Apipost-Helper-2.0,写完代码IDEA内一键生成API文档,无需安装、打开任何其他软件;写完代码IDEA内一键调试,无需安装、打开任何其他软件;生成API目录树,双击即可快速定位API定义的代码…非常好用!主要包含以下功能:1、无侵入生成API文档编写完代码后,只需右键up......
  • 使用TS进行Vue-Router的Meta类型扩展
    目录1、前言2、解决1、前言使用Vue-Router时,会将一些字段信息附加到路由的Meta对象里面,比如图标icon,标题,权限等,如下:{path:'/billboard/board/:boardId',name:'billboardBoard',props:true,component:()=>import('@/views/billboard/board.vue'),meta:{......
  • 【2023-10-30】乡土人情
    20:00与简单的误解不同的是,偏见会积极抗拒所有可能撼动它的证据。当我们的偏见与现实发生冲突时,我们倾向于意气用事。                                                 ......
  • python初学者学习笔记-第七章-面向对象
    chapter7/面向对象7.1面向对象基础7.1.1定义类:同一类的属性和方法的集合;对象:类的实例化就是对象;7.1.2特征类的三大特性:封装,继承,和多态;封装封装就是将属性和方法相捆绑,形成一个整体;封装可以提高程序的简洁性和安全性;使用者只需调用程序的相关接口,不必关注程序的实现......
  • Redis迁移工具redis-migrate-tool的使用
    背景:公司由于以前没有同意规划,导致出现好几个redis实例,现在需要整合到一起,查到还有redis-migrate-tool工具,所以把使用情况写一下,以备后用参考 一、下载编译#从git库直接拉取gitclonehttps://github.com/vipshop/redis-migrate-tool.git#CentOS安装必......