-----------------------------------------------------------下面是例子[1]-------------------------------------------------------------------------------------

代码来自[1],但是[1]中的代码不完整,所以修改后如下:

import java.util.*;
import java.util.List;
import java.util.stream.Collectors;
public class Main
{
    private static class Customer
    {
        private String name;
        private int points;
        private Customer(String n,int p){
            name=n;
            points=p;
        }
        //getter
    }

public static void main(String[] args)
    {

Customer john = new Customer("John P.", 15);
        Customer sarah = new Customer("Sarah M.", 200);
        Customer charles = new Customer("Charles B.", 150);
        Customer mary = new Customer("Mary T.", 1);

List<Customer> customers = Arrays.asList(john, sarah, charles, mary);

List<Customer> customersWithMoreThan100Points = customers
                .stream()
                .filter(c -> c.points > 100)
                .collect(Collectors.toList());

//        然后对列表中的对象进行读取即可。
        System.out.println(customersWithMoreThan100Points.get(0).name);
        System.out.println(customersWithMoreThan100Points.get(0).points);
        System.out.println(customersWithMoreThan100Points.get(1).name);
        System.out.println(customersWithMoreThan100Points.get(1).points);

}
    }

输出结果如下:
Sarah M.
Charles B.

Process finished with exit code 0

-----------------------------------------------------------下面是例子[2]-------------------------------------------------------------------------------------

public class Java8Tester {

interface MathOperation {
        int operation(int a, int b);
    }

interface GreetingService {
        void sayMessage(String message);
    }

private int operate(int a, int b, MathOperation mathOperation){
        return mathOperation.operation(a, b);
    }

public static void main(String args[]){
        Java8Tester tester = new Java8Tester();

// 类型声明
        MathOperation addition = (int a, int b) -> a + b;

// 不用类型声明
        MathOperation subtraction = (a, b) -> a - b;

// 大括号中的返回语句
        MathOperation multiplication = (int a, int b) -> { return a * b; };

// 没有大括号及返回语句
        MathOperation division = (int a, int b) -> a / b;

System.out.println("10 + 5 = " + tester.operate(10, 5, addition));
        System.out.println("10 - 5 = " + tester.operate(10, 5, subtraction));
        System.out.println("10 x 5 = " + tester.operate(10, 5, multiplication));
        System.out.println("10 / 5 = " + tester.operate(10, 5, division));

// 不用括号
        GreetingService greetService1 = message ->
                System.out.println("Hello " + message);

// 用括号
        GreetingService greetService2 = (message) ->
                System.out.println("Hello " + message);

greetService1.sayMessage("Runoob");
        greetService2.sayMessage("Google");
    }

}

Reference:

[1]Java Stream Filter with Lambda Expression

[2]JAVA8 新特性~~~~

java8中的lambda用法实例相关推荐

  1. python中max函数用法_Python中max函数用法实例分析

    Python中max函数用法实例分析 更新时间:2015年07月17日 15:45:09 作者:优雅先生 这篇文章主要介绍了Python中max函数用法,实例分析了Python中max函数的功能与使用 ...

  2. java8中的lambda表达式实用详解

    java8中的lambda表达式实用详解 1. lambda简介 ​ Lambda 表达式(lambda expression)是一个匿名函数,Lambda表达式基于数学中的λ演算得名,直接对应于其中 ...

  3. java8中 Collectors.groupingBy用法

    目录 1.数据准备: 2.分组 按照类目分组: 按照几个属性拼接分组: 根据不同条件分组 3.多级分组 4.按子组收集数据 求总数 求和 把收集器的结果转换为另一种类型 联合其他收集器 Collect ...

  4. python编程字典100例_python中字典(Dictionary)用法实例详解

    本文实例讲述了python中字典(Dictionary)用法.分享给大家供大家参考.具体分析如下: 字典(Dictionary)是一种映射结构的数据类型,由无序的"键-值对"组成. ...

  5. python元类的使用_python中元类用法实例

    本文实例讲述了python中元类用法,分享给大家供大家参考.具体方法分析如下: 1.元类(metaclass)是用来创建类的类 2.type(object):返回一个对象的类型,与object.__c ...

  6. python中mysqldb模块_python中MySQLdb模块用法实例

    本文实例讲述了python中MySQLdb模块用法.分享给大家供大家参考.具体用法分析如下: MySQLdb其实有点像php或asp中连接数据库的一个模式了,只是MySQLdb是针对mysql连接了接 ...

  7. python中property函数_python 邮件表格Python中property函数用法实例分析

    本文实例讲述了Python中property函数用法.分享给大家供大家参考,具体如下: 通常我们在访问和赋值属性的时候,都是在直接和类(实例的)的__dict__打交道,或者跟数据描述符等在打交道.但 ...

  8. zip在python中的用法_Python中zip()函数用法实例教程

    本文实例讲述了Python中zip()函数的定义及用法,相信对于Python初学者有一定的借鉴价值.详情如下: 一.定义: zip([iterable, ...])zip()是Python的一个内建函 ...

  9. php simplexml_load_file 详解,php中simplexml_load_file函数用法实例讲解

    php中simplexml_load_file函数用法实例讲解 发布于 2015-02-07 06:53:40 | 136 次阅读 | 评论: 0 | 来源: 网友投递 PHP开源脚本语言PHP(外文 ...

最新文章

  1. 【随机共振】基于随机共振的高频弱信号检测的MATLAB仿真
  2. MM模块常用T-code
  3. snapshot相关
  4. 仿短视频竖屏播放源码
  5. oc的分类category
  6. linux常见权限相关错误及解决
  7. 正则表达式批量重命名
  8. 使用软件测试路由器性能报告,小米路由器网络性能初步测试报告
  9. php框架laravel下载,Laravel框架下载,安装及路由操作图文详解
  10. 【2021版】想要专升本你不得不看的全干货_吐血整理_专升本_计算机文化基础(三)
  11. python 三角函数 计算
  12. 申请计算机软件著作权费用,申请计算机软件著作权费用多少有哪些费用
  13. C#通过LPT1端口控制打印机
  14. win10的安装与优化
  15. Linux部署KVM虚拟化平台
  16. 如何使用ps的扭曲里面的旋转扭曲
  17. http://www.youku.com/playlist_show/id_4637211.html
  18. .Net Core环境搭建Hangfire项目
  19. 各种滤波器的设计实现
  20. 从零开始学Python爬虫系列:Matplotlib FuncAnimation这1个功能,加1个更新函数,画出最简单的动图,让你爬取的数据动起来。(附:图片上加弹幕功能)

热门文章

  1. python学习之函数
  2. 【剑指offer】面试题31:连续子数组的最大和
  3. mysql5.5.30源码安装及主从搭建
  4. 每日一句(2014-9-22)
  5. 华为内部面试题库---(2)
  6. 浅谈代码的执行效率(2):编译器的威力 [摘自赵劼老师的博客]
  7. mac下使用n管理node版本
  8. vue获取当前日期和时间并进行格式化
  9. kotlin android获取按钮,Kotlin 实现按钮点击跳转监听事件方式
  10. android 常驻轮询服务,Android实现自动轮询的RecycleView