Java基礎(chǔ)教程之Map遍歷的5種方式
前言
首先我們需要把map轉(zhuǎn)換為set進行遍歷,可使用entrySet和keySet共2種方式進行轉(zhuǎn)換。
每一種情況都可以采用下面的方式進行遍歷:
分別是使用迭代器iterator()遍歷;增強for循環(huán)遍歷;forEach+lambda循環(huán)遍歷,將循環(huán)簡化;還可以直接通過方法獲取Collection集合在進行便利;最后一個就是使用streams流遍歷。
創(chuàng)建一個集合
HashMap<Integer, String> map = new HashMap<>();
map.put(1,"ljj");
map.put(2,"zsy");
map.put(3,"sxf");
map.put(4,"wjx");
}方式一:Iterator 迭代器遍歷
map.entrySet().iterator();
map.keySet().iterator();
//Iterator 迭代器遍歷
public static void iterator(Map<Integer, String> map){
System.out.println("---使用entrySet()迭代器進行遍歷map集合---");
Iterator<Map.Entry<Integer, String>> entryIterator = map.entrySet().iterator();
while (entryIterator.hasNext()){
Map.Entry<Integer, String> entry = entryIterator.next();
System.out.println(entry.getKey()+":"+entry.getValue());
}
System.out.println("---使用KeySet()迭代器進行遍歷map集合---");
Iterator<Integer> keySetIterator = map.keySet().iterator();
while (keySetIterator.hasNext()){
Integer key = keySetIterator.next();
System.out.println(key+":"+map.get(key));
}
}方式二:For Each方式遍歷
map.forEach(BiConsumer action)
//For Each方式遍歷
public static void forEach_map(Map<Integer,String> map){
System.out.println("map.forEach(BiConsumer action)方法遍歷map");
map.forEach((key,value) -> System.out.println(key+" : "+value));
}方式三:獲取Collection集合
map.values().forEach()
map中的values()方法,通過這個方法可以直接獲取Map中存儲所有值的Collection集合
//values()方法
public static void Collection_forEach(Map<Integer,String> map){
System.out.println("獲取map集合的values值集合對象,進行forEach遍歷");
//Map中的values()方法,通過這個方法可以直接獲取Map中存儲所有值的Collection集合
Collection<String> values = map.values();
values.forEach( v -> System.out.println(v));
}方式四:增強for遍歷map
map.entrySet().for
map.keySet().for
public static void for_plus(Map<Integer,String> map){
System.out.println("增強for循環(huán),Map.Entry<>");
for (Map.Entry<Integer,String> entry : map.entrySet()){
String value = entry.getValue();
System.out.println(value);
}
System.out.println("增強for循環(huán),map.keySet");
for (Integer key : map.keySet()){
String value = map.get(key);
System.out.println(value);
}
}方法五:Stream流遍歷
map.entrySet().stream().forEach()
map.keySet().stream().forEach()
//Stream流遍歷
public static void stream_list(Map<Integer,String> map){
System.out.println("map.entrySet().stream.forEach()遍歷—Stream流遍歷");
map.entrySet().stream().forEach((Map.Entry<Integer,String> entry) -> {
System.out.print(entry.getKey()+":");
System.out.println(entry.getValue());
});
System.out.println("map.keySet().stream.forEach()遍歷—Stream流遍歷");
map.keySet().stream().forEach(key -> {
System.out.println(map.get(key));
});
}附:遍歷Map的性能比較
不同的遍歷方式對Map的性能有影響,下面通過實驗來看看它們之間的性能差異。
實驗代碼如下:
Map map = new HashMap<>();
Random random = new Random();
for (int i = 0; i < 1000000; i++) {
map.put(random.nextInt(1000000), "some value");
}
long start = System.currentTimeMillis();
for (Map.Entry entry : map.entrySet()) {
// do nothing
}
long end = System.currentTimeMillis();
System.out.println("遍歷entrySet耗時:" + (end - start) + "ms");
start = System.currentTimeMillis();
for (Integer key : map.keySet()) {
// do nothing
}
end = System.currentTimeMillis();
System.out.println("遍歷keySet耗時:" + (end - start) + "ms");
start = System.currentTimeMillis();
for (String value : map.values()) {
// do nothing
}
end = System.currentTimeMillis();
System.out.println("遍歷values耗時:" + (end - start) + "ms");實驗結(jié)果(遍歷1000000個元素,單位:毫秒):
遍歷entrySet耗時:14ms
遍歷keySet耗時:16ms
遍歷values耗時:33ms
從實驗結(jié)果來看,通過entrySet遍歷Map是最快的,而通過values遍歷是最慢的。
寫到最后
到此這篇關(guān)于Java基礎(chǔ)教程之Map遍歷的5種方式的文章就介紹到這了,更多相關(guān)Java Map遍歷方式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot在自定義類中調(diào)用service層等Spring其他層操作
這篇文章主要介紹了SpringBoot在自定義類中調(diào)用service層等Spring其他層操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
Mybatis-plus解決兼容oracle批量插入的示例詳解
Mybatis-Plus 是一個 MyBatis 的增強工具,提供無侵入、損耗小的 CRUD 操作,本文給大家介紹了Mybatis-plus解決兼容oracle批量插入,文中通過大家介紹的非常詳細(xì),需要的朋友可以參考下2024-11-11
一篇文章帶你解決 IDEA 每次新建項目 maven home directory 總是改變的問題
這篇文章主要介紹了一篇文章帶你解決 IDEA 每次新建項目 maven home directory 總是改變的問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09
spring security實現(xiàn)下次自動登錄功能過程解析
這篇文章主要介紹了spring security實現(xiàn)記住我下次自動登錄功能,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-11-11
SpringBoot中使用Jsoup爬取網(wǎng)站數(shù)據(jù)的方法
這篇文章主要介紹了SpringBoot中使用Jsoup爬取網(wǎng)站數(shù)據(jù)的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06

