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方法对其排序。该方法不推荐。
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。
-
TreeMap是有序的,HashMap和HashTable是无序的。
-
Hashtable的方法是同步的,HashMap的方法不是同步的。这是两者最主要的区别。
这就意味着Hashtable是线程安全的,HashMap不是线程安全的。HashMap效率较高,Hashtable效率较低。 如果对同步性或与遗留代码的兼容性没有任何要求,建议使用HashMap。 查看Hashtable的源代码就可以发现,除构造函数外,Hashtable的所有 public 方法声明中都有 synchronized关键字,而HashMap的源码中则没有。
-
Hashtable不允许null值,HashMap允许null值(key和value都允许)
-
父类不同:Hashtable的父类是Dictionary,HashMap的父类是AbstractMap
-
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架构师。如果你想拿高薪,想突破瓶颈,想跟别人竞争能取得优势的,想进BAT但是有担心面试不过的,可以加我的Java架构进阶群:554355695
注:加群要求
1、具有2-5工作经验的,面对目前流行的技术不知从何下手,需要突破技术瓶颈的可以加。
2、在公司待久了,过得很安逸,但跳槽时面试碰壁。需要在短时间内进修、跳槽拿高薪的可以加。
3、如果没有工作经验,但基础非常扎实,对java工作机制,常用设计思想,常用java开发框架掌握熟练的,可以加。
4、觉得自己很牛B,一般需求都能搞定。但是所学的知识点没有系统化,很难在技术领域继续突破的可以加。
5.阿里Java高级大牛直播讲解知识点,分享知识,多年工作经验的梳理和总结,带着大家全面、科学地建立自己的技术体系和技术认知!
6.小号加群一律不给过,谢谢。