【九月打卡】第3天 HashMap的使用
课程名称:Map从入门到性能分析
课程章节:第2章 HashMap的使用
主讲老师:大谷
课程内容:
- HashMap基本用法
- HashMap的Entry结构
- HashMap遍历-keySet
- HashMap遍历-values
- HashMap遍历-entrySet
- HashMap遍历-Iterator
- HashMap遍历-性能分析
课程收获:
通过学习本章节掌握了HashMap的基本用法、Entry结构和几种遍历方法,通过性能分析掌握了每种遍历方法的优劣。
1.Map接口通用方法
- V put(K key, V value): Map中存入一个key-value映射
- V get(Object key): 返回到指定键所映射的值
- int size(): 返回Map中键值映射的数量
- V remove(Object key): 从该Map中删除一个键的映射
- boolean containsKey(Object key): 是否包含指定键的key
2.HashMap基本用法
2.1HashMap的构造方法
- HashMap()
- HashMap(int initialCapacity)
- HashMap(int initialCapacity, float loadFactor)
2.2创建HashMap对象
Map<String,Object> userMap = new HashMap<>();
userMap.put("zhangsan", new Integer(120));
userMap.get("zhangsan");
3.HashMap的Entry结构
static class Entry<K,V> implements Map.Entry<K,V>{
final K key;
V value;
Entry<K,V> next;
final int hash;
}
Map<String,Object> userMap = new HashMap<>();
userMap.put("zhang1", new Integer(2));
userMap.put("zhang2", new Integer(4));
userMap.put("zhang3", new Integer(3));
userMap.put("zhang4", new Integer(1));
userMap.put("zhang5", new Integer(5));
System.out.println(userMap);
// 输出顺序并没有按照put顺序输出
// {zhang2=4, zhang3=3, zhang1=2, zhang4=1, zhang5=5}
4.利用map.keySet()遍历HashMap
for(String key: map.keySet()){
System.out.println(key + "***" + map.get(key));
}
public static void main(String[] args) {
Map map1 = inputMap();
System.out.println(map1);
// {zhang2=4, zhang3=3, zhang1=2, zhang4=1, zhang5=5}
showMap1(map1);
// zhang2***4
// zhang3***3
// zhang1***2
// zhang4***1
// zhang5***5
}
public static Map inputMap(){
Map<String,Object> userMap = new HashMap<>();
userMap.put("zhang1", new Integer(2));
userMap.put("zhang2", new Integer(4));
userMap.put("zhang3", new Integer(3));
userMap.put("zhang4", new Integer(1));
userMap.put("zhang5", new Integer(5));
return userMap;
}
public static void showMap1(Map<String,Object> map){
for (String key: map.keySet()){
System.out.println(key+"***"+map.get(key));
}
}
5.利用entrySet遍历HashMap
for (Map.Entry<String, Object> entry : map.entrySet()) {
System.out.println(entry.getKey() + "***" + entry.getValue());
}
6.利用Iterator遍历HashMap
Iterator<Map.Entry<String, Object>> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, Object> nextEntry = it.next();
System.out.println(nextEntry.getKey() + "***" + nextEntry.getValue());
}
7.HashMap遍历-性能分析
public static void main(String[] args) {
Map map1 = testSpeedMap();
showMap1(map1);
showMap2(map1);
showMap3(map1);
showMap4(map1);
}
/**
* 利用map.keySet()遍历
*
* @param map
*/
public static void showMap1(Map<String, Object> map) {
Long start = System.currentTimeMillis();
for (String key : map.keySet()) {
// System.out.println(key + "***" + map.get(key));
}
Long end = System.currentTimeMillis();
System.out.println("map.keySet()=" + (end - start));
}
/**
* 利用map.values()遍历
*
* @param map
*/
public static void showMap2(Map<String, Object> map) {
Long start = System.currentTimeMillis();
for (Object val : map.values()) {
// System.out.println(val + "***");
}
Long end = System.currentTimeMillis();
System.out.println("map.values()=" + (end - start));
}
/**
* 利用map.entrySet()遍历
*
* @param map
*/
public static void showMap3(Map<String, Object> map) {
Long start = System.currentTimeMillis();
for (Map.Entry<String, Object> entry : map.entrySet()) {
// System.out.println(entry.getKey() + "***" + entry.getValue());
}
Long end = System.currentTimeMillis();
System.out.println("map.entrySet()=" + (end - start));
}
/**
* 利用Iterator遍历
*
* @param map
*/
public static void showMap4(Map<String, Object> map) {
Long start = System.currentTimeMillis();
Iterator<Map.Entry<String, Object>> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, Object> nextEntry = it.next();
// System.out.println(nextEntry.getKey() + "***" + nextEntry.getValue());
}
Long end = System.currentTimeMillis();
System.out.println("Iterator=" + (end - start));
}
public static Map testSpeedMap() {
Map<String, Object> userMap = new HashMap<>();
String[] str = new String[]{"a", "s", "d", "f", "g", "h", "j", "k", "l", "q"};
String key;
String value;
// 10W条
for (int i = 0; i <= 100000; i++) {
int m = (int) (Math.random() * str.length);
key = str[m] + i;
value = "y" + i;
userMap.put(key, value);
}
return userMap;
}
10W条数据:
map.keySet()=20
map.values()=10
map.entrySet()=9
Iterator=10
50W条数据:
map.keySet()=27
map.values()=22
map.entrySet()=22
Iterator=23
100W条数据:
map.keySet()=40
map.values()=34
map.entrySet()=36
Iterator=38
500W条数据:
map.keySet()=172
map.values()=153
map.entrySet()=166
Iterator=160
1000W条数据:
map.keySet()=319
map.values()=294
map.entrySet()=309
Iterator=306
结论:
效率最低: map.keySet()
循环取值用: map.values()
循环遍历用: map.entrySet()
或Iterator
点击查看更多内容
1人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦