新特性
一、Java8新特性
1.Lambda表达式
Lambda表达式的使用
1.举例: (o1,o2) -> Integer.compare(o1,o2);
2.格式:
-> :lambda操作符 或 箭头操作符
->左边:lambda形参列表 (其实就是接口中的抽象方法的形参列表)
->右边:lambda体 (其实就是重写的抽象方法的方法体)
3.Lambda表达式的使用:(分为6种情况介绍)
总结:
->左边:lambda形参列表的参数类型可以省略(类型推断);如果lambda形参列表只有一个参数,其一对()也可以省略
->右边:lambda体应该使用一对{}包裹;如果lambda体只有一条执行语句(可能是return语句),省略这一对{}和return关键字
4.Lambda表达式的本质:作为函数式接口的实例
5.如果一个接口中,只声明了一个抽象方法,则此接口就称为函数式接口。我们可以在一个接口上使用 @FunctionalInterface 注解,
这样做可以检查它是否是一个函数式接口。
6.所以以前用匿名实现类表示的现在都可以用Lambda表达式来写。
Lambda表达式的使用举例
LambdaTest1.java
public class LambdaTest1 {
@Test
public void test1 ( ) {
Runnable r1 = new Runnable ( ) {
@Override
public void run ( ) {
System. out. println ( "我爱北京天安门" ) ;
}
} ;
r1. run ( ) ;
Runnable r2 = ( ) - > {
System. out. println ( "我爱北京故宫" ) ;
} ;
r2. run ( ) ;
}
@Test
public void test2 ( ) {
Consumer< String> con = new Consumer < String> ( ) {
@Override
public void accept ( String s) {
System. out. println ( s) ;
}
} ;
con. accept ( "谎言和誓言的区别是什么?" ) ;
Consumer< String> con1 = ( String s) - > {
System. out. println ( s) ;
} ;
con1. accept ( "一个是听的人当真了,一个是说的人当真了" ) ;
}
@Test
public void test3 ( ) {
Consumer< String> con1 = ( String s) - > {
System. out. println ( s) ;
} ;
con1. accept ( "一个是听得人当真了,一个是说的人当真了" ) ;
Consumer< String> con2 = ( s) - > {
System. out. println ( s) ;
} ;
con2. accept ( "一个是听得人当真了,一个是说的人当真了" ) ;
}
@Test
public void test4 ( ) {
Consumer< String> con1 = ( s) - > {
System. out. println ( s) ;
} ;
con1. accept ( "一个是听得人当真了,一个是说的人当真了" ) ;
Consumer< String> con2 = s - > {
System. out. println ( s) ;
} ;
con2. accept ( "一个是听得人当真了,一个是说的人当真了" ) ;
}
@Test
public void test5 ( ) {
Comparator< Integer> com1 = new Comparator < Integer> ( ) {
@Override
public int compare ( Integer o1, Integer o2) {
System. out. println ( o1) ;
System. out. println ( o2) ;
return o1. compareTo ( o2) ;
}
} ;
System. out. println ( com1. compare ( 12 , 21 ) ) ;
Comparator< Integer> com2 = ( o1, o2) - > {
System. out. println ( o1) ;
System. out. println ( o2) ;
return o1. compareTo ( o2) ;
} ;
System. out. println ( com2. compare ( 12 , 6 ) ) ;
}
@Test
public void test6 ( ) {
Comparator< Integer> com1 = ( o1, o2) - > {
return o1. compareTo ( o2) ;
} ;
System. out. println ( com1. compare ( 12 , 6 ) ) ;
Comparator< Integer> com2 = ( o1, o2) - > o1. compareTo ( o2) ;
System. out. println ( com2. compare ( 12 , 21 ) ) ;
}
}
java内置的4大核心函数式接口
消费型接口 Consumer<T> void accept(T t)
供给型接口 Supplier<T> T get()
函数型接口 Function<T,R> R apply(T t)
断定型接口 Predicate<T> boolean test(T t)
LambdaTest2.java
public class LambdaTest2 {
@Test
public void test1 ( ) {
happyTime ( 500 , new Consumer < Double> ( ) {
@Override
public void accept ( Double aDouble) {
System. out. println ( "矿泉水价格为:" + aDouble) ;
}
} ) ;
happyTime ( 400 , money - > System. out. println ( "去喝了口水,价格为:" + money) ) ;
}
public void happyTime ( double money, Consumer< Double> con) {
con. accept ( money) ;
}
@Test
public void test2 ( ) {
List< String> list = Arrays. asList ( "北京" , "南京" , "天津" , "东京" , "西京" , "普京" ) ;
List< String> filterStrs = filterString ( list, new Predicate < String> ( ) {
@Override
public boolean test ( String s) {
return s. contains ( "京" ) ;
}
} ) ;
System. out. println ( filterStrs) ;
List< String> filterStrs1 = filterString ( list, s - > s. contains ( "京" ) ) ;
System. out. println ( filterStrs1) ;
}
public List< String> filterString ( List< String> list, Predicate< String> pre) {
ArrayList< String> filterList = new ArrayList < > ( ) ;
for ( String s : list) {
if ( pre. test ( s) ) {
filterList. add ( s) ;
}
}
return filterList;
}
}
方法引用
方法引用的使用
1.使用情境:当要传递给Lambda体的操作,已经有实现的方法了,可以使用方法引用!
2.方法引用,本质上就是Lambda表达式,而Lambda表达式作为函数式接口的实例。所以
方法引用,也是函数式接口的实例。
3.使用格式: 类(或对象) :: 方法名
4.具体分为如下的三种情况:
情况1 对象 :: 非静态方法
情况2 类 :: 静态方法
情况3 类 :: 非静态方法
5.方法引用使用的要求:要求接口中的抽象方法的形参列表和返回值类型与方法引用的方法的
形参列表和返回值类型相同!(针对于情况1和情况2)
Employee.java
public class Employee {
private int id;
private String name;
private int age;
private double salary;
public int getId ( ) {
return id;
}
public void setId ( int id) {
this . id = id;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public int getAge ( ) {
return age;
}
public void setAge ( int age) {
this . age = age;
}
public double getSalary ( ) {
return salary;
}
public void setSalary ( double salary) {
this . salary = salary;
}
public Employee ( ) {
System. out. println ( "Employee()....." ) ;
}
public Employee ( int id) {
this . id = id;
System. out. println ( "Employee(int id)....." ) ;
}
public Employee ( int id, String name) {
this . id = id;
this . name = name;
}
public Employee ( int id, String name, int age, double salary) {
this . id = id;
this . name = name;
this . age = age;
this . salary = salary;
}
@Override
public String toString ( ) {
return "Employee{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + ", salary=" + salary + '}' ;
}
@Override
public boolean equals ( Object o) {
if ( this == o)
return true ;
if ( o == null || getClass ( ) != o. getClass ( ) )
return false ;
Employee employee = ( Employee) o;
if ( id != employee. id)
return false ;
if ( age != employee. age)
return false ;
if ( Double. compare ( employee. salary, salary) != 0 )
return false ;
return name != null ? name. equals ( employee. name) : employee. name == null;
}
@Override
public int hashCode ( ) {
int result;
long temp;
result = id;
result = 31 * result + ( name != null ? name. hashCode ( ) : 0 ) ;
result = 31 * result + age;
temp = Double. doubleToLongBits ( salary) ;
result = 31 * result + ( int ) ( temp ^ ( temp >>> 32 ) ) ;
return result;
}
}
EmployeeData.java
public class EmployeeData {
public static List< Employee> getEmployees ( ) {
List< Employee> list = new ArrayList < > ( ) ;
list. add ( new Employee ( 1001 , "马化腾" , 34 , 6000.38 ) ) ;
list. add ( new Employee ( 1002 , "马云" , 12 , 9876.12 ) ) ;
list. add ( new Employee ( 1003 , "刘强东" , 33 , 3000.82 ) ) ;
list. add ( new Employee ( 1004 , "雷军" , 26 , 7657.37 ) ) ;
list. add ( new Employee ( 1005 , "李彦宏" , 65 , 5555.32 ) ) ;
list. add ( new Employee ( 1006 , "比尔盖茨" , 42 , 9500.43 ) ) ;
list. add ( new Employee ( 1007 , "任正非" , 26 , 4333.32 ) ) ;
list. add ( new Employee ( 1008 , "扎克伯格" , 35 , 2500.32 ) ) ;
return list;
}
}
MethodRefTest.java
public class MethodRefTest {
@Test
public void test1 ( ) {
Consumer< String> con1 = str - > System. out. println ( str) ;
con1. accept ( "北京" ) ;
PrintStream ps = System. out;
Consumer< String> con2 = ps: : println;
con2. accept ( "beijing" ) ;
}
@Test
public void test2 ( ) {
Employee emp = new Employee ( 1001 , "Tom" , 23 , 5600 ) ;
Supplier< String> sup1 = ( ) - > emp. getName ( ) ;
System. out. println ( sup1. get ( ) ) ;
Supplier< String> sup2 = emp: : getName;
System. out. println ( sup2. get ( ) ) ;
}
@Test
public void test3 ( ) {
Comparator< Integer> com1 = ( t1, t2) - > Integer. compare ( t1, t2) ;
System. out. println ( com1. compare ( 12 , 21 ) ) ;
Comparator< Integer> com2 = Integer: : compare;
System. out. println ( com2. compare ( 12 , 3 ) ) ;
}
@Test
public void test4 ( ) {
Function< Double, Long> func = new Function < Double, Long> ( ) {
@Override
public Long apply ( Double d) {
return Math. round ( d) ;
}
} ;
Function< Double, Long> func1 = d - > Math. round ( d) ;
System. out. println ( func1. apply ( 12.3 ) ) ;
Function< Double, Long> func2 = Math: : round;
System. out. println ( func2. apply ( 12.6 ) ) ;
}
@Test
public void test5 ( ) {
Comparator< String> com1 = ( s1, s2) - > s1. compareTo ( s2) ;
System. out. println ( com1. compare ( "abc" , "abd" ) ) ;
Comparator< String> com2 = String : : compareTo;
System. out. println ( com2. compare ( "abd" , "abm" ) ) ;
}
@Test
public void test6 ( ) {
BiPredicate< String, String> pre1 = ( s1, s2) - > s1. equals ( s2) ;
System. out. println ( pre1. test ( "abc" , "abc" ) ) ;
BiPredicate< String, String> pre2 = String : : equals;
System. out. println ( pre2. test ( "abc" , "abd" ) ) ;
}
@Test
public void test7 ( ) {
Employee employee = new Employee ( 1001 , "Jerry" , 23 , 6000 ) ;
Function< Employee, String> func1 = e - > e. getName ( ) ;
System. out. println ( func1. apply ( employee) ) ;
Function< Employee, String> func2 = Employee: : getName;
System. out. println ( func2. apply ( employee) ) ;
}
}
构造器引用和数组引用
一、构造器引用
和方法引用类似,函数式接口的抽象方法的形参列表和构造器的形参列表一致。
抽象方法的返回值类型即为构造器所属的类的类型
二、数组引用
大家可以把数组看做是一个特殊的类,则写法与构造器引用一致。
ConstructorRefTest.java
public class ConstructorRefTest {
@Test
public void test1 ( ) {
Supplier< Employee> sup = new Supplier < Employee> ( ) {
@Override
public Employee get ( ) {
return new Employee ( ) ;
}
} ;
Supplier< Employee> sup1 = ( ) - > new Employee ( ) ;
System. out. println ( sup1. get ( ) ) ;
Supplier< Employee> sup2 = Employee : : new ;
System. out. println ( sup2. get ( ) ) ;
}
@Test
public void test2 ( ) {
Function< Integer, Employee> func1 = id - > new Employee ( id) ;
Employee employee = func1. apply ( 1001 ) ;
System. out. println ( employee) ;
Function< Integer, Employee> func2 = Employee : : new ;
Employee employee1 = func2. apply ( 1002 ) ;
System. out. println ( employee1) ;
}
@Test
public void test3 ( ) {
BiFunction< Integer, String, Employee> func1 = ( id, name) - > new Employee ( id, name) ;
System. out. println ( func1. apply ( 1001 , "Tom" ) ) ;
BiFunction< Integer, String, Employee> func2 = Employee : : new ;
System. out. println ( func2. apply ( 1002 , "Tom" ) ) ;
}
@Test
public void test4 ( ) {
Function< Integer, String[ ] > func1 = length - > new String [ length] ;
String[ ] arr1 = func1. apply ( 5 ) ;
System. out. println ( Arrays. toString ( arr1) ) ;
Function< Integer, String[ ] > func2 = String[ ] : : new ;
String[ ] arr2 = func2. apply ( 10 ) ;
System. out. println ( Arrays. toString ( arr2) ) ;
}
}
2.Stream API
1. Stream关注的是对数据的运算,与CPU打交道
集合关注的是数据的存储,与内存打交道
2.
①Stream 自己不会存储元素。
②Stream 不会改变源对象。相反,他们会返回一个持有结果的新Stream。
③Stream 操作是延迟执行的。这意味着他们会等到需要结果的时候才执行
3.Stream 执行流程
①Stream的实例化
②一系列的中间操作(过滤、映射、...)
③终止操作
4.说明:
①一个中间操作链,对数据源的数据进行处理
②一旦执行终止操作,就执行中间操作链,并产生结果。之后,不会再被使用
StreamAPITest.java
public class StreamAPITest {
@Test
public void test1 ( ) {
List< Employee> employees = EmployeeData. getEmployees ( ) ;
Stream< Employee> stream = employees. stream ( ) ;
Stream< Employee> parallelStream = employees. parallelStream ( ) ;
}
@Test
public void test2 ( ) {
int [ ] arr = new int [ ] { 1 , 2 , 3 , 4 , 5 , 6 } ;
IntStream stream = Arrays. stream ( arr) ;
Employee e1 = new Employee ( 1001 , "Tom" ) ;
Employee e2 = new Employee ( 1002 , "Jerry" ) ;
Employee[ ] arr1 = new Employee [ ] { e1, e2} ;
Stream< Employee> stream1 = Arrays. stream ( arr1) ;
}
@Test
public void test3 ( ) {
Stream< Integer> stream = Stream. of ( 1 , 2 , 3 , 4 , 5 , 6 ) ;
}
@Test
public void test4 ( ) {
Stream. iterate ( 0 , t - > t + 2 ) . limit ( 10 ) . forEach ( System. out: : println) ;
Stream. generate ( Math: : random) . limit ( 10 ) . forEach ( System. out: : println) ;
}
}
测试Stream的中间操作
StreamAPITest1.java
public class StreamAPITest1 {
@Test
public void test1 ( ) {
List< Employee> list = EmployeeData. getEmployees ( ) ;
Stream< Employee> stream = list. stream ( ) ;
stream. filter ( e - > e. getSalary ( ) > 7000 ) . forEach ( System. out: : println) ;
System. out. println ( ) ;
list. stream ( ) . limit ( 3 ) . forEach ( System. out: : println) ;
System. out. println ( ) ;
list. stream ( ) . skip ( 3 ) . forEach ( System. out: : println) ;
System. out. println ( ) ;
list. add ( new Employee ( 1010 , "刘强东" , 40 , 8000 ) ) ;
list. add ( new Employee ( 1010 , "刘强东" , 41 , 8000 ) ) ;
list. add ( new Employee ( 1010 , "刘强东" , 40 , 8000 ) ) ;
list. add ( new Employee ( 1010 , "刘强东" , 40 , 8000 ) ) ;
list. add ( new Employee ( 1010 , "刘强东" , 40 , 8000 ) ) ;
list. stream ( ) . distinct ( ) . forEach ( System. out: : println) ;
}
@Test
public void test2 ( ) {
List< String> list = Arrays. asList ( "aa" , "bb" , "cc" , "dd" ) ;
list. stream ( ) . map ( str - > str. toUpperCase ( ) ) . forEach ( System. out: : println) ;
List< Employee> employees = EmployeeData. getEmployees ( ) ;
Stream< String> namesStream = employees. stream ( ) . map ( Employee: : getName) ;
namesStream. filter ( name - > name. length ( ) > 3 ) . forEach ( System. out: : println) ;
System. out. println ( ) ;
Stream< Stream< Character> > streamStream = list. stream ( ) . map ( StreamAPITest1: : fromStringToStream) ;
streamStream. forEach ( s - > {
s. forEach ( System. out: : println) ;
} ) ;
System. out. println ( ) ;
Stream< Character> characterStream = list. stream ( ) . flatMap ( StreamAPITest1: : fromStringToStream) ;
characterStream. forEach ( System. out: : println) ;
}
public static Stream< Character> fromStringToStream ( String str) {
ArrayList< Character> list = new ArrayList < > ( ) ;
for ( Character c : str. toCharArray ( ) ) {
list. add ( c) ;
}
return list. stream ( ) ;
}
@Test
public void test3 ( ) {
List< Integer> list = Arrays. asList ( 12 , 43 , 65 , 34 , 87 , 0 , - 98 , 7 ) ;
list. stream ( ) . sorted ( ) . forEach ( System. out: : println) ;
List< Employee> employees = EmployeeData. getEmployees ( ) ;
employees. stream ( ) . sorted ( ( e1, e2) - > {
int ageValue = Integer. compare ( e1. getAge ( ) , e2. getAge ( ) ) ;
if ( ageValue != 0 ) {
return ageValue;
} else {
return - Double. compare ( e1. getSalary ( ) , e2. getSalary ( ) ) ;
}
} ) . forEach ( System. out: : println) ;
}
}
测试Stream的终止操作
StreamAPITest2.java
public class StreamAPITest2 {
@Test
public void test1 ( ) {
List< Employee> employees = EmployeeData. getEmployees ( ) ;
boolean allMatch = employees. stream ( ) . allMatch ( e - > e. getAge ( ) > 18 ) ;
System. out. println ( allMatch) ;
boolean anyMatch = employees. stream ( ) . anyMatch ( e - > e. getSalary ( ) > 10000 ) ;
System. out. println ( anyMatch) ;
boolean noneMatch = employees. stream ( ) . noneMatch ( e - > e. getName ( ) . startsWith ( "雷" ) ) ;
System. out. println ( noneMatch) ;
Optional< Employee> employee = employees. stream ( ) . findFirst ( ) ;
System. out. println ( employee) ;
Optional< Employee> employee1 = employees. parallelStream ( ) . findAny ( ) ;
System. out. println ( employee1) ;
}
@Test
public void test2 ( ) {
List< Employee> employees = EmployeeData. getEmployees ( ) ;
long count = employees. stream ( ) . filter ( e - > e. getSalary ( ) > 5000 ) . count ( ) ;
System. out. println ( count) ;
Stream< Double> salaryStream = employees. stream ( ) . map ( e - > e. getSalary ( ) ) ;
Optional< Double> maxSalary = salaryStream. max ( Double: : compare) ;
System. out. println ( maxSalary) ;
Optional< Employee> employee = employees. stream ( ) . min ( ( e1, e2) - > Double. compare ( e1. getSalary ( ) , e2. getSalary ( ) ) ) ;
System. out. println ( employee) ;
System. out. println ( ) ;
employees. stream ( ) . forEach ( System. out: : println) ;
System. out. println ( ) ;
employees. forEach ( System. out: : println) ;
}
@Test
public void test3 ( ) {
List< Integer> list = Arrays. asList ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ) ;
Integer sum = list. stream ( ) . reduce ( 0 , Integer: : sum) ;
System. out. println ( sum) ;
List< Employee> employees = EmployeeData. getEmployees ( ) ;
Stream< Double> salaryStream = employees. stream ( ) . map ( Employee: : getSalary) ;
Optional< Double> sumMoney = salaryStream. reduce ( ( d1, d2) - > d1 + d2) ;
System. out. println ( sumMoney. get ( ) ) ;
}
@Test
public void test4 ( ) {
List< Employee> employees = EmployeeData. getEmployees ( ) ;
List< Employee> employeeList = employees. stream ( ) . filter ( e - > e. getSalary ( ) > 6000 ) . collect ( Collectors. toList ( ) ) ;
employeeList. forEach ( System. out: : println) ;
System. out. println ( ) ;
Set< Employee> employeeSet = employees. stream ( ) . filter ( e - > e. getSalary ( ) > 6000 ) . collect ( Collectors. toSet ( ) ) ;
employeeSet. forEach ( System. out: : println) ;
}
}
3.Optional类
Optional类:为了在程序中避免出现空指针异常而创建的。
常用的方法:ofNullable(T t)
orElse(T t)
Girl.java
public class Girl {
private String name;
@Override
public String toString ( ) {
return "Girl{" + "name='" + name + '\'' + '}' ;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public Girl ( ) {
}
public Girl ( String name) {
this . name = name;
}
}
Boy.java
public class Boy {
private Girl girl;
@Override
public String toString ( ) {
return "Boy{" + "girl=" + girl + '}' ;
}
public Girl getGirl ( ) {
return girl;
}
public void setGirl ( Girl girl) {
this . girl = girl;
}
public Boy ( ) {
}
public Boy ( Girl girl) {
this . girl = girl;
}
}
OptionalTest.java
public class OptionalTest {
@Test
public void test1 ( ) {
Girl girl = new Girl ( ) ;
Optional< Girl> optionalGirl = Optional. of ( girl) ;
}
@Test
public void test2 ( ) {
Girl girl = new Girl ( ) ;
Optional< Girl> optionalGirl = Optional. ofNullable ( girl) ;
System. out. println ( optionalGirl) ;
Girl girl1 = optionalGirl. orElse ( new Girl ( "赵丽颖" ) ) ;
System. out. println ( girl1) ;
}
public String getGirlName ( Boy boy) {
return boy. getGirl ( ) . getName ( ) ;
}
@Test
public void test3 ( ) {
Boy boy = new Boy ( ) ;
boy = null;
String girlName = getGirlName ( boy) ;
System. out. println ( girlName) ;
}
public String getGirlName1 ( Boy boy) {
if ( boy != null) {
Girl girl = boy. getGirl ( ) ;
if ( girl != null) {
return girl. getName ( ) ;
}
}
return null;
}
@Test
public void test4 ( ) {
Boy boy = new Boy ( ) ;
boy = null;
String girlName = getGirlName1 ( boy) ;
System. out. println ( girlName) ;
}
public String getGirlName2 ( Boy boy) {
Optional< Boy> boyOptional = Optional. ofNullable ( boy) ;
Boy boy1 = boyOptional. orElse ( new Boy ( new Girl ( "迪丽热巴" ) ) ) ;
Girl girl = boy1. getGirl ( ) ;
Optional< Girl> girlOptional = Optional. ofNullable ( girl) ;
Girl girl1 = girlOptional. orElse ( new Girl ( "古力娜扎" ) ) ;
return girl1. getName ( ) ;
}
@Test
public void test5 ( ) {
Boy boy1 = null;
Boy boy2 = new Boy ( ) ;
Boy boy3 = new Boy ( new Girl ( "苍老师" ) ) ;
String girlName1 = getGirlName2 ( boy1) ;
String girlName2 = getGirlName2 ( boy2) ;
String girlName3 = getGirlName2 ( boy3) ;
System. out. println ( girlName1) ;
System. out. println ( girlName2) ;
System. out. println ( girlName3) ;
}
}
二、Java9新特性
1.JDK 和 JRE 目录结构的改变
2.模块化系统: Jigsaw -> Modularity
3.Java的REPL工具:jShell命令
4.语法改进:接口的私有方法
MyInterface.java
public interface MyInterface {
void methodAbstract ( ) ;
static void methodStatic ( ) {
System. out. println ( "我是接口中的静态方法" ) ;
}
default void methodDefault ( ) {
System. out. println ( "我是接口中的默认方法" ) ;
methodPrivate ( ) ;
}
private void methodPrivate ( ) {
System. out. println ( "我是接口中的私有方法" ) ;
}
}
MyInterfaceImpl.java
public class MyInterfaceImpl implements MyInterface {
@Override
public void methodAbstract ( ) {
}
@Override
public void methodDefault ( ) {
System. out. println ( "实现类重写了接口中的默认方法" ) ;
}
public static void main ( String[ ] args) {
MyInterface. methodStatic ( ) ;
MyInterfaceImpl impl = new MyInterfaceImpl ( ) ;
impl. methodDefault ( ) ;
}
}
5.语法改进:钻石操作符使用升级
@Test
@Test
public void test ( ) {
Comparator< Object> com = new Comparator < > ( ) {
@Override
public int compare ( Object o1, Object o2) {
return 0 ;
}
} ;
ArrayList< String> list = new ArrayList < > ( ) ;
}
6.语法改进:try语句
main()
public static void main ( String[ ] args) {
InputStreamReader reader = new InputStreamReader ( System. in) ;
try ( reader) {
char [ ] cbuf = new char [ 20 ] ;
int len;
if ( ( len = reader. read ( cbuf) ) != - 1 ) {
String str = new String ( cbuf, 0 , len) ;
System. out. println ( str) ;
}
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
7.String存储结构变更
动机:
String类的当前实现将字符存储在char数组中,每个字符使用两个字节(16位)。
从许多不同应用程序收集的数据表明,字符串是堆使用的一个主要组件,而且,
大多数字符串对象只包含Latin-1字符。这样的字符只需要一个字节的存储,
因此这样的字符串对象的内部char数组中有一半的空间没有使用。
描述:
我们建议将String类的内部表示形式从UTF-16 char数组更改为字节数组加上编码标志字段。
新的String类将根据字符串的内容存储编码为ISO-8859-1/Latin-1(每个字符一个字节)
或UTF-16(每个字符两个字节)的字符。编码标志将指示使用哪种编码。
8.集合工厂方法:快速创建只读集合
@Test
@Test
public void test1 ( ) {
List< String> namesList = new ArrayList < > ( ) ;
namesList. add ( "Joe" ) ;
namesList. add ( "Bob" ) ;
namesList. add ( "Bill" ) ;
namesList = Collections. unmodifiableList ( namesList) ;
namesList. add ( "Tom" ) ;
System. out. println ( namesList) ;
}
@Test
public void test2 ( ) {
List< String> list = Collections. unmodifiableList ( Arrays. asList ( "a" , "b" , "c" ) ) ;
Set< String> set = Collections. unmodifiableSet ( new HashSet < > ( Arrays. asList ( "a" , "b" , "c" ) ) ) ;
Map< String, Integer> map = Collections. unmodifiableMap ( new HashMap < > ( ) {
{
put ( "a" , 1 ) ;
put ( "b" , 2 ) ;
put ( "c" , 3 ) ;
}
} ) ;
map. forEach ( ( k, v) - > System. out. println ( k + ":" + v) ) ;
}
@Test
public void test3 ( ) {
List< Integer> list = Arrays. asList ( 1 , 2 , 3 , 4 , 5 ) ;
list. add ( 6 ) ;
}
@Test
public void test4 ( ) {
List< Integer> list1 = List. of ( 1 , 2 , 3 , 4 , 5 ) ;
System. out. println ( list1) ;
Set< Integer> set1 = Set. of ( 23 , 3 , 54 , 65 , 43 , 76 , 87 , 34 , 46 ) ;
System. out. println ( set1) ;
Map< String, Integer> map1 = Map. of ( "Tom" , 23 , "Jerry" , 54 , "HanMeimei" , 12 ) ;
System. out. println ( map1) ;
Map< String, Integer> map2 = Map. ofEntries ( Map. entry ( "Tom" , 34 ) , Map. entry ( "Jerry" , 21 ) ) ;
System. out. println ( map2) ;
}
9.InputStream 加强
@Test
@Test
public void test ( ) {
ClassLoader cl = this . getClass ( ) . getClassLoader ( ) ;
try ( InputStream is = cl. getResourceAsStream ( "hello.txt" ) ;
OutputStream os = new FileOutputStream ( "src\\hello1.txt" ) ) {
is. transferTo ( os) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
10.增强的 Stream API
@Test
@Test
public void test1 ( ) {
List< Integer> list = Arrays. asList ( 23 , 43 , 45 , 55 , 61 , 54 , 32 , 2 , 45 , 89 , 7 ) ;
list. stream ( ) . takeWhile ( x - > x < 60 ) . forEach ( System. out: : println) ;
System. out. println ( ) ;
list. stream ( ) . dropWhile ( x - > x < 60 ) . forEach ( System. out: : println) ;
}
@Test
public void test2 ( ) {
Stream< Integer> stream1 = Stream. of ( 1 , 2 , 3 , null) ;
stream1. forEach ( System. out: : println) ;
Integer i = 10 ;
i = null;
Stream< Integer> stream3 = Stream. ofNullable ( i) ;
long count = stream3. count ( ) ;
System. out. println ( count) ;
}
@Test
public void test3 ( ) {
Stream. iterate ( 0 , x - > x + 1 ) . limit ( 10 ) . forEach ( System. out: : println) ;
Stream. iterate ( 0 , x - > x < 10 , x - > x + 1 ) . forEach ( System. out: : println) ;
}
11.Optional获取Stream的方法
@Test
@Test
public void test4 ( ) {
List< String> list = new ArrayList < > ( ) ;
list. add ( "Tom" ) ;
list. add ( "Jerry" ) ;
list. add ( "Tim" ) ;
Optional< List< String> > optional = Optional. ofNullable ( list) ;
Stream< List< String> > stream = optional. stream ( ) ;
stream. flatMap ( x - > x. stream ( ) ) . forEach ( System. out: : println) ;
}
12.Javascript引擎升级:Nashorn
三、Java10新特性
1.局部变量类型推断
@Test
@Test
public void test1 ( ) {
var num = 10 ;
var list = new ArrayList < Integer> ( ) ;
list. add ( 123 ) ;
for ( var i : list) {
System. out. println ( i) ;
System. out. println ( i. getClass ( ) ) ;
}
for ( var i = 0 ; i < 5 ; i++ ) {
System. out. println ( i) ;
}
}
@Test
public void test2 ( ) {
int num = 123 ;
Supplier< Double> sup = ( ) - > Math. random ( ) ;
Consumer< String> con = System. out: : println;
int [ ] arr = { 1 , 2 , 3 , 4 } ;
}
@Test
public void test3 ( ) {
}
@Test
public void test4 ( ) {
try {
var url = new URL ( "http://www.atguigu.com" ) ;
var connection = url. openConnection ( ) ;
var reader = new BufferedReader ( new InputStreamReader ( connection. getInputStream ( ) ) ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
2.集合新增创建不可变集合的方法
@Test
@Test
public void test5 ( ) {
var list1 = List. of ( "Java" , "Python" , "C" ) ;
var copy1 = List. copyOf ( list1) ;
System. out. println ( list1 == copy1) ;
var list2 = new ArrayList < String> ( ) ;
list2. add ( "aaa" ) ;
var copy2 = List. copyOf ( list2) ;
System. out. println ( list2 == copy2) ;
}
四、Java11新特性
1.新增了一系列字符串处理方法
@Test
@Test
public void test1 ( ) {
System. out. println ( " \t \t \n " . isBlank ( ) ) ;
System. out. println ( "-----" + " \t abc \t \n " . strip ( ) + "-------" ) ;
System. out. println ( "-----" + " \t abc \t \n " . trim ( ) + "-------" ) ;
System. out. println ( "-----" + " \t abc \t \n " . stripTrailing ( ) + "-------" ) ;
System. out. println ( "-----" + " \t abc \t \n " . stripLeading ( ) + "-------" ) ;
String str1 = "abc" ;
String str2 = str1. repeat ( 5 ) ;
System. out. println ( str2) ;
String str3 = "abc\ndef\ng" ;
System. out. println ( str3. lines ( ) . count ( ) ) ;
}
2.Optional加强
@Test
@Test
public void test2 ( ) {
var op = Optional. empty ( ) ;
System. out. println ( op. isPresent ( ) ) ;
System. out. println ( op. isEmpty ( ) ) ;
op = Optional. of ( "abc" ) ;
var obj = op. orElseThrow ( ) ;
System. out. println ( obj) ;
Optional< String> op1 = Optional. of ( "hello" ) ;
Optional< Object> op2 = op. or ( ( ) - > op1) ;
System. out. println ( op2) ;
}
3.局部变量类型推断升级
@Test
@Test
public void test3 ( ) {
Consumer< String> con2 = ( @Deprecated var t) - > System. out. println ( t. toUpperCase ( ) ) ;
}
4.全新的HTTP客户端API
@Test
@Test
public void test4 ( ) {
try {
HttpClient client = HttpClient. newHttpClient ( ) ;
HttpRequest request = HttpRequest. newBuilder ( URI. create ( "http://127.0.0.1:8080/test/" ) ) . build ( ) ;
HttpResponse. BodyHandler< String> responseBodyHandler = HttpResponse. BodyHandlers. ofString ( ) ;
HttpResponse< String> response = client. send ( request, responseBodyHandler) ;
String body = response. body ( ) ;
System. out. println ( body) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
@Test
public void test5 ( ) {
HttpClient client = HttpClient. newHttpClient ( ) ;
HttpRequest request = HttpRequest. newBuilder ( URI. create ( "http://127.0.0.1:8080/test/" ) ) . build ( ) ;
HttpResponse. BodyHandler< String> responseBodyHandler = HttpResponse. BodyHandlers. ofString ( ) ;
CompletableFuture< HttpResponse< String> > sendAsync = client. sendAsync ( request, responseBodyHandler) ;
sendAsync. thenApply ( t - > t. body ( ) ) . thenAccept ( System. out: : println) ;
}
5.更简化的编译运行程序
6.废弃Nashorn引擎
7.ZGC
8.其它新特性