HashMap的遍历方式有七种?经常用的感觉就两种,一种用迭代器,一种ForEach(增强型for循环),感觉就没了。网上查了一下,还真有其他的。今天总结一下。
四大类遍历方式
在JDK1.8之前,遍历方式还没有这样多地,为了提高开发效率,JDK1.8以后引入了Stream流,Lambda表达式等新特性。七种遍历方式看起来很多,其实可以分成四个大类:
迭代器方式遍历
foreach方式遍历
Lambda表达式遍历
Stream流方式遍历
它们又根据具体的实现形式分成多个小类:
1.迭代器方式遍历
EntrySet遍历方式
public class Test01 {
public static void main(String[] args) {
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("1","2");
hashMap.put("2","2");
hashMap.put("3","2");
hashMap.put("4","2");
hashMap.put("5","2");
hashMap.put("6","2");
//迭代器
Iterator<Map.Entry<String, String>> iterator = hashMap.entrySet().iterator();
while (iterator.hasNext()){
Map.Entry<String, String> next = iterator.next();
System.out.println(next.getKey()+" "+next.getValue());
}
}
}
KeySet遍历方式
package com.pf.gather;
import java.util.HashMap;
import java.util.Iterator;
public class Test02 {
public static void main(String[] args) {
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("1","21");
hashMap.put("2","22");
hashMap.put("3","23");
hashMap.put("4","24");
hashMap.put("5","25");
hashMap.put("6","26");
Iterator<String> iterator = hashMap.keySet().iterator();
while (iterator.hasNext()){
String key = iterator.next();
System.out.println(hashMap.get(key));
}
}
}
foreach方式遍历
EntrySet遍历方式
package com.pf.gather;
import java.util.HashMap;
import java.util.Map;
public class Test03 {
public static void main(String[] args) {
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("1","21");
hashMap.put("2","22");
hashMap.put("3","23");
hashMap.put("4","24");
hashMap.put("5","25");
hashMap.put("6","26");
for(Map.Entry<String,String> entry:hashMap.entrySet()){
System.out.println(entry.getKey()+" "+entry.getValue());
}
}
}
KeySet遍历方式
package com.pf.gather;
import java.util.HashMap;
import java.util.Map;
public class Test04 {
public static void main(String[] args) {
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("1","21");
hashMap.put("2","22");
hashMap.put("3","23");
hashMap.put("4","24");
hashMap.put("5","25");
hashMap.put("6","26");
for (String str:hashMap.keySet()){
System.out.println(hashMap.get(str));
}
}
}
Lambda表达式遍历
package com.pf.gather;
import java.util.HashMap;
public class Test05 {
public static void main(String[] args) {
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("1","21");
hashMap.put("2","22");
hashMap.put("3","23");
hashMap.put("4","24");
hashMap.put("5","25");
hashMap.put("6","26");
hashMap.forEach((key,value)->{
System.out.println(key+" "+value);
});
}
}
Stream流方式遍历
Streams API 单线程场景下遍历方式
package com.pf.gather;
import java.util.HashMap;
public class Test06 {
public static void main(String[] args) {
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("1","21");
hashMap.put("2","22");
hashMap.put("3","23");
hashMap.put("4","24");
hashMap.put("5","25");
hashMap.put("6","26");
hashMap.entrySet().stream().forEach((entry)->{
System.out.println(entry.getKey()+" "+entry.getValue());
});
}
}
Streams API 多线程场景下遍历方式
package com.pf.gather;
import java.util.HashMap;
public class Test07 {
public static void main(String[] args) {
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("1","21");
hashMap.put("2","22");
hashMap.put("3","23");
hashMap.put("4","24");
hashMap.put("5","25");
hashMap.put("6","26");
hashMap.entrySet().parallelStream().forEach((entry)->{
System.out.println(entry.getKey()+" "+entry.getValue());
});
}
}