前言: **Java 自带了各种 Map 类。这些 Map 类可归为三种类型:
通用Map,用于在应用程序中管理映射,通常在 java.util 程序包中实现 HashMap、Hashtable、Properties、LinkedHashMap、IdentityHashMap、TreeMap、WeakHashMap、ConcurrentHashMap专用Map,通常我们不必亲自创建此类Map,而是通过某些其他类对其进行访问 java.util.jar.Attributes、javax.print.attribute.standard.PrinterStateReasons、java.security.Provider、java.awt.RenderingHints、javax.swing.UIDefaults一个用于帮助我们实现自己的Map类的抽象类 AbstractMap**
HashMap的遍历
public static void main(String
[] args
) {
HashMap
<String
,Integer
> map
= new HashMap
<>();
map
.put("zdj",1);
map
.put("zdk",2);
Iterator
<String
> it
= map
.keySet().iterator();
while(it
.hasNext()){
String key
= it
.next();
System
.out
.println(key
+ map
.get(key
));
}
System
.out
.println("---------------------");
Iterator
<Map
.Entry
<String
,Integer
>>it2
= map
.entrySet().iterator();
while(it2
.hasNext()){
Map
.Entry
<String
,Integer
>entry
= it2
.next();
System
.out
.println(entry
.getKey()+" "+entry
.getValue());
}
System
.out
.println("---------------------");
for(Map
.Entry
<String
,Integer
> entry
: map
.entrySet()){
System
.out
.println(entry
.getKey()+" "+entry
.getValue());
}
System
.out
.println("---------------------");
for(String key
: map
.keySet())
System
.out
.println(key
);
}
总结: 主要分为for each 循环遍历 和迭代器遍历这两种遍历里面有keySet 迭代和entrySet迭代两种 迭代器遍历效率比较高,entrySet遍历效率高,迭代器的遍历速度要比增强for循环快很多,是增强for循环的2倍左右。使用entrySet遍历的速度要比keySet快很多,是keySet的1.5倍左右。
排序
HashMap是无法排序,只能用自己的map.entryset传给list用ArrayList实现排序, TreeMap是可以通过重写比较器方法,自身按key值排序,不能按value排序,TreeMap底层是根据红黑树的数据结构构建的,默认是根据key的自然排序来组织(比如integer的大小,String的字典排序)。所以,TreeMap只能根据key来排序,是不能根据value来排序的(否则key来排序根本就不能形成TreeMap)。 如果非要按值排序就得利用ArrayList接一下,重新排序,
HashMap 按key排序:
Map
<String
,Integer
>map
= new HashMap
<String
,Integer
>();
map
.put("b",1);
map
.put("a",4);
map
.put("d",3);
map
.put("c",2);
List
<Map
.Entry
<String
,Integer
>>list
= new ArrayList
<Map
.Entry
<String
, Integer
>>(map
.entrySet());
Collections
.sort(list
, new Comparator
<Map
.Entry
<String
, Integer
>>() {
@Override
public int compare(Map
.Entry
<String
, Integer
> o1
, Map
.Entry
<String
, Integer
> o2
) {
return o1
.getKey().compareTo(o2
.getKey());
}
});
for(Map
.Entry
<String
,Integer
> entry
:list
) System
.out
.println(entry
.getKey()+" "+entry
.getValue());
a
4
b
1
c
2
d
3
Process finished with exit code
0
HashMap 按value排序:
Map
<String
,Integer
>map
= new HashMap
<String
,Integer
>();
map
.put("a",1);
map
.put("b",4);
map
.put("c",3);
map
.put("d",2);
List
<Map
.Entry
<String
,Integer
>>list
= new ArrayList
<Map
.Entry
<String
, Integer
>>(map
.entrySet());
Collections
.sort(list
, new Comparator
<Map
.Entry
<String
, Integer
>>() {
@Override
public int compare(Map
.Entry
<String
, Integer
> o1
, Map
.Entry
<String
, Integer
> o2
) {
return o1
.getKey().compareTo(o2
.getKey());
}
});
for(Map
.Entry
<String
,Integer
> entry
:list
) System
.out
.println(entry
.getKey()+" "+entry
.getValue());
b
1
c
2
d
3
a
4
Process finished with exit code
0
TreeMap 按key排序*(自身排序)
Map
<String
,Integer
>map
= new TreeMap
<String
,Integer
>(new Comparator
<String
>() {
@Override
public int compare(String o1
, String o2
) {
return o1
.compareTo(o2
);
}
});
map
.put("b",1);
map
.put("a",4);
map
.put("d",3);
map
.put("c",2);
for(Map
.Entry
<String
,Integer
> entry
:map
.entrySet()) System
.out
.println(entry
.getKey()+" "+entry
.getValue());
a
4
b
1
c
2
d
3
Process finished with exit code
0
TreeMap的按值排序还是跟HashMap的一样靠ArrayList实现的就不写了;
HashMap的实现原理
HashMap是一个由数组和单链表组成的散列桶,