JAVA8獨有的map遍歷方式(非常好用)
更新時間:2019年12月22日 14:11:28 作者:楓葉の飄雪
這篇文章主要介紹了JAVA8獨有的map遍歷方式(非常好用),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
使用JAV8 帶來的map遍歷方式使遍歷非常簡單
public class LambdaMap {
private Map<String, Object> map = new HashMap<>();
@Before
public void initData() {
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");
map.put("key4", 4);
map.put("key5", 5);
map.put("key5", 'h');
}
/**
* 遍歷Map的方式一
* 通過Map.keySet遍歷key和value
*/
@Test
public void testErgodicWayOne() {
System.out.println("---------------------Before JAVA8 ------------------------------");
for (String key : map.keySet()) {
System.out.println("map.get(" + key + ") = " + map.get(key));
}
System.out.println("---------------------JAVA8 ------------------------------");
map.keySet().forEach(key -> System.out.println("map.get(" + key + ") = " + map.get(key)));
}
/**
* 遍歷Map第二種
* 通過Map.entrySet使用Iterator遍歷key和value
*/
@Test
public void testErgodicWayTwo() {
System.out.println("---------------------Before JAVA8 ------------------------------");
Iterator<Map.Entry<String, Object>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, Object> entry = iterator.next();
System.out.println("key:value = " + entry.getKey() + ":" + entry.getValue());
}
System.out.println("---------------------JAVA8 ------------------------------");
map.entrySet().iterator().forEachRemaining(item -> System.out.println("key:value=" + item.getKey() + ":" + item.getValue()));
}
/**
* 遍歷Map第三種
* 通過Map.entrySet遍歷key和value,在大容量時推薦使用
*/
@Test
public void testErgodicWayThree() {
System.out.println("---------------------Before JAVA8 ------------------------------");
for (Map.Entry<String, Object> entry : map.entrySet()) {
System.out.println("key:value = " + entry.getKey() + ":" + entry.getValue());
}
System.out.println("---------------------JAVA8 ------------------------------");
map.entrySet().forEach(entry -> System.out.println("key:value = " + entry.getKey() + ":" + entry.getValue()));
}
/**
* 遍歷Map第四種
* 通過Map.values()遍歷所有的value,但不能遍歷key
*/
@Test
public void testErgodicWayFour() {
System.out.println("---------------------Before JAVA8 ------------------------------");
for (Object value : map.values()) {
System.out.println("map.value = " + value);
}
System.out.println("---------------------JAVA8 ------------------------------");
map.values().forEach(System.out::println); // 等價于map.values().forEach(value -> System.out.println(value));
}
/**
* 遍歷Map第五種
* 通過k,v遍歷,Java8獨有的
*/
@Test
public void testErgodicWayFive() {
System.out.println("---------------------Only JAVA8 ------------------------------");
map.forEach((k, v) -> System.out.println("key:value = " + k + ":" + v));
}
}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Java之定時器Timer和定時任務TimerTask應用以及原理解讀
文章介紹了Java JDK自帶的定時器Timer和定時任務TimerTask的使用和原理,Timer和TimerTask成對出現(xiàn),Timer是定時器,TimerTask是定時任務,TimerTask實現(xiàn)Runnable接口的run方法,Timer的屬性TimerThreadthread繼承Thread2024-12-12
Spring Boot JPA Repository之existsBy查詢方法失效的解決
這篇文章主要介紹了Spring Boot JPA Repository之existsBy查詢方法失效的解決方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
Spring boot @RequestBody數(shù)據(jù)傳遞過程詳解
這篇文章主要介紹了Spring boot @RequestBody數(shù)據(jù)傳遞過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-12-12
解決springboot configuration processor對maven子模塊不起作用的問題
這篇文章主要介紹了解決springboot configuration processor對maven子模塊不起作用的問題,本文通過圖文實例代碼給大家講解的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09

