一般来说,有5种遍历,其中一种只遍历值,4种完全遍历整个地图。分类及原理介绍如下
划分
Map遍历:
遍历方法:分为foreach和iterator两种,foreach效率更高。这就是使用的方法和遍历因子的区别;
遍历因子:按照遍历因子划分,那么有keySet()和entrySet()两种,map的内部函数是通过Map.Entry接口实现的,keySet()是直接获取key,entrySet是获取每个条目先,然后使用条目的 getKey() 和 getValue() 完成遍历,速度会更好。
CreateTraversal.java(使用示例)
import lombok.extern.slf4j.Slf4j;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
* @Author: renjiaxin
* @Despcription:
* @Date: Created in 2018/8/17 9:52
* @Modified by:
*/
@Slf4j
public class CreateTraversal {
public static Map<String, Object> createMap() {
Map<String, Object> map = new HashMap<>();
map.put("Sky Sea Wing", "36C");
map.put("Ozawa Maria", "42F");
map.put("", "");//Yes, it can be defined like this
map.put(null, null);/*key can be null, but there can only be one, and only one works*/
map.put(null, null);
map.put("Hatano Yui", "36C");
map.put("Aoi Sola", "28B");
map.put(" ", "");//Yes, it can be defined like this
map.put("Ogura Yuko", "26A");
map.put("Fukada Kyoko", null);
map.put("Amamiya piano sound", "36C");
map.put("Hojo Mafei", null);//value is null, data is temporarily empty, value is null and there can be multiple
map.put("Green Wasabi", "43F");
return map;
}
public static void traversalMap(Map<String, Object> map, int type) {
if (type < 0 || type > 4) {
log.warn("Wrong traversal type, type:{}", type);
}
switch (type) {
case 0:
partForEachTraversal(map);
break;
case 1:
forEachKeySetTraversal(map);
break;
case 2:
forEachEntrySetTraversal(map);
break;
case 3:
iteratorKeySetTraversal(map);
break;
case 4:
System.out.println("Recommended map.entrySet()+iterator");
iteratorEntrySetTraversal(map);
break;
default:
iteratorEntrySetTraversal(map);
}
}
/*It just traversed the values, not the keys*/
private static void partForEachTraversal(Map<String, Object> map) {
System.out.println("\nUse values to loop, can only traverse values, not keys");
long start = System.currentTimeMillis();
for (Object value : map.values()) {
System.out.print(value + " ");
}
long end = System.currentTimeMillis();
long time = start - end;
System.out.println("\nmap size n: "+ map.values().size());
System.out.println("The running time is: "+ time + "ms");
System.out.println("");
}
/*map.keySet()+foreach*/
private static void forEachKeySetTraversal(Map<String, Object> map) {
System.out.println("map.keySet()+foreach, complete traversal, slow speed!");
long start = System.currentTimeMillis();
for (String key : map.keySet()) {
System.out.print("key: " + key + ", values: " + map.get(key) + "; ");
}
long end = System.currentTimeMillis();
long time = start - end;
System.out.println("\nmap size n: "+ map.values().size());
System.out.println("The running time is: "+ time + "ms");
System.out.println("");
}
/*map.entrySet()+foreach*/
private static void forEachEntrySetTraversal(Map<String, Object> map) {
System.out.println("map.entrySet()+foreach, complete traversal, fast!");
System.out.println("Map.Entry is an interface inside the Map class, which provides the main methods and functions of the Map class");
long start = System.currentTimeMillis();
Set<Map.Entry<String, Object>> entrySet = map.entrySet();//Take out this set
for (Map.Entry<String, Object> entry : entrySet) {/*---*/
System.out.print("key= " + entry.getKey() + " and value= " + entry.getValue() + "; ");
}
long end = System.currentTimeMillis();
long time = start - end;
System.out.println("\nmap size n: "+ map.values().size());
System.out.println("The running time is: "+ time + "ms");
System.out.println("");
}
/*map.keySet()+iterator*/
private static void iteratorKeySetTraversal(Map<String, Object> map) {
System.out.println("map.keySet()+iterator, complete traversal");
long start = System.currentTimeMillis();
Iterator<String> it = map.keySet().iterator();/**/
while (it.hasNext()) {
System.out.print("key :" + it.next() + " , value: " + map.get(it.next()));
}
long end = System.currentTimeMillis();
long time = start - end;
System.out.println("\nmap size n: "+ map.values().size());
System.out.println("The running time is: "+ time + "ms");
System.out.println("");
}
/*map.entrySet()+iterator*/
private static void iteratorEntrySetTraversal(Map<String, Object> map) {
System.out.println("map.entrySet()+iterator, complete traversal, the recommended approach!");
long start = System.currentTimeMillis();
//Iterator it = map.entrySet().iterator();/*---*/
Iterator<Map.Entry<String, Object>> it = map.entrySet().iterator();//Same up and down, this one is clearer
while (it.hasNext()) {
System.out.print("key :" + it.next() + " , value: " + map.get(it.next()));
}
long end = System.currentTimeMillis();
long time = start - end;
System.out.println("\nmap size n: "+ map.values().size());
System.out.println("The running time is: "+ time + "ms");
System.out.println("");
}
public static void main(String[] args) {
System.out.println("Because map uses a key-value form of data structure, there is no position in digital form, so for(int i=0;i<n;i++ form)");
Map<String, Object> mp = createMap();
traversalMap(mp, 0);
traversalMap(mp, 1);
traversalMap(mp, 2);
traversalMap(mp, 3);
traversalMap(mp, 4);
}
}
你适合学Java吗?4大专业测评方法
代码逻辑 吸收能力 技术学习能力 综合素质
先测评确定适合在学习