Java Map集合

Map集合:


Map接口

     Map与List、Set接口不同,它是由一系列键值对组成的集合,提供了key到Value的映射。同时它也没有继承Collection。在Map中它保证了key与value之间的一一对应关系。也就是说一个key对应一个value,所以它不能存在相同的key值,当然value值可以相同

1.HashMap

      以哈希表数据结构实现,查找对象时通过哈希函数计算其位置,它是为快速查询而设计的,其内部定义了一个hash表数组(Entry[] table),元素会通过哈希转换函数将元素的哈希地址转换成数组中存放的索引,如果有冲突,则使用散列链表的形式将所有相同哈希地址的元素串起来,可能通过查看HashMap.Entry的源码它是一个单链表结构。

2.LinkedHashMap

     LinkedHashMap是HashMap的一个子类,它保留插入的顺序,如果需要输出的顺序和输入时的相同,那么就选用LinkedHashMap。
     LinkedHashMap是Map接口的哈希表和链接列表实现,具有可预知的迭代顺序。此实现提供所有可选的映射操作,并允许使用null值和null键。此类不保证映射的顺序,特别是它不保证该顺序恒久不变
     LinkedHashMap实现与HashMap的不同之处在于,后者维护着一个运行于所有条目的双重链接列表。此链接列表定义了迭代顺序,该迭代顺序可以是插入顺序或者是访问顺序
     根据链表中元素的顺序可以分为:按插入顺序的链表,和按访问顺序(调用get方法)的链表。默认是按插入顺序排序,如果指定按访问顺序排序,那么调用get方法后,会将这次访问的元素移至链表尾部,不断访问可以形成按访问顺序排序的链表。
     注意,此实现不是同步的。如果多个线程同时访问链接的哈希映射,而其中至少一个线程从结构上修改了该映射,则它必须保持外部同步。
     由于LinkedHashMap需要维护元素的插入顺序,因此性能略低于HashMap的性能,但在迭代访问Map里的全部元素时将有很好的性能,因为它以链表来维护内部顺序。

3.TreeMap

     TreeMap 是一个有序的key-value集合,非同步基于红黑树(Red-Black tree)实现,每一个key-value节点作为红黑树的一个节点。TreeMap存储时会进行排序的,会根据key来对key-value键值对进行排序,其中排序方式也是分为两种,一种是自然排序,一种是定制排序,具体取决于使用的构造方法。

自然排序:TreeMap中所有的key必须实现Comparable接口,并且所有的key都应该是同一个类的对象,否则会报ClassCastException异常。

定制排序:定义TreeMap时,创建一个comparator对象,该对象对所有的treeMap中所有的key值进行排序,采用定制排序的时候不需要TreeMap中所有的key必须实现Comparable接口。

TreeMap判断两个元素相等的标准:两个key通过compareTo()方法返回0,则认为这两个key相等。

如果使用自定义的类来作为TreeMap中的key值,且想让TreeMap能够良好的工作,则必须重写自定义类中的equals()方法,TreeMap中判断相等的标准是:两个key通过equals()方法返回为true,并且通过compareTo()方法比较应该返回为0。

以下基于JDK1.9
public interface Map<K,V>
An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.

This interface takes the place of the Dictionary class, which was a totally abstract class rather than an interface.

一个将键映射到值的对象。地图不能包含重复的键;每个键最多只能映射到一个值。

这个接口取代了Dictionary类,它是一个完全抽象的类,而不是一个接口。

The Map interface provides three collection views, which allow a map's contents to be viewed as a set of keys, collection of values, or set of key-value mappings. The orderof a map is defined as the order in which the iterators on the map's collection views return their elements. Some map implementations, like the TreeMap class, make specific guarantees as to their order; others, like the HashMap class, do not.

Map接口提供三个托收视图,允许将Map的内容视为一组键、值集合或键值映射集。映射的顺序是指映射集合视图上的迭代器返回它们的元素的顺序。一些映射实现,如TreeMap类,对它们的顺序做出具体的保证;其他的,如HashMap类,则没有。

Note: great care must be exercised if mutable objects are used as map keys. The behavior of a map is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is a key in the map. A special case of this prohibition is that it is not permissible for a map to contain itself as a key. While it is permissible for a map to contain itself as a value, extreme caution is advised: the equals and hashCode methods are no longer well defined on such a map.

注意:如果可变对象被用作map键,必须执行非常小心的操作。如果一个对象的值发生了改变,而对象是map中的键,那么就不会指定映射的行为。这条禁令的一个特例是,地图不允许将自己作为一个密钥。虽然可以允许map将自身作为一个值来包含,但是要特别注意:在这样的映射中,equals和hashCode方法不再很好地定义。

All general-purpose map implementation classes should provide two "standard" constructors: a void (no arguments) constructor which creates an empty map, and a constructor with a single argument of type Map, which creates a new map with the same key-value mappings as its argument. In effect, the latter constructor allows the user to copy any map, producing an equivalent map of the desired class. There is no way to enforce this recommendation (as interfaces cannot contain constructors) but all of the general-purpose map implementations in the JDK comply.

所有通用的Map实现类都应该提供两个“标准”构造器:一个void(无参数)构造器,它创建一个空的映射,一个构造函数带有一个类型映射的单一参数,它创建了一个新的映射,它的键值映射和它的参数一样。实际上,后者的构造函数允许用户复制任何映射,生成所需类的等效映射。没有办法强制执行这个建议(因为接口不能包含构造函数),但是JDK中的所有通用映射实现都遵从了。

The "destructive" methods contained in this interface, that is, the methods that modify the map on which they operate, are specified to throwUnsupportedOperationException if this map does not support the operation. If this is the case, these methods may, but are not required to, throw an UnsupportedOperationException if the invocation would have no effect on the map. For example, invoking the putAll(Map) method on an unmodifiable map may, but is not required to, throw the exception if the map whose mappings are to be "superimposed" is empty.

此接口中包含的“破坏性”方法,即修改地图的方法操作,指定throwUnsupportedOperationException如果这张地图不支持的操作。如果是这样的话,这些方法可能,但不需要,抛出UnsupportedOperationException如果调用方式在地图上没有影响。例如,在不可修改的映射上调用putAll(Map)方法,但不需要,如果映射为“叠加”的映射为空,则抛出异常。

Some map implementations have restrictions on the keys and values they may contain. For example, some implementations prohibit null keys and values, and some have restrictions on the types of their keys. Attempting to insert an ineligible key or value throws an unchecked exception, typically NullPointerException or ClassCastException. Attempting to query the presence of an ineligible key or value may throw an exception, or it may simply return false; some implementations will exhibit the former behavior and some will exhibit the latter. More generally, attempting an operation on an ineligible key or value whose completion would not result in the insertion of an ineligible element into the map may throw an exception or it may succeed, at the option of the implementation. Such exceptions are marked as "optional" in the specification for this interface.

有些映射实现对它们可能包含的键和值有限制。例如,有些实现禁止空键和值,有些实现对键的类型有限制。试图插入一个不合格的键或值抛出未检查的异常,通常是NullPointerException或ClassCastException。试图查询一个不合格的键或值的存在可能会抛出异常,或者它可能只是返回false;一些实现将展示前者的行为,而有些实现将展示后者。更一般的情况是,尝试在不符合条件的键或值上执行操作,如果没有将不符合条件的元素插入到map中,则可能会抛出异常,或者在实现的选项中可能成功。在该接口的规范中,这些异常被标记为“可选”。

Many methods in Collections Framework interfaces are defined in terms of the equals method. For example, the specification for the containsKey(Object key) method says: "returns true if and only if this map contains a mapping for a key k such that (key==null ? k==null : key.equals(k))." This specification should not be construed to imply that invoking Map.containsKey with a non-null argument key will cause key.equals(k) to be invoked for any key k. Implementations are free to implement optimizations whereby the equals invocation is avoided, for example, by first comparing the hash codes of the two keys. (The Object.hashCode()specification guarantees that two objects with unequal hash codes cannot be equal.) More generally, implementations of the various Collections Framework interfaces are free to take advantage of the specified behavior of underlying Object methods wherever the implementor deems it appropriate.

集合框架接口中的许多方法都是用equals方法定义的。例如,容器键(Object key)方法的规范说:“如果且仅当这个映射包含一个键k的映射时返回true(key==null吗?k = = null:key.equals(k))。”该规范不应被解释为暗示调用Map。带有非空参数键的容器键将会引起键.equals(k)被调用,因为任何关键k实现都可以自由地实现优化,从而避免相等的调用,例如,首先比较两个键的散列码。(object.hashcode()规范保证了不相等的哈希码的两个对象不能相等。)更广泛地说,各种集合框架接口的实现可以自由地利用底层对象方法的指定行为,无论实现者认为它是合适的。

Some map operations which perform recursive traversal of the map may fail with an exception for self-referential instances where the map directly or indirectly contains itself. This includes the clone()equals()hashCode() and toString() methods. Implementations may optionally handle the self-referential scenario, however most current implementations do not do so.

有些映射操作执行递归遍历映射操作,但对于直接或间接包含映射的自引用实例来说,可能会失败。 这包括克隆()、equals()、hashCode()和toString()方法。 实现可以随意地处理自我引用的场景,但是大多数当前实现没有这样做。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/274055.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

gsettings命令使用简介

1.gsettings创建项 应用程序可以使用gsettings来保存配置信息&#xff0c;可以通过代码在程序中进行设置、修改gsettings的已有的项&#xff0c;但是不能通过程序代码创建新的gsettings项&#xff0c;gsettings的项的在一个叫做schema的规范文件中创建&#xff0c;schema文档其…

Collection 和 Collections区别

Collection 和 Collections区别&#xff08;1&#xff09;java.util.Collection 是一个集合接口&#xff08;集合类的一个顶级接口&#xff09;。它提供了对集合对象进行基本操作的通用接口方法。Collection接口在Java 类库中有很多具体的实现。Collection接口的意义是为各种具…

Http状态码完整说明

在网站建设的实际应用中&#xff0c;容易出现很多小小的失误&#xff0c;就像mysql当初优化不到位&#xff0c;影响整体网站的浏览效果一样&#xff0c;其实&#xff0c;网站的常规http状态码的表现也是一样&#xff0c; 一些常见的状态码为&#xff1a; 200 - 服务器成功返回网…

运用xlib进行事件响应(X11 API)的小例子

转自&#xff1a;http://blog.csdn.net/linuxheik/article/details/7659090 File: x11_test.cxx #include <X11/Xlib.h> 每一个Xlib 程序都必须包含这个头文件 #include <stdio.h>1. int main(void) {2. Display *display XopenDisplay(NULL);首先打开与server …

Java 之HashSet、LinkedHashSet、TreeSet比较

4.HashSet、LinkedHashSet、TreeSet比较 Set接口Set不允许包含相同的元素&#xff0c;如果试图把两个相同元素加入同一个集合中&#xff0c;add方法返回false。Set判断两个对象相同不是使用运算符&#xff0c;而是根据equals方法。也就是说&#xff0c;只要两个对象用equals方法…

jquery1.9学习笔记 之选择器(基本元素四)

ID选择器("#id") 描述&#xff1a; 选择与给出ID属性匹配的单元标签。 对于ID选择器&#xff0c;jquery使用JS的函数document.getElementById()&#xff0c;当一个标签附加到ID选择器上时&#xff0c;也是非常有效的。如h2#pageTitle&#xff0c;jquery会在识别元素标…

Java(ArrayList和LinkedList)、(HashTable与HashMap)、(HashMap、Hashtable、LinkedHashMap和TreeMap比较)

1.ArrayList和LinkedList &#xff08;1&#xff09;ArrayList是实现了基于动态数组的数据结构&#xff0c;LinkedList基于链表的数据结构。 &#xff08;2&#xff09;对于随机访问get和set&#xff0c;ArrayList绝对优于LinkedList&#xff0c;因为LinkedList要移动指针。 &a…

oracle 事务测试

此文章是根据官方改变 模拟帐户转账流程1.JOHN帐户扣除-DAVID帐户增加-记录日志&#xff0d;事务提交三个操作必须全部完成此事务才完成&#xff0c;否则失败创建帐户余额表自增字段自增序列&#xff1b;createsequencesaving_seqincrementby1startwith1maxvalue99999999999999…

apt-get 获取源码的方法

apt-get source gconf-editor –allow-unauthenticated 注&#xff1a;gconf-editor是一个包名&#xff0c;根据自己的需求相应更改即可

Java 集合之自动打包和解包以及泛型

自动打包与解包&#xff1a;泛型&#xff1a;上栗子&#xff1a; TestMap1.java: package com.zhj.www; import java.util.*;public class TestMap {public static void main(String[] args) {Map m1 new HashMap();Map m2 new TreeMap();//m1.put("one", new Inte…

select * from dim.dim_area_no@to_dw

应该是建的有database linksdim是用户名&#xff0c;dim_area_no是表名&#xff0c;to_dw 是建的database links的名&#xff0c;dim_area_no表属于dim用户创建database links的作用是连接其他数据库的表select * from dim.dim_area_noto_dw 这个语句的作用是查询属于dim用户的…

ios 内存管理 心得

- alloc, copy, retain会把引用计数1 - release会把引用计数-1 - 局部变量如果初始化时不是autorelease的&#xff0c;要及时调用release释放&#xff0c;并且赋值为nil否则引用仍然存在导致下次无法用nil做是否有值的判断 - 实例变量要在每次赋值时要先释放当前引用的对象再赋…

error while loading shared libraries: xxx.so.x 错误的原因和解决办法

一般我们在Linux下执行某些外部程序的时候可能会提示找不到共享库的错误, 比如: tmux: error while loading shared libraries: libevent-1.4.so.2: cannot open shared object file: No such file or directory 原因一般有两个, 一个是操作系统里确实没有包含该共享库(lib*.…

泗洪高薪行业

泗洪高薪行业转载于:https://www.cnblogs.com/soundcode/p/3302297.html

libghttp 编译及封装使用实例

想用C语言写个采集程序&#xff0c;涉及到http相关的东西&#xff0c;找了找&#xff0c;有现成的libghttp库。 libghttp库的官方网址google一下第一条结果一般就是的&#xff1a;http://lfs.linuxsir.org/htdocs/blfscvs/gnome/libghttp.html 将源码包下载下来&#xff0c;进…

Java IO 节点流与处理流类型

处理流类型&#xff1a;1、处理流之首先缓冲流&#xff1a;解释&#xff1a;例子&#xff1a;TestBufferStream1.java package com.zhj.www;import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException;public class TestBufferStream1 …

高级浏览器-SRWare Iron 29.0.1600.0 版本发布

SRWare Iron是德国一安全公司srware改造的Chrome&#xff08;铬&#xff09;命名为铁&#xff08;iron&#xff09;的浏览器。于2008年9月18日首次发布。 据官方介绍&#xff0c;Iron浏览器砍掉了Chromium原程序中的很多有碍“隐私”问题的代码。 “iron中去除的功能包括&#…

shell中的${},##和%%的使用

假设我们定义了一个变量为&#xff1a; file/dir1/dir2/dir3/my.file.txt 可以用${ }分别替换得到不同的值&#xff1a; ${file#*/}&#xff1a;删掉第一个 / 及其左边的字符串&#xff1a;dir1/dir2/dir3/my.file.txt ${file##*/}&#xff1a;删掉最后一个 / 及其左边的字…

Java 线程多线程编程1---基础

1、线程的基本概念例子&#xff1a;分析&#xff1a;2、线程的创建和启动第一种线程的创建&#xff1a;定义一个线程类来实现Runner接口 例子&#xff1a; package com.zhj.www; import java.lang.Thread; public class TestThread1 {public static void main(String[] args) {…

移动互联网下一步:“深度学习”配合大数据

随着电子商务不断深入&#xff0c;百度、腾讯、阿里巴巴的移动互联网战略的可比性越来月低&#xff0c;如今百度的移动互联网的战略也面临挑战&#xff0c;最大的因素在于数据的来源。 对于互联网的公司最近的动态是什么&#xff1f;这个不是很难的&#xff0c;主要看一下公司的…