lambda表達式 遍歷list 或者 使用 map.map.entrySet() entry.getKey()

package com.winner.eurekaserver;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 用lamdba  一次性遍歷 list<map>
 *
 * @author wangxl
 * @date 2019-09-20
 */
public class T {

    /**
     *
     * 用lamdba  一次性遍歷 list<map>   foreach的嵌套
     *
     * @param args
     */
    public static void main(String[] args) {
        HashMap<String, Object> hm = new HashMap<>();
        hm.put("id", 11);
        hm.put("name", "張三");
        ArrayList<HashMap> list = new ArrayList<>();
        list.add(hm);
        list.forEach(hashMap -> hashMap.forEach((k, v) -> System.out.println(k + "----" + v)));
    }
}
​
//遍歷hashmap最常用的方法, 使用For-Each迭代entries
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for(Map.Entry<Integer, Integer> entry : map.entrySet()){
	System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue())
}


​
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章