Java数据结构之Map学习总结

前言:

    前面学习总结了List的使用及效率对比,今天总结学习一下键值映射关系Map,顺便学习一下Android中使用Map需要注意哪些,以及谷歌官方针对Android对Map做了哪些优化。

先了解下Map

   Map 是一种把键对象和值对象映射的集合,它的每一个元素都包含一对键对象和值对象。 Map没有继承于Collection接口 从Map集合中检索元素时,只要给出键对象,就会返回对应的值对象。 

Map是一个接口,实例化Map可以采用下面的方式:

  • HashMap //Map基于散列表的实现。插入和查询“键值对”的开销是固定的。可以通过构造器设置容量capacity和负载因子load factor,以调整容器的性能。 
  • LinkedHashMap  //类似于HashMap,但是迭代遍历它时,取得“键值对”的顺序是其插入次序,或者是最近最少使用(LRU)的次序。只比HashMap慢一点。而在迭代访问时发而更快,因为它使用链表维护内部次序。 
  • TreeMap //底层是二叉树数据结构,线程不同步,可用于给Map集合中的键进行排序。
  • HashTable //HashMap是Hashtable的轻量级实现,非线程安全的实现他们都实现了map接口,主要区别是HashMap键值可以为空null,效率可以高于Hashtable。
  • ConcurrentHashMap //ConcurrentHashMap通常只被看做并发效率更高的Map,用来替换其他线程安全的Map容器,比如Hashtable和Collections.synchronizedMap。
  • WeakHashMap //弱键(weak key)Map,Map中使用的对象也被允许释放: 这是为解决特殊问题设计的。如果没有map之外的引用指向某个“键”,则此“键”可以被垃圾收集器回收。
  • IdentifyHashMap //使用==代替equals()对“键”作比较的hash map
  • ArrayMap //ArrayMap是一个<key,value>映射的数据结构,它设计上更多的是考虑内存的优化,内部是使用两个数组进行数据存储,一个数组记录key的hash值,另外一个数组记录Value值,它和SparseArray一样,也会对key使用二分法进行从小到大排序,在添加、删除、查找数据的时候都是先使用二分查找法得到相应的index,然后通过index来进行添加、查找、删除等操作,所以,应用场景和SparseArray的一样,如果在数据量比较大的情况下,那么它的性能将退化至少50%。
  • SparseArray //SparseArray比HashMap更省内存,在某些条件下性能更好,主要是因为它避免了对key的自动装箱(int转为Integer类型),它内部则是通过两个数组来进行数据存储的,一个存储key,另外一个存储value,为了优化性能,它内部对数据还采取了压缩的方式来表示稀疏数组的数据,从而节约内存空间。

Map的基本操作:

  • Object put(Object key, Object value): 向集合中加入元素   
  • Object remove(Object key): 删除与KEY相关的元素   
  • void putAll(Map t):  将来自特定映像的所有元素添加给该映像   
  • void clear():从映像中删除所有映射   

Map使用

  这里以最常用的HashMap为例

 添加数据

   Map<Integer, String> hashMap = new HashMap<>();
   for (int i = 0; i < maxCount; i++) {
      hashMap.put(i, String.valueOf(i));
   }

遍历entrySet方式

 long start = System.currentTimeMillis();
 for (Map.Entry<Integer, String> entry : hashMap.entrySet()) {
        Integer key=entry.getKey();
        String  value=entry.getValue();
        Log.i(TAG, "key: " + key +"value: "+value);
     }

    long end = System.currentTimeMillis();
    Log.e(TAG, "for-each方式 cost time : " + (end - start));

entrySet迭代器遍历方式

long start1 = System.currentTimeMillis();
Iterator<Map.Entry<Integer, String>> entries = hashMap.entrySet().iterator();

while (entries.hasNext()) {
    Map.Entry<Integer, String> entry = entries.next();
    Integer key=entry.getKey();
    String  value=entry.getValue();
    Log.i(TAG, "key: " + key +"value: "+value);
   }

long end1 = System.currentTimeMillis();
Log.e(TAG, "entrySet iterator迭代器 cost time : " + (end1 - start1));

键找值遍历

 long end1 = System.currentTimeMillis();
 Log.e(TAG, "iterator迭代器 cost time : " + (end1 - start1));

 long start2 = System.currentTimeMillis();
 for (Integer key : hashMap.keySet()) {
      String value = hashMap.get(key);
      Log.i(TAG, "key: " + key +"value: "+value);
 }

  long end2 = System.currentTimeMillis();
  Log.e(TAG, "键找值遍历 cost time : " + (end2 - start2));

keySet迭代器遍历

long start3 = System.currentTimeMillis();

Iterator<Integer> iterator=hashMap.keySet().iterator();
        while (iterator.hasNext()) {
        Integer key=iterator.next();
        String  value=hashMap.get(key);
        Log.i(TAG, "key: " + key +"value: "+value);
    }

 long end3 = System.currentTimeMillis();
 Log.e(TAG, "keySet iterator迭代器 cost time : " + (end3 - start3));

 上述四种情况执行结果如下:

总结:

  主要重新熟悉一下Map这种数据结构,以及更好的在以后的编程中选择更合适的方式来进行key-value存储。

 

转载于:https://www.cnblogs.com/whoislcj/p/6516789.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
This book explores the concept of a map as a fundamental data type. It defines maps at three levels. The first is an abstract level, in which mathematic concepts are leveraged to precisely explain maps and operational semantics. The second is at a discrete level, in which graph theory is used to create a data model with the goal of implementation in computer systems. Finally, maps are examined at an implementation level, in which the authors discuss the implementation of a fundamental map data type in database systems. The map data type presented in this book creates new mechanisms for the storage, analysis, and computation of map data objects in any field that represents data in a map form. The authors develop a model that includes a map data type capable of representing thematic and geometric attributes in a single data object. The book provides a complete example of mathematically defining a data type, ensuring closure properties of those operations, and then translating that type into a state that is suited for implementation in a particular context. The book is designed for researchers and professionals working in geography or computer science in a range of fields including navigation, reasoning, robotics, geospatial analysis, data management, and information retrieval. Table of Contents Chapter 1 Concepts of Maps Chapter 2 A Formal Model of Maps as a Fundamental Type Chapter 3 PLR Partitions: Extending Maps to Include Point and Line Features Chapter 4 Foundational Operations for Maps Chapter 5 Constructing Map Operations Using the Fundamental Map Operations Chapter 6 Extended Operations Over Maps Chapter 7 Topological Relationships Between Maps Chapter 8 A Discrete Model of Maps Chapter 9 Implementing Maps: Map2D

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值