Java Map中的几个常见问题

 

列举几个关于Java Map的常见问题并给出答案。java

1. 将Map转化成List

Map接口提供了三种collection:key set,value set 和 key-value set,每一种均可以转成List。以下:面试

//mapHashMap<Integer,Integer> map = new HashMap<>();map.put(1,10);map.put(2,20);map.put(3,30);//key listArrayList<Integer> keyList = new ArrayList<>(map.keySet());//value listArrayList<Integer> valueList = new ArrayList<>(map.values());//key-value listArrayList<Map.Entry<Integer,Integer>> entryList = new ArrayList<>(map.entrySet());

2. 迭代Map

最高效的遍历map的每一个entry的方法以下:数组

for (Map.Entry entry : map.entrySet()){
    int key = (int) entry.getKey();
    int value = (int) entry.getValue();}

也可使用iterator,特别是JDK 1.5以前。安全

Iterator itr = map.entrySet().iterator();while(itr.hasNext()){
  Map.Entry entry = itr.next();
  int key = (int) entry.getKey();
  int value = (int) entry.getValue();}

3. 根据key对map进行排序

能够将Map.Entry放入一个list,而后本身实现Comparator来对list排序。架构

ArrayList<Map.Entry<Integer,Integer>> list = new ArrayList<>(map.entrySet());Collections.sort(list, new Comparator<Map.Entry<Integer, Integer>>() {
    @Override
    public int compare(Map.Entry<Integer, Integer> e1, Map.Entry<Integer, Integer> e2) {
        return e1.getKey().compareTo(e2.getKey());
    }});

可使用SortedMap。SortedMap的一个实现类是TreeMap。TreeMap的构造器能够接受一个Comparator参数。以下:框架

SortedMap<Integer,Integer> sortedMap = new TreeMap<>(new Comparator<Integer>() {
    @Override
    public int compare(Integer k1, Integer k2) {
        return k1.compareTo(k2);
    }});sortedMap.putAll(map);

注:TreeMap默认对key进行排序。分布式

4. 根据value对map进行排序

ArrayList<Map.Entry<Integer,Integer>> list = new ArrayList<>(map.entrySet());Collections.sort(list, new Comparator<Map.Entry<Integer, Integer>>() {
    @Override
    public int compare(Map.Entry<Integer, Integer> e1, Map.Entry<Integer, Integer> e2) {
        return e1.getValue().compareTo(e2.getValue());
    }});

若是map中的value不重复,能够经过反转key-value对为value-key对来用上面的3中的TreeMap方法对其排序。该方法不推荐。ide

5. 初始化一个不可变Map

正确的作法:函数

public class Test{
  private static Map<Integer,Integer> map1 = new HashMap<>();
  static {
    map1.put(8,9);
    map1.put(88,99);
    map1 = Collections.unmodifiableMap(map1);
 }}

错误的作法:源码分析

public class Test{
  private static final Map<Integer,Integer> map1 = new HashMap<>();
  static {
    map1.put(8,9);
    map1.put(88,99);
 }}

加了final只能确保不能 map1 = new,可是能够修改map1中的元素。

6. HashMap、TreeMap和HashTable的区别

Map接口有三个比较重要的实现类,分别是HashMap、TreeMap和HashTable。

  1. TreeMap是有序的,HashMap和HashTable是无序的。

  2. Hashtable的方法是同步的,HashMap的方法不是同步的。这是二者最主要的区别。

    这就意味着Hashtable是线程安全的,HashMap不是线程安全的。HashMap效率较高,Hashtable效率较低。 若是对同步性或与遗留代码的兼容性没有任何要求,建议使用HashMap。 查看Hashtable的源代码就能够发现,除构造函数外,Hashtable的全部 public 方法声明中都有 synchronized关键字,而HashMap的源码中则没有。

  3. Hashtable不容许null值,HashMap容许null值(key和value都容许)

  4. 父类不一样:Hashtable的父类是Dictionary,HashMap的父类是AbstractMap

  5. Hashtable中hash数组默认大小是11,增长的方式是 old*2+1。HashMap中hash数组的默认大小是16,并且必定是2的指数。

                | HashMap | Hashtable | TreeMap
-------------------------------------------------------
iteration order  | no      | no        | yes
null key-value   | yes-yes | no-no   | no-yes
synchronized     | no      | yes       | no
time performance | O(1)    | O(1)      | O(log n)
implementation   | buckets | buckets   | red-black tree

7. 建立一个空的Map

若是但愿该map为不可变的,则:

map = Collections.emptyMap();

不然:

map = new HashMap();

 

若是你想学习Java工程化、高性能及分布式、高性能、深刻浅出。性能调优、Spring,MyBatis,Netty源码分析和大数据等知识点能够来找我。 而如今我就有一个平台能够提供给大家学习,让你在实践中积累经验掌握原理。主要方向是JAVA架构师。若是你想拿高薪,想突破瓶颈,想跟别人竞争能取得优点的,想进BAT可是有担忧面试不过的,能够加个人Java架构进阶群:554355695 注:加群要求 一、具备2-5工做经验的,面对目前流行的技术不知从何下手,须要突破技术瓶颈的能够加。  二、在公司待久了,过得很安逸,但跳槽时面试碰壁。须要在短期内进修、跳槽拿高薪的能够加。  三、若是没有工做经验,但基础很是扎实,对java工做机制,经常使用设计思想,经常使用java开发框架掌握熟练的,能够加。  四、以为本身很牛B,通常需求都能搞定。可是所学的知识点没有系统化,很难在技术领域继续突破的能够加。  5.阿里Java高级大牛直播讲解知识点,分享知识,多年工做经验的梳理和总结,带着你们全面、科学地创建本身的技术体系和技术认知!  6.小号加群一概不给过,谢谢。

相关文章
相关标签/搜索