Map的四种遍历方式
package com.junfeng;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* Created with IDEA
* author:TangJunfeng
* description:Map遍历方式
* Date:4/21/21 06:55
*/
public class TestMap {
public static void main(String[] args) {
Map<String, Integer> map = buildMap();
showMapByKeySet(map);
showMapByValue(map);
showMapByEntrySet(map);
showMapByIterator(map);
}
private static void showMapByIterator(Map<String, Integer> map) {
Long start = System.currentTimeMillis();
Iterator<Map.Entry<String, Integer>> it = map.entrySet().iterator();
Integer value;
String key;
while (it.hasNext()) {
Map.Entry<String, Integer> entry = it.next();
value = entry.getValue();
key = entry.getKey();
//System.out.println(entry.getKey() + "------" + entry.getValue());
}
Long end = System.currentTimeMillis();
System.out.println("showMapByIterator=" + (end - start));
}
private static void showMapByEntrySet(Map<String, Integer> map) {
Long start = System.currentTimeMillis();
Integer value;
String key;
for (Map.Entry<String, Integer> entry : map.entrySet()) {
key = entry.getKey();
value = entry.getValue();
//System.out.println(entry.getKey() + "----" + entry.getValue());
}
Long end = System.currentTimeMillis();
System.out.println("showMapByEntrySet=" + (end - start));
}
private static void showMapByValue(Map<String, Integer> map) {
Long start = System.currentTimeMillis();
Integer value;
for (Integer v : map.values()) {
value = v;
// System.out.println(v);
}
Long end = System.currentTimeMillis();
System.out.println("showMapByValue=" + (end - start));
}
private static void showMapByKeySet(Map<String, Integer> map) {
Long start = System.currentTimeMillis();
Integer value;
String k;
for (String key : map.keySet()) {
k = key;
value = map.get(key);
// System.out.println(key + "-----" + map.get(key));
}
Long end = System.currentTimeMillis();
System.out.println("showMapByKeySet=" + (end - start));
}
public static Map<String, Integer> buildMap() {
Map<String, Integer> map = new HashMap<String, Integer>();
String key;
Integer value;
String[] str = new String[]{"a", "b", "c", "d", "e", "f", "g", "h", "o", "g", "l"};
for (int i = 0; i < 10000000; i++) {
int m = (int) (Math.random() * 10);
key = String.valueOf(str[m] + i * 100);
value = i;
map.put(key, value);
}
return map;
}
}
Map的常用方法
HashMap 继承于AbstractMap,实现了 Map、Cloneable、java.io.Serializable 接口。
size() //获取map集合类的大小
isEmpty() //判断是否为空
containsKey()//判断Map是否包含某个key值
containsValue()//判断是否包含某个value值
get()//通过键(key)的名字获取和键相匹配的值
put()//指定的key与指定的value添加到Map集合中
remove()//把指定的key,所对应的key值value,在Map集合中删除,返回被删除元素的value
putAll()//把某map的元素放到map中
clear()//清空
keySet()//获取所有的key值
values()//获取map中所有的values值;
entrySet()//获取map中的所有元素, k1=v2,k2=v2,.......形式输出;
getOrDefault()//如果map中含有指定的key,就返回该key对应的value,否则使用该方法的第二个参数作为默认值返回,
forEach()//遍历
replaceAll()//方法将 hashMap 中的所有映射关系替换成给定的函数所执行的结果。
putIfAbsent()//如果Map 中不存在指定的键,则将指定的键/值对插入到 Map 中。
remove()//删除
replace()//替换
computeIfAbsent()//对 hashMap 中指定 key 的值进行重新计算,如果不存在这个 key,则添加到 hasMap 中