jdk8的新特性 Lambda表达式

很多同学一开始接触Java8可能对Java8 Lambda表达式有点陌生。 

//这是一个普通的集合

List<Employee> list = em.selectEmployeeByLoginLike(params);


   // 查看返回结果   这是jdk8之前的遍历方式

     for(Employee entry:list){
            System.out.println(entry);
       }

       // 查看返回结果   这是jdk8的一种新特性
      list.forEach(employee -> System.out.println(employee));

 

比如说我有这样一个Map

 private static Map<String, Integer> items = new HashMap<>();
    static {
        items.put("a", 30);
        items.put("b", 40);
        items.put("v", 50);
        items.put("d", 60);
        items.put("e", 20);
        items.put("f", 10);
    }
 //Java8之前遍历是这样遍历map
        for(Map.Entry<String,Integer> entry:items.entrySet()){
            System.out.println("key:" + entry.getKey() + " value:" + entry.getValue());
        }
        //Java8遍历map
        items.forEach((key,value)-> System.out.println("key:" + key + " value:" + value));

希望能对大家有点帮助,我也是刚刚才接触,不对的地方请大家指出
posted @ 2017-04-17 10:08  LuoBaiLin  阅读(200)  评论(0编辑  收藏  举报