博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java 8 Lambda 表达式
阅读量:4611 次
发布时间:2019-06-09

本文共 23891 字,大约阅读时间需要 79 分钟。

java8新增了一些特性,总结如下:

Lambda表达式 函数式接口 Stream Optional Predicate Function Consumer Filter Map-Reduce 新的Date API

Lambda 表达式,也可称为闭包,它是推动 Java 8 发布的最重要新特性。

Lambda 允许把函数作为一个方法的参数(函数作为参数传递进方法中)。

使用 Lambda 表达式可以使代码变的更加简洁紧凑。

一、初识Lambda 

语法

lambda 表达式的语法格式如下:

(parameters) -> expression或(parameters) ->{ statements; }

以下是lambda表达式的重要特征:

  • 可选类型声明:不需要声明参数类型,编译器可以统一识别参数值。
  • 可选的参数圆括号:一个参数无需定义圆括号,但多个参数需要定义圆括号。
  • 可选的大括号:如果主体包含了一个语句,就不需要使用大括号。
  • 可选的返回关键字:如果主体只有一个表达式返回值则编译器会自动返回值,大括号需要指定明表达式返回了一个数值。

Lambda 表达式实例

Lambda 表达式的简单例子:

// 1. 不需要参数,返回值为 5  () -> 5    // 2. 接收一个参数(数字类型),返回其2倍的值  x -> 2 * x    // 3. 接受2个参数(数字),并返回他们的差值  (x, y) -> x – y    // 4. 接收2个int型整数,返回他们的和  (int x, int y) -> x + y    // 5. 接受一个 string 对象,并在控制台打印,不返回任何值(看起来像是返回void)  (String s) -> System.out.print(s)

在 Java8Tester.java 文件输入以下代码:

public class Java8Tester {   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");   }       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);   }}
View Code

执行以上脚本,输出结果为:

$ javac Java8Tester.java $ java Java8Tester10 + 5 = 1510 - 5 = 510 x 5 = 5010 / 5 = 2Hello RunoobHello Google

使用 Lambda 表达式需要注意以下两点:

  • Lambda 表达式主要用来定义行内执行的方法类型接口,例如,一个简单方法接口。在上面例子中,我们使用各种类型的Lambda表达式来定义MathOperation接口的方法。然后我们定义了sayMessage的执行。
  • Lambda 表达式免去了使用匿名方法的麻烦,并且给予Java简单但是强大的函数化的编程能力。

变量作用域

lambda 表达式只能引用标记了 final 的外层局部变量,这就是说不能在 lambda 内部修改定义在域外的局部变量,否则会编译错误。

在 Java8Tester.java 文件输入以下代码:

public class Java8Tester {    final static String salutation = "Hello! ";      public static void main(String args[]){      GreetingService greetService1 = message ->       System.out.println(salutation + message);      greetService1.sayMessage("Runoob");   }       interface GreetingService {      void sayMessage(String message);   }}

执行以上脚本,输出结果为:

$ javac Java8Tester.java $ java Java8TesterHello! Runoob

我们也可以直接在 lambda 表达式中访问外层的局部变量:

public class Java8Tester {    public static void main(String args[]) {        final int num = 1;        Converter
s = (param) -> System.out.println(String.valueOf(param + num)); s.convert(2); // 输出结果为 3 } public interface Converter
{ void convert(int i); }}

lambda 表达式的局部变量可以不用声明为 final,但是必须不可被后面的代码修改(即隐性的具有 final 的语义)

int num = 1;  Converter
s = (param) -> System.out.println(String.valueOf(param + num));s.convert(2);num = 5; //报错信息:Local variable num defined in an enclosing scope must be final or effectively final

在 Lambda 表达式当中不允许声明一个与局部变量同名的参数或者局部变量。

String first = "";  Comparator
comparator = (first, second) -> Integer.compare(first.length(), second.length()); //编译会出错

为什么lambda表达式要用final

在 Java 8 之前,匿名类中如果要访问局部变量的话,那个局部变量必须显式的声明为  final,否则在匿名类中不可引用。

在 Java 8 下,即使局部变量未声明为 final 类型,一旦在匿名类中访问了一下就被强型加上了 final 属性,所以后面就无法再次给 version 赋值了:

String version = "1.8";foo(new Supplier() {  @Override  public String get() {   return version;  }});version = "1.7"; //在 Java 8 下注释这行就能编译通过,否则报出前面同样的错误

前面演示了是匿名类,在 Java 8 中换成 Lambda 表达式也是一回事:

String version = "1.8";foo(() -> version); //对局部变量 version 的访问让 version 变成 final 了version = "1.7";  //有了这行就编译不过了

因此,Java 8 的 Lambda 表达式访问局部变量时虽然没有硬性规定要被声明为 final,但实质上是和 Java 7 一样的。

总之一个局部变量如果要在 Java 7/8  的匿名类或是 Java 8 的 Lambda 表达式中访问,那么这个局部变量必须是 final 的,即使没有  final 饰它也是  final 类型。

注意,并不是 Lambda 开始访问时那个局部变量才变为 final,这是编译器的需求,例如

String version = "1.8";version = "1.7";      //注释掉这行或下行中另一行才能编译通过foo(() -> version );  //这行让编译器决定给 version 加上 final 属性

换句话说,如果在匿名类或 Lambda 表达式中访问的局部变量,如果不是 final 类型的话,编译器自动加上 final 修饰符。

  为什么 Lambda 表达式(匿名类) 不能访问非 final  的局部变量呢?因为实例变量存在堆中,而局部变量是在栈上分配,Lambda 表达(匿名类) 会在另一个线程中执行。如果在线程中要直接访问一个局部变量,可能线程执行时该局部变量已经被销毁了,而 final 类型的局部变量在 Lambda 表达式(匿名类) 中其实是局部变量的一个拷贝。

  你要是一定要在Lambda表达式里面修改外部变量的值也是可以的,可以将变量定义为实例变量或者将变量定义为数组或者封装到list、map集合中等方式解决。

String result = "select * from where id = '#userId#' and name = '#userName#'";Map
sysParams = new HashMap
();sysParams.put("#userId#", "userId");sysParams.put("#userName#", "userName");sysParams.put("#realName#", "realName");sysParams.put("#orgIds#", "orgIds");sysParams.put("#departname#", "departname");sysParams.put("#roleId#", "roleId"); sysParams.forEach((key,value)->{ if(result.contains(key)){//该行报错:Local variable result defined in an enclosing scope must be final or effectively final result = result.replaceAll(key, value); }

 

修改为数组封装:

String result = "select * from where id = '#userId#' and name = '#userName#'";    //将变量定义为数组就好了        String[] arr = new String[]{result};    Map
sysParams = new HashMap
(); sysParams.put("#userId#", "userId"); sysParams.put("#userName#", "userName"); sysParams.put("#realName#", "realName"); sysParams.put("#orgIds#", "orgIds"); sysParams.put("#departname#", "departname"); sysParams.put("#roleId#", "roleId"); sysParams.forEach((key,value)->{ if(arr[0].contains(key)){ //都是对数组进行操作了 arr[0] = arr[0].replaceAll(key, value); } });

 

二、lambda的使用

在Java 8以前的代码中,为了实现带一个方法的接口,往往需要定义一个匿名类并复写接口方法,代码显得很臃肿。比如常见的Comparator接口:

String[] oldWay = "Improving code with Lambda expressions in Java 8".split(" ");Arrays.sort(oldWay, new Comparator
() { @Override public int compare(String s1, String s2) { // 忽略大小写排序: return s1.toLowerCase().compareTo(s2.toLowerCase()); }});System.out.println(String.join(", ", oldWay));

对于只有一个方法的接口,在Java 8中,现在可以把它视为一个函数,用lambda表示式简化如下:

String[] newWay = "Improving code with Lambda expressions in Java 8".split(" ");Arrays.sort(newWay, (s1, s2) -> {    return s1.toLowerCase().compareTo(s2.toLowerCase());});System.out.println(String.join(", ", newWay));

Java 8没有引入新的关键字lambda,而是用()->{}这个奇怪的符号表示lambda函数。

对于Java自带的标准库里的大量单一方法接口,很多都已经标记为@FunctionalInterface,表明该接口可以作为函数使用。

Runnable接口为例,很多时候干活的代码还没有定义class的代码多,现在可以用lambda实现:

public static void main(String[] args) {    // old way:    Runnable oldRunnable = new Runnable() {        @Override        public void run() {            System.out.println(Thread.currentThread().getName() + ": Old Runnable");        }    };    Runnable newRunnable = () -> {        System.out.println(Thread.currentThread().getName() + ": New Lambda Runnable");    };    new Thread(oldRunnable).start();    new Thread(newRunnable).start();}

再看下面的代码:

interface eat {  void eatFood();}public static void main(String[] args) {  eat e = () -> System.out.printf("hello\n");  e.eatFood();    eat e1 = new eat()   {      @Override      public void eatFood()       {          System.out.printf("anoymous class\n");      }  };  e1.eatFood();}

上面的代码中,e是一个lambda的对象,根据java的继承的特性,我们可以说e对象的类型是继承自eat接口。而e1是一个正常的匿名类的对象.

通过对比, 可以说 lambda的表达式其实是接口的实现的“另一种方式”。这种方式更加简洁,更容易阅读。除了代码层面的简洁外,在编译的结果时候lambda也不会产生一个多余的匿名类。

对于eat这个特殊的接口,称之为:函数式接口。

函数式接口

什么是函数式接口?这个是我们理解Lambda表达式的重点,也是产生lambda表达式的“母体”,这里我们引用一个比较容易理解的说法:

函数式接口是 一个只有一个抽象方法(不包含object中的方法)的接口。

这个需要说明一点,就是在Java中任何一个对象都来自Object 所有接口中自然会继承自Object中的方法,但在判断是否是函数式接口的时候要排除Object中的方法。

Note:加不加@FunctionalInterface对于接口是不是函数式接口没有影响,该注解知识提醒编译器去检查该接口是否仅包含一个抽象方法。

三、结合实例

基础类

@Data@Builder@AllArgsConstructor@NoArgsConstructorpublic class Person {     private int id;    private String name;    private String address;  }

1、List操作

public class ExampleList {    private static List
items = new ArrayList<>(); static { items.add("A"); items.add("BC"); items.add("C"); items.add("BD"); items.add("E"); } public static void main(String[] args) { //Java8之前操作List for(String item:items){ System.out.println(item); } //Java8 lambda遍历list items.forEach(c-> System.out.println(c)); items.forEach(item->{ if("C".equals(item)){ System.out.println(item); } }); System.out.println("--------"); //先过滤 items.stream().filter(s->s.contains("B")).forEach(c1-> System.out.println(c1)); }
View Code

foreach循环

List
userList = initData();userList.forEach(user -> { System.out.println(user.getName());});

 

//使用lambda表达式对列表进行迭代// Java 8之前:List features = Arrays.asList("Lambdas", "Default Method", "Stream API", "Date and Time API");for (String feature : features) {    System.out.println(feature);}// Java 8之后:List features = Arrays.asList("Lambdas", "Default Method", "Stream API", "Date and Time API");features.forEach(n -> System.out.println(n)); // 使用Java 8的方法引用更方便,方法引用由::双冒号操作符标示,// 看起来像C++的作用域解析运算符features.forEach(System.out::println);//输出:LambdasDefault MethodStream APIDate and Time API

 

// 不使用lambda表达式为每个订单加上12%的税List costBeforeTax = Arrays.asList(100, 200, 300, 400, 500);for (Integer cost : costBeforeTax) {    double price = cost + .12*cost;    System.out.println(price);} // 使用lambda表达式List costBeforeTax = Arrays.asList(100, 200, 300, 400, 500);costBeforeTax.stream().map((cost) -> cost + .12*cost).forEach(System.out::println);//输出:112.0224.0336.0448.0560.0112.0224.0336.0448.0560.0

 

2、Map操作

public class ExampleMap {     private static Map
items = new HashMap<>(); static { items.put("A", 10); items.put("B", 20); items.put("C", 30); items.put("D", 40); items.put("E", 50); items.put("F", 60); } public static void main(String[] args) { //Java8之前遍历是这样遍历map for(Map.Entry
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)); } }
View Code

3、Groupingby操作

/** *  *Java8 Collectors.groupingBy and Collectors.mapping example */public class ExampleMapping {     private static List
personList = Lists.newArrayList(); static { personList.add(Person.builder().id(10).address("apple").address("shanghai").build()); personList.add(Person.builder().id(12).address("apple").address("wuhan").build()); personList.add(Person.builder().id(16).address("apple").address("nanjing").build()); } public static void main(String[] args) { //分组 Map
> collect = personList.stream().collect(Collectors.groupingBy(c -> c.getAddress())); System.out.println(collect); }}
View Code

4、List转换为Map

public class ExampleListConvertMap {     private static List
personList = Lists.newArrayList(); static{ personList.add(Person.builder().id(20).name("zhangsan").address("shanghai").build()); personList.add(Person.builder().id(30).name("lisi").address("nanjing").build()); } public static void main(String[] args) { //Java8 List转换Map Map
map_ = personList.stream().collect(Collectors.toMap((key->key.getId()),(value->value))); map_.forEach((key,value)-> System.out.println(key + ":" + value)); Map
mappedMovies = personList.stream().collect( Collectors.toMap(Person::getRank, Person::getData)); }}
View Code

5、FilterMap操作

public class ExampleFilterMap {     private static Map
map_ = Maps.newHashMap(); static{ map_.put(1, "linode.com"); map_.put(2, "heroku.com"); map_.put(3, "digitalocean.com"); map_.put(4, "aws.amazon.com"); } public static void main(String[] args) { //before java iterator map String result = null; for(Map.Entry
entry:map_.entrySet()){ if("heroku.com".equals(entry.getValue())){ result = entry.getValue(); } } System.out.println("Before Java 8 :" + result); //Java8 Map->Stream->Filter->String result = map_.entrySet().stream(). filter(map->"heroku.com".equals(map.getValue())) .map(map->map.getValue()) .collect(Collectors.joining()); System.out.println("Java 8 :" + result); Map
collect = map_.entrySet().stream() .filter(c->c.getKey()==2) .collect(Collectors.toMap(p->p.getKey(),p->p.getValue())); System.out.println(collect); }}
View Code

6、Optional操作可以防止NullPointException

Optional
optional = Optional.of("hello");System.out.println(optional.isPresent());//trueSystem.out.println(optional.get());//helloSystem.out.println(optional.orElse("false"));optional.ifPresent((s)-> System.out.println(s.charAt(0)));//h

7、一个详细的例子

@Data@Builder@AllArgsConstructor@NoArgsConstructorpublic class Employee {     private String name;    private int salary;    private String office;}  public class ExampleEmployee {     private static List
employeeList = Lists.newArrayList(); static{ employeeList.add(Employee.builder().name("Matt").salary(5000).office("New York").build()); employeeList.add(Employee.builder().name("Steve").salary(6000).office("London").build()); employeeList.add(Employee.builder().name("Carrie").salary(20000).office("New York").build()); employeeList.add(Employee.builder().name("Peter").salary(7000).office("New York").build()); employeeList.add(Employee.builder().name("Pat").salary(8000).office("London").build()); employeeList.add(Employee.builder().name("Tammy").salary(29000).office("Shanghai").build()); } public static void main(String[] args) { //anyMatch boolean isMatch = employeeList.stream().anyMatch(employee -> employee.getOffice().equals("London")); System.out.println(isMatch); //返回所有salary大于6000 boolean matched = employeeList.stream().allMatch(employee -> employee.getSalary()>4000); System.out.println(matched); //找出工资最高 Optional
hightestSalary = employeeList.stream().max((e1,e2)->Integer.compare(e1.getSalary(),e2.getSalary())); System.out.println(hightestSalary); //返回姓名列表 List
names = employeeList.stream().map(employee -> employee.getName()).collect(Collectors.toList()); System.out.println(names); //List转换成Map Map
employeeMap = employeeList.stream().collect(Collectors.toMap((key->key.getName()),(value->value))); employeeMap.forEach((key,value)-> System.out.println(key + "=" + value)); //统计办公室是New York的个数 long officeCount = employeeList.stream().filter(employee -> employee.getOffice().equals("Shanghai")).count(); System.out.println(officeCount); //List转换为Set Set
officeSet = employeeList.stream().map(employee -> employee.getOffice()).distinct().collect(Collectors.toSet()); System.out.println(officeSet); //查找办公室地点是New York的员工 Optional
allMatchedEmployees = employeeList.stream().filter(employee -> employee.getOffice().equals("New York")).findAny(); System.out.println(allMatchedEmployees); //按照工资的降序来列出员工信息 List
sortEmployeeList = employeeList.stream().sorted((e1,e2)->Integer.compare(e2.getSalary(),e1.getSalary())).collect(Collectors.toList()); //按照名字的升序列出员工信息 List
sortEmployeeByName = employeeList.stream().sorted((e1,e2)->e1.getName().compareTo(e2.getName())).collect(Collectors.toList()); System.out.println(sortEmployeeList); System.out.println("按照名字的升序列出员工信息:" + sortEmployeeByName); //获取工资最高的前2条员工信息 List
top2EmployeeList= employeeList.stream() .sorted((e1,e2)->Integer.compare(e2.getSalary(),e1.getSalary())) .limit(2) .collect(Collectors.toList()); System.out.println(top2EmployeeList); //获取平均工资 OptionalDouble averageSalary = employeeList.stream().mapToInt(employee->employee.getSalary()).average(); System.out.println("平均工资:" + averageSalary); //查找New York OptionalDouble averageSalaryByOffice = employeeList.stream().filter(employee -> employee.getOffice() .equals("New York")) .mapToInt(employee->employee.getSalary()) .average(); System.out.println("New York办公室平均工资:" + averageSalaryByOffice); }}
View Code

8、Java8常见操作

public class EmployeeTest {     public static List
generateData() { return Arrays.asList(new Employee("Matt", 5000, "New York"), new Employee("Steve", 6000, "London"), new Employee("Carrie", 10000, "New York"), new Employee("Peter", 7000, "New York"), new Employee("Alec", 6000, "London"), new Employee("Sarah", 8000, "London"), new Employee("Rebecca", 4000, "New York"), new Employee("Pat", 20000, "New York"), new Employee("Tammy", 9000, "New York"), new Employee("Fred", 15000, "Tokyo")); } public static Map
generateMapData() { Map
items = Maps.newHashMap(); items.put("A", 10); items.put("B", 20); items.put("C", 30); items.put("D", 40); items.put("E", 50); items.put("F", 60); return items; } @Test public void testEmployee() { List
results = generateData(); //打印出名字是Steve的员工信息 results.forEach(c -> { if (c.getName().equals("Steve")) { System.out.println(c); } }); System.out.println("---------"); //找出年薪超过6000的员工 results.stream().filter(c -> c.getSalary() >= 60000).forEach(c -> System.out.println(c)); System.out.println("--->>>>>>----"); //java8遍历map Map
map_ = generateMapData(); map_.forEach((key, value) -> System.out.println("key:" + key + "," + "value:" + value)); System.out.println("---->>>>分组>>>-----"); //java8 分组操作 Map
> groupMap = results.stream().collect(Collectors.groupingBy(c -> c.getOffice())); System.out.println(groupMap); System.out.println("---->>>>List转化为Map>>>----"); //List转化Map Map
map = results.stream().collect(Collectors.toMap(Employee::getName, Employee::getOffice)); map.forEach((key, value) -> System.out.println("key:" + key + "," + "value:" + value)); System.out.println("---->>>>>>>----"); Map
employeeMap = results.stream().collect(Collectors.toMap((key -> key.getSalary()), (value -> value))); employeeMap.forEach((key, value) -> System.out.println(key + "," + value)); System.out.println("---->>遍历map>>>----"); //打印出值大于30的map Map
resultMap = map_.entrySet().stream().filter(c -> c.getValue() > 30).collect(Collectors.toMap(p -> p.getKey(), p -> p.getValue())); resultMap.forEach((key, value) -> System.out.println(key + "=" + value)); System.out.println(">>>>>>>>>>>>>>>"); //打印key=D的map Map
mapResults = map_.entrySet().stream().filter(c -> c.getKey().equals("D")).collect(Collectors.toMap(p -> p.getKey(), p -> p.getValue())); mapResults.forEach((key, value) -> System.out.println(key + ">>>>" + value)); System.out.println(">>>>>>>Optional>>>>>>>"); Optional
optional = Optional.of("hello"); System.out.println(optional.isPresent()); } @Test public void testEmployeeExample() { //anyMatch List
employeeList = generateData(); boolean isMatch = employeeList.stream().anyMatch(employee -> employee.getOffice().equals("London")); System.out.println(isMatch); //allMatch boolean matched = employeeList.stream().allMatch(employee -> employee.getOffice().equals("London")); System.out.println(matched); //找出工资最高的 Optional
employeeOptional = employeeList.stream().max((e1,e2)->Integer.compare(e1.getSalary(),e2.getSalary())); System.out.println(employeeOptional); //找出工资最少的 Optional
employee = employeeList.stream().min((e1,e2)->Integer.compare(e1.getSalary(),e2.getSalary())); System.out.println(employee); //返回姓名列表 List
names = employeeList.stream().map(c->c.getName()).collect(Collectors.toList()); System.out.println(names); System.out.println(">>>>>>>>>>>"); //List转化Map Map
employeeMap = employeeList.stream().collect(Collectors.toMap((key->key.getName()),(value->value))); employeeMap.forEach((key,value)-> System.out.println(key + "=" + value)); //统计办公室是New York的个数 long officeCount = employeeList.stream().filter(c->c.getOffice().equals("New York")).count(); System.out.println(officeCount); long salaryCount = employeeList.stream().filter(c->c.getSalary()>60000).count(); System.out.println(salaryCount); //List转化为Set Set
officeSet = employeeList.stream().map(c->c.getOffice()).distinct().collect(Collectors.toSet()); System.out.println(officeSet); Set
salarySet = employeeList.stream().map(c->c.getSalary()).distinct().collect(Collectors.toSet()); System.out.println(salarySet); //查找办公室地点是New York的员工 Optional
optionals = employeeList.stream().filter(c->c.getOffice().equals("New York")).findAny(); System.out.println(optionals); System.out.println(">>>>>工资降序排序>>>>>"); //按照工资的降序来列出员工信息 List
sortSalaryEmployeeList = employeeList.stream().sorted((e1,e2)->Integer.compare(e2.getSalary(),e1.getSalary())).collect(Collectors.toList()); System.out.println(sortSalaryEmployeeList); System.out.println(">>>>>姓名升序排序>>>>>"); List
sortNameEmployeeList = employeeList.stream().sorted((e1,e2)->e1.getName().compareTo(e2.getName())).collect(Collectors.toList()); System.out.println(sortNameEmployeeList); System.out.println(">>>>获取工资最高的前2条员工信息"); List
dispaly2EmployeeList = employeeList.stream().sorted((e1,e2)->Integer.compare(e2.getSalary(),e1.getSalary())).limit(2).collect(Collectors.toList()); System.out.println(dispaly2EmployeeList); System.out.println(">>>>获取平均工资"); OptionalDouble averageSalary = employeeList.stream().mapToInt(c->c.getSalary()).average(); System.out.println(averageSalary); System.out.println(">>>>获取工作地点的平均工资"); OptionalDouble optionalDouble = employeeList.stream().filter(c->c.getOffice().equals("New York")).mapToInt(c->c.getSalary()).average(); System.out.println(optionalDouble); System.out.println(">>>>>>Java8 Optional用法>>>>>>"); Optional
stringOptional = Optional.of("test"); System.out.println(stringOptional.get()); Optional
isOptional = Optional.ofNullable("hello"); System.out.println(isOptional.isPresent()); System.out.println(isOptional.get()); System.out.println(isOptional.orElse("0")); System.out.println(">>>>>>>>>>>>"); //Optional
optionalVal = Optional.of(null); // System.out.println(optionalVal); Optional
optional = Optional.ofNullable("optional"); System.out.println(optional); System.out.println(optional.isPresent()); System.out.println(optional.get()); System.out.println(optional.orElse("haha")); System.out.println(">>>>>>>>>>>>"); Optional
employeeOptional_ = employeeList.stream().filter(c->c.getOffice().equals("New York")).findFirst(); System.out.println(employeeOptional_); }
View Code

 

 

 

 

参考文章:

 

转载于:https://www.cnblogs.com/java-jun-world2099/articles/11300634.html

你可能感兴趣的文章
webapi同一个Controller多个函数
查看>>
ASP.NET Web API身份验证和授权
查看>>
C#模拟POST提交表单(一)--WebClient
查看>>
java socket来实现私聊和群聊-简易版
查看>>
UWP开发入门(十二)——神器Live Visual Tree
查看>>
Ubuntu 16.04下安装64位谷歌Chrome浏览器
查看>>
如何配置寄存器
查看>>
ASP.NET中七种页面跳转的办法
查看>>
POJ2528 Mayor's poster 线段树+插入节点建树解法
查看>>
ViewTreeObserver简介
查看>>
JavaScript 30 - 1 学习笔记
查看>>
Nginx 假如reload或reopen时发生错误如何解决
查看>>
Linux网络配置方法(DNS,IP,GW)
查看>>
go语言基础之二维数组
查看>>
vim基本命令
查看>>
[转]HTTP协议之状态码详解
查看>>
box-shadow
查看>>
select * 和select 1 以及 select count(*) 和select count(1)的区别
查看>>
进度条04
查看>>
Silverlight RadGridView的HeaderCellStyle样式
查看>>