博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java8 新特性
阅读量:6219 次
发布时间:2019-06-21

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

1.Lambada表达式:

public class Calculator {    public static void  main(String...args) {        Calculate plus = (int a, int b) -> a + b;        Calculate substract = (a, b) -> a - b;        Calculate multiply = (int a, int b) -> {
return a * b;}; Calculate divide = (a, b) -> {
return a / b;}; Printer printer = result -> System.out.println(result); int a = 20, b = 10; int result = plus.calculate(a, b); printer.print(result); result = substract.calculate(a, b); printer.print(result); result = multiply.calculate(a, b); printer.print(result result = divide.calculate(a, b); printer.print(result); } interface Calculate { int calculate(int a, int b); } interface Printer { void print(int result); }} 注意:   1.Lambada表达式也称为闭包,类似于C语言中的函数指针;   2.Lambada表达式,形参>=2个时,要用括号;一个参数时括号可以省略;   3.Lambada表达式, 如果闭包体只一个语句时,可以不用return, 默认返回该语句的结果;如果闭包体语句>=2时,返回值要用return;   4.Lambada表达式, 不能在闭包体内修改外部非final标记的变量;   5.Lambada表达式, 在闭包体内局部变量可以不用声明为 final,但是必须不可被后面的代码修改(即隐性的具有 final 的语义);   6.上面代码,如果接口中有两个访求,就不能用函数式接口赋值方式;
 
 

 2.方法引用

import java.util.function.Consumer;public class Calculator {    private static void print(String message) {        System.out.println(message);    }    private void sayHello(String world) {        System.out.println(world);    }    public static void  main(String...args) {        Consumer
print = Calculator::print; print.accept("message"); }} 注意:   1.常见的方法引用有 User::new, System.out::println

 3.函数式接口

import java.util.Arrays;import java.util.List;import java.util.function.Predicate;public class Calculator {    public static void main(String args[]) {        List
names = Arrays.asList("Jack", "Jimmy", "Tom", "Cate"); find(names, name -> name.indexOf("J") != -1); //找出名称中包含J的 find(names, name -> name.indexOf("C") != -1); //找出名称中包含C的 } private static void find(List
names, Predicate
predicate) { for(String name : names) { if (predicate.test(name)) { System.out.println(name); } } }} 注意:   1.闭包体“name -> name.indexOf("J") != -1”, 作为参数传递给predicate中, predicate.test(name), 将name作为参数传递给闭包体。   2.具体其他函数接口参照java.uitl.function包。
 

 4.接口默认方法

public class Calculator {    public static void main(String args[]) {        Calculator.Three three = new Calculator().new Three();        three.print();    }    class Three implements One, Two {        @Override        public void print() {            System.out.println("Three");            One.super.print();            Two.super.print();        }    }    interface One {        default void print() {            System.out.println("one");        }    }    interface Two {        default void print() {            System.out.println("two");        }    }} 注意:   1.接口中的默认方法在接口中默认实现,在接口中修改该方法,可以避免所有实现类都要修改实现方法的情况;   2.调用父接口中的方法是要用"父接口.super.方法"的形式。

5.Stream

import java.util.Arrays;import java.util.List;import java.util.stream.Collectors;public class Calculator {    public static void main(String args[]) {        List
users = Arrays.asList( new User(10, "Jack"), new User(20, "Tom"), new User(30, "Cate") ); //过滤年龄小于20的对象 List
collect = users.parallelStream().filter(user -> user.age < 20).collect(Collectors.toList()); System.out.println(collect); //过滤年龄小于20的对象的name属性 List
names = users.parallelStream().filter(user -> user.age < 20).map(User::getName).collect(Collectors.toList()); System.out.println(names); } private static class User { private Integer age; private String name; public User(Integer age, String name) { this.age = age; this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "User{" + "age=" + age + ", name='" + name + '\'' + '}'; } }}
import java.util.Arrays;import java.util.List;import java.util.Map;import java.util.stream.Collectors;public class Calculator {    public static void main(String args[]) {        List
users = Arrays.asList( new User(10, "Jack"), new User(20, "Tom"), new User(30, "Cate"), new User(30, "Cate") ); Map
> collect = users.parallelStream().collect(Collectors.groupingBy(user -> user.getAge() + user.getName(), Collectors.toList())); System.out.println(collect); } private static class User { private Integer age; private String name; public User(Integer age, String name) { this.age = age; this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "User{" + "age=" + age + ", name='" + name + '\'' + '}'; } }} 注意:   1.根据用户的age和name分组形成map   2.用非stream方法实现过于繁琐

 

import java.util.Arrays;import java.util.List;import java.util.Map;import java.util.function.BinaryOperator;import java.util.function.Function;import java.util.stream.Collectors;public class Calculator {    public static void main(String args[]) {        List
users = Arrays.asList( new User(10, "Jack"), new User(20, "Tom"), new User(30, "Cate"), new User(30, "Andy") ); Map
collect = users.parallelStream().collect(Collectors.toMap(new Function
() { @Override public Integer apply(User t) { return t.getAge(); } }, new Function
() { @Override public String apply(User t) { return t.getName(); } }, new BinaryOperator
() { @Override public String apply(String value1, String value2) { return value1 + value2; } })); System.out.println(collect); } private static class User { private Integer age; private String name; public User(Integer age, String name) { this.age = age; this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "User{" + "age=" + age + ", name='" + name + '\'' + '}'; } }}注意: 1.分组的非lambda表达式实现import java.util.Arrays;import java.util.List;import java.util.Map;import java.util.function.BinaryOperator;import java.util.function.Function;import java.util.stream.Collectors;public class Calculator { public static void main(String args[]) { List
users = Arrays.asList( new User(10, "Jack"), new User(20, "Tom"), new User(30, "Cate"), new User(30, "Andy") ); Map
collect = users.parallelStream().collect(Collectors.toMap(User::getAge, User::getName, (value1, value2) -> value1 + value2)); System.out.println(collect); } private static class User { private Integer age; private String name; public User(Integer age, String name) { this.age = age; this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "User{" + "age=" + age + ", name='" + name + '\'' + '}'; } }}注意: 1.分组的Lambda表达式实现
import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.function.BiConsumer;import java.util.function.Supplier;public class Calculator {    public static void main(String args[]) {        Map
map = new HashMap<>(); map.put("a", "AAA"); map.put("b", "BBB"); map.put("c", "CCC"); List
collect = map.values().parallelStream().collect(new Supplier
>() { @Override public List
get() { return new ArrayList
(); } }, new BiConsumer
, String>() { @Override public void accept(List
strs, String s) { strs.add(s); } }, new BiConsumer
, List
>() { @Override public void accept(List
o, List
o2) { o.addAll(o2); } }); System.out.println("object = " + collect); }} 注意:   1.map中的value转换成List,非Lambda实现;
import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;public class Calculator {    public static void main(String args[]) {        Map
hashMap = new HashMap<>(); hashMap.put("a", "AAA"); hashMap.put("b", "BBB"); hashMap.put("c", "CCC"); List
collect = hashMap.values().parallelStream().collect(ArrayList::new, (values, value) -> values.add(value), ArrayList::addAll); System.out.println("object = " + collect); }} 注意: 1.map中的value 转换成List, Lambda实现。
import java.util.*;import java.util.function.Predicate;public class Calculator {    public static void main(String args[]) {        List
integers = Arrays.asList(1, 2, 3, 4, 5); boolean b = integers.parallelStream().anyMatch(new Predicate
() { @Override public boolean test(Integer integer) { return integer == 2; } }); System.out.println(b); }}
import java.util.Arrays;import java.util.List;public class Calculator {    public static void main(String args[]) {        List
integers = Arrays.asList(1, 2, 3, 4, 5); boolean b = integers.parallelStream().anyMatch(integer -> integer == 2); System.out.println(b); }}
import java.util.Arrays;import java.util.List;import java.util.Set;import java.util.stream.Collectors;public class Calculator {    public static void main(String args[]) {        List
integers = Arrays.asList(1, 2, 3, 4, 5); Set
collect = integers.parallelStream().limit(3).collect(Collectors.toSet()); System.out.println(collect); }}

 

import java.util.Arrays;import java.util.IntSummaryStatistics;import java.util.List;public class Calculator {    public static void main(String args[]) {        List
numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7); IntSummaryStatistics stats = numbers.stream().mapToInt((x) -> x).summaryStatistics(); System.out.println("列表中最大的数 : " + stats.getMax()); System.out.println("列表中最小的数 : " + stats.getMin()); System.out.println("所有数之和 : " + stats.getSum()); System.out.println("平均数 : " + stats.getAverage()); }}

6.Optional

import java.util.Optional;public class Calculator {    public static void main(String args[]) {        String username = "abcdef";        String password = null;        Optional
password1 = Optional.ofNullable(password); System.out.println(password1.isPresent()); Optional
username1 = Optional.ofNullable(username); System.out.println(username1.isPresent()); }}

7.Nashorn Javascript引擎

import javax.script.ScriptEngine;import javax.script.ScriptEngineManager;public class Calculator {    public static void main(String args[]) {        ScriptEngineManager scriptEngineManager = new ScriptEngineManager();        ScriptEngine nashorn = scriptEngineManager.getEngineByName("nashorn");        String name = "dys";        try {            nashorn.eval("print('" + name + "')");        } catch (Exception e) {            e.printStackTrace();        }    }}

8.新的时间方面API

import java.time.LocalDateTime;public class Calculator {    public static void main(String args[]) {        LocalDateTime now = LocalDateTime.now();        System.out.println(now);        System.out.println(now.toLocalDate());        System.out.println(now.getHour() + ":" + now.getMinute() + ":" + now.getSecond());    }}

9,转码工具

   核心API:Base64。

转载地址:http://jpoja.baihongyu.com/

你可能感兴趣的文章
Eclipse快捷键
查看>>
关于H5自定义属性
查看>>
CentOS网络配置
查看>>
poj2396 Budget&&ZOJ1994 Budget[有源汇上下界可行流]
查看>>
Android:自定义控件样式(Selector)
查看>>
第一个springboot项目
查看>>
Windows10 使用docker toolbox安装docker
查看>>
响应式布局简介
查看>>
oracle 备份脚步
查看>>
docker探索-docker安装运行tomcat(六)
查看>>
详解CorelDRAW中如何合并与拆分对象
查看>>
ASP.NET跨平台最佳实践
查看>>
Python学习笔记8-单元测试(1)
查看>>
程序员英语学习指引
查看>>
三层和MVC 初识篇
查看>>
RecyclerView重用导致的元素重复问题
查看>>
动态调用WCF服务
查看>>
自訂 SQL Server 的 PadLeft、PadRight 字串填補函數
查看>>
c# enum用法
查看>>
Python—发邮件总结
查看>>