本文总结下HashMap的7种遍历方式。
文章目录
概述
HashMap遍历大体上可以分为4类:
1,迭代器
2,ForEach 遍历
3,lambda 表达式遍历
4,StreamsApi 遍历
但是每种类型下有不同的实现方式,所以又可以分为7种:
使用迭代器 EntrySet 的方式遍历
import org.junit.Test;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class MyTest {
@Test
public void demo1(){
//创建Map 对象
Map<Integer, String> map = new HashMap<>();
//添加数据
map.put(1,"hello");
map.put(2,"hello2");
map.put(3,"hello3");
map.put(4,"hello4");
//遍历
Iterator<Map.Entry<Integer, String>> iterator = map.entrySet().iterator();
while (iterator.hasNext()){
Map.Entry<Integer, String> next = iterator.next();
System.out.println(next.getKey());
System.out.println(next.getValue());
}
}
}
运行结果
使用迭代器的KeySet
import org.junit.Test;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class MyTest {
@Test
public void demo1(){
//创建Map 对象
Map<Integer, String> map = new HashMap<>();
//添加数据
map.put(1,"hello");
map.put(2,"hello2");
map.put(3,"hello3");
map.put(4,"hello4");
//遍历
Iterator<Integer> iterator = map.keySet().iterator();
while (iterator.hasNext()){
Integer key = iterator.next();
System.out.print(key + ":");
System.out.println(map.get(key));
}
}
}
程序结果
使用 For Each EntrySet 的方式进行遍历
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
public class MyTest {
@Test
public void demo1(){
//创建Map 对象
Map<Integer, String> map = new HashMap<>();
//添加数据
map.put(1,"hello");
map.put(2,"hello2");
map.put(3,"hello3");
map.put(4,"hello4");
//遍历
for (Map.Entry<Integer,String> entry: map.entrySet()) {
System.out.println("entry.getKey() = " + entry.getKey());
System.out.println("entry.getValue() = " + entry.getValue());
}
}
}
程序结果
使用 For Each KeySet 的方式进行遍历
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
public class MyTest {
@Test
public void demo1(){
//创建Map 对象
Map<Integer, String> map = new HashMap<>();
//添加数据
map.put(1,"hello");
map.put(2,"hello2");
map.put(3,"hello3");
map.put(4,"hello4");
//遍历
for (Integer key: map.keySet()) {
System.out.print(key + ":");
System.out.println(map.get(key));
}
}
}
程序结果
hr>
使用 Lambda 表达式的方式进行遍历
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
public class MyTest {
@Test
public void demo1(){
//创建Map 对象
Map<Integer, String> map = new HashMap<>();
//添加数据
map.put(1,"hello");
map.put(2,"hello2");
map.put(3,"hello3");
map.put(4,"hello4");
//遍历
map.forEach((key,value) -> {
System.out.print(key + ":");
System.out.println(value);
});
}
}
程序结果
使用 Streams API 单线程的方式进行遍历
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
public class MyTest {
@Test
public void demo1(){
//创建Map 对象
Map<Integer, String> map = new HashMap<>();
//添加数据
map.put(1,"hello");
map.put(2,"hello2");
map.put(3,"hello3");
map.put(4,"hello4");
//遍历
map.entrySet().stream().forEach((integerStringEntry -> {
System.out.print(integerStringEntry.getKey() + ":");
System.out.println(integerStringEntry.getValue());
}));
}
}
程序结果
使用 Streams API 多线程的方式进行遍历
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
public class MyTest {
@Test
public void demo1(){
//创建Map 对象
Map<Integer, String> map = new HashMap<>();
//添加数据
map.put(1,"hello");
map.put(2,"hello2");
map.put(3,"hello3");
map.put(4,"hello4");
//遍历
map.entrySet().parallelStream().forEach((integerStringEntry -> {
System.out.println(integerStringEntry.getKey());
System.out.println(integerStringEntry.getValue());
}));
}
}
程序结果