选择一个map进行软件基础数据初始化操作,要求第一次初始化后,不修改数据,可能会出现静态类被回收,然后在进行初始化操作?
1.Map :接口
/*** A {@codeMap} is a data structure consisting of a set of keys and values
* in which each key is mapped to a single value. The class of the objects
* used as keys is declared when the {@codeMap} is declared, as is the
* class of the corresponding values.
*
* A {@codeMap} provides helper methods to iterate through all of the
* keys contained in it, as well as various methods to access and update
* the key/value pairs.*/
一个map是由一组键和值组成的数据结构 ,其中每个键映射到一个单一的值。当Map被声明后,values的值将被作为key 的关联值被声明
Map提供帮助方法来遍历所有它的包含键值,以及各种方法来访问和更新键/值对。
ps:Map提供key、values形式的声明,public interface Map{},map是一个接口,不能直接使用。
2.hashMap
/*** HashMap is an implementation of {@linkMap}. All optional operations are supported.
*
*
All elements are permitted as keys or values, including null.
*
*
Note that the iteration order for HashMap is non-deterministic. If you want
* deterministic iteration, use {@linkLinkedHashMap}.
*
*
Note: the implementation of {@codeHashMap} is not synchronized.
* If one thread of several threads accessing an instance modifies the map
* structurally, access to the map needs to be synchronized. A structural
* modification is an operation that adds or removes an entry. Changes in
* the value of an entry are not structural changes.
*
*
The {@codeIterator} created by calling the {@codeiterator} method
* may throw a {@codeConcurrentModificationException} if the map is structurally
* changed while an iterator is used to iterate over the elements. Only the
* {@coderemove} method that is provided by the iterator allows for removal of
* elements during iteration. It is not possible to guarantee that this
* mechanism works in all cases of unsynchronized concurrent modification. It
* should only be used for debugging purposes.
*
*@param the type of keys maintained by this map
*@param the type of mapped values*/
HashMap继承了map,实现了map的所有方法。key和value允许使用全部的元素,包括null
注意遍历hashMap是随机的,如果你想定义遍历顺序,请使用LinkedHashMap。
注意:HashMap实现是不同步的。如果多个线程的一个线程访问一个实例map,修改了该map结构,使用的map需要同步。
结构上的修改是指添加或删除一个节点的操作。改变一个map的值不是结构上的变化。
通过Iterator创建并遍历hashMap时,如果使用iterator遍历map的元素,map的结构被修改时,此时可能抛ConcurrentMod