package
collection;
import
java.util.Collection;
import
java.util.HashMap;
import
java.util.Hashtable;
import
java.util.Iterator;
import
java.util.Map;
import
java.util.Set;
public
class
MapTest {
public
static
void
main(String[] args) {
Map<String,Integer> m =
new
HashMap<>();
m.put(
"hello"
,
11
);
m.put(
"world"
,
11
);
m.put(
"nihao"
,
21
);
m.put(
"shijie"
,
22
);
//输出map中的所有value
Collection<Integer> values = m.values();
for
(
int
value: values) {
System.out.println(value);
}
//map遍历1,通过keySet获得map中所有可以的集合,在通过key得到value
//这种方式会遍历两次,一次是对key值得iterator,再一次是通过key获得值
System.out.println(
"--------map遍历1------------"
);
Set keySet = m.keySet();
Iterator<String> it = keySet.iterator();
while
(it.hasNext()) {
String key = it.next();
System.out.print(
"[key="
+ key +
",value="
+ m.get(key) +
"] "
);
}
//map遍历2,foreach方法来遍历keyset,和第一种没有什么区别
System.out.println(
"--------map遍历2------------"
);
for
(Object key: keySet) {
System.out.print(
"[key="
+ key +
",value="
+ m.get(key) +
"] "
);
}
//map遍历3,Map内部的Entry封装所有map的key-value对放入集合中,集 //合中的一个元素就包括了key和value,这样就之遍历一次就能得到集合,并可以访问所
//有key-value。
System.out.println(
"--------map遍历3------------"
);
Set entrySet = m.entrySet();
Iterator it1 = entrySet.iterator();
while
(it1.hasNext()) {
@SuppressWarnings
(
"unchecked"
)
Map.Entry<String, Integer> e = (Map.Entry<String, Integer>)it1.next();
System.out.print(
"[key="
+ e.getKey() +
",value="
+ e.getValue() +
"] "
);
}
System.out.println();
System.out.println(
"--------map遍历4------------"
);
//map遍历4,java8中新增方法。
m.forEach((key,value) -> {
System.out.print(
"[key="
+ key +
",value="
+ value +
"] "
);
});
//关于hashMap的遍历,推荐使用3,4两种方法。
}
}
二.遍历HashSet
Set set = new HashSet();
for(Iterator it=set.iterator();it.hasNext();){
System.out.println(it.next());
}
三.遍历Hshtable
要遍历一个Hashtable,api中提供了如下几个方法可供我们遍历:
keys() - returns an Enumeration of the keys of this Hashtable keySet() - returns a Set of the keys entrySet() - returns a Set of the mappings elements() - returns an Enumeration of the values of this Hashtable |
4种方法,那种更好呢,写段代码来比较一下吧:
import
java.util.Enumeration;
import
java.util.Hashtable;
import
java.util.Iterator;
import
java.util.Map.Entry;
public
class
HashtableTest {
public
static
void
main(String[] args) {
long
start = 0;
long
end = 0;
Hashtable<String, String> table =
new
Hashtable<String, String>();
for
(
int
i = 0; i < 1000000; i++) {
table.put(
"key:"
+ i,
"value:"
+ i);
}
//1、使用keys()
start = System. currentTimeMillis();
Enumeration<String> en1 = table.keys();
while
(en1.hasMoreElements()) {
en1.nextElement();
}
end = System. currentTimeMillis();
System.
out
.println(
"Enumeration keys costs "
+ (end - start) +
" milliseconds"
);
//2、使用elements()
start = System. currentTimeMillis();
Enumeration<String> en2 = table.elements();
while
(en2.hasMoreElements()) {
en2.nextElement();
}
end = System. currentTimeMillis();
System.
out
.println(
"Enumeration elements costs "
+ (end - start) +
" milliseconds"
);
//3、使用keySet()
start = System. currentTimeMillis();
Iterator<String> it1 = table.keySet().iterator();
while
(it1.hasNext()) {
it1.next();
}
end = System. currentTimeMillis();
System.
out
.println(
"Iterator keySet costs "
+ (end - start) +
" milliseconds"
);
//4、使用entrySet()
start = System. currentTimeMillis();
Iterator<Entry<String, String>> it2 = table.entrySet().iterator();
while
(it2.hasNext()) {
it2.next();
}
end = System. currentTimeMillis();
System.
out
.println(
"Iterator entrySet costs "
+ (end - start) +
" milliseconds"
);
}
}
|
运行结果如下:
Enumeration keys costs 16 milliseconds
Enumeration elements costs 15 milliseconds
Iterator keySet costs 17 milliseconds
Iterator entrySet costs 16 milliseconds
|
通过运行结果显示,在如今jdk的版本下,两者差距不是很大,不过使用Enumeration去遍历会稍微快一些。