jdk1.8 十大特性
1.Lambda表达式
2.Stream函数式操作流元素集合
3.接口新增:默认方法与静态方法
4.方法引用,与Lambda表达式联合使用
5.引入重复注解
6.类型注解
7.最新的Date/Time API (JSR 310)
8.新增base64加解密API
9.数组并行(parallel)操作
10.JVM的PermGen空间被移除:取代它的是Metaspace(JEP 122)元空间
11、HashMap 底层数据结构的变化
java 8 之前,HashMap 底层的数据结构是数组+链表实现的, Java 8 之后是数组+链表+红黑树实现的,当链表的长度超过 8 之后,会转换成红黑树。
作用:解决因哈希冲突导致的链表过长,查询效率低的问题。
public class JDK8_features {
public List< Integer> list = Lists. newArrayList ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ) ;
@Test
public void testLambda ( ) {
list. forEach ( System. out: : println) ;
list. forEach ( e - > System. out. println ( "方式二:" + e) ) ;
}
@Test
public void testStream ( ) {
List< Integer> nums = Lists. newArrayList ( 1 , 1 , null, 2 , 3 , 4 , null, 5 , 6 , 7 , 8 , 9 , 10 ) ;
System. out. println ( "求和:" + nums
. stream ( )
. filter ( team - > team!= null)
. distinct ( )
. mapToInt ( num- > num* 2 )
. skip ( 2 )
. limit ( 4 )
. peek ( System. out: : println)
. sum ( ) ) ;
}
@Test
public void testDefaultFunctionInterface ( ) {
JDK8Interface1. staticMethod ( ) ;
new JDK8InterfaceImpl1 ( ) . defaultMethod ( ) ;
new JDK8InterfaceImpl2 ( ) . defaultMethod ( ) ;
}
public class JDK8InterfaceImpl1 implements JDK8Interface1 {
}
public class JDK8InterfaceImpl2 implements JDK8Interface1 , JDK8Interface2 {
@Override
public void defaultMethod ( ) {
JDK8Interface1. super . defaultMethod ( ) ;
System. out. println ( "实现类复写重名默认方法!!!!" ) ;
}
}
@Test
public void testMethodReference ( ) {
final Car car = Car. create ( Car: : new ) ;
final List< Car > cars = Arrays. asList ( car ) ;
cars. forEach ( Car: : collide ) ;
cars. forEach ( Car: : repair ) ;
final Car police = Car. create ( Car: : new ) ;
cars. forEach ( police: : follow ) ;
}
public static class Car {
public static Car create ( final Supplier< Car > supplier ) {
return supplier. get ( ) ;
}
public static void collide ( final Car car ) {
System. out. println ( "静态方法引用 " + car. toString ( ) ) ;
}
public void repair ( ) {
System. out. println ( "任意对象的方法引用 " + this . toString ( ) ) ;
}
public void follow ( final Car car ) {
System. out. println ( "特定对象的方法引用 " + car. toString ( ) ) ;
}
}
@Test
public void RepeatingAnnotations ( ) {
RepeatingAnnotations. main ( null) ;
}
@Test
public void ElementType ( ) {
Annotations. main ( null) ;
}
@Test
public void DateTime ( ) {
final Clock clock = Clock. systemUTC ( ) ;
System. out. println ( clock. instant ( ) ) ;
System. out. println ( clock. millis ( ) ) ;
final LocalDate date = LocalDate. now ( ) ;
final LocalDate dateFromClock = LocalDate. now ( clock ) ;
System. out. println ( date ) ;
System. out. println ( dateFromClock ) ;
final LocalTime time = LocalTime. now ( ) ;
final LocalTime timeFromClock = LocalTime. now ( clock ) ;
System. out. println ( time ) ;
System. out. println ( timeFromClock ) ;
final LocalDateTime datetime = LocalDateTime. now ( ) ;
final LocalDateTime datetimeFromClock = LocalDateTime. now ( clock ) ;
System. out. println ( datetime ) ;
System. out. println ( datetimeFromClock ) ;
final ZonedDateTime zonedDatetime = ZonedDateTime. now ( ) ;
final ZonedDateTime zonedDatetimeFromClock = ZonedDateTime. now ( clock ) ;
final ZonedDateTime zonedDatetimeFromZone = ZonedDateTime. now ( ZoneId. of ( "America/Los_Angeles" ) ) ;
System. out. println ( zonedDatetime ) ;
System. out. println ( zonedDatetimeFromClock ) ;
System. out. println ( zonedDatetimeFromZone ) ;
final LocalDateTime from = LocalDateTime. of ( 2014 , Month. APRIL, 16 , 0 , 0 , 0 ) ;
final LocalDateTime to = LocalDateTime. of ( 2015 , Month. APRIL, 16 , 23 , 59 , 59 ) ;
final Duration duration = Duration. between ( from, to ) ;
System. out. println ( "Duration in days: " + duration. toDays ( ) ) ;
System. out. println ( "Duration in hours: " + duration. toHours ( ) ) ;
}
@Test
public void testBase64 ( ) {
final String text = "就是要测试加解密!!abjdkhdkuasu!!@@@@" ;
String encoded = Base64. getEncoder ( )
. encodeToString ( text. getBytes ( StandardCharsets. UTF_8 ) ) ;
System. out. println ( "加密后=" + encoded ) ;
final String decoded = new String (
Base64. getDecoder ( ) . decode ( encoded ) ,
StandardCharsets. UTF_8 ) ;
System. out. println ( "解密后=" + decoded ) ;
}
@Test
public void testParallel ( ) {
long [ ] arrayOfLong = new long [ 20000 ] ;
Arrays. parallelSetAll ( arrayOfLong,
index - > ThreadLocalRandom. current ( ) . nextInt ( 1000000 ) ) ;
Arrays. stream ( arrayOfLong ) . limit ( 10 ) . forEach (
i - > System. out. print ( i + " " ) ) ;
System. out. println ( ) ;
Arrays. parallelSort ( arrayOfLong ) ;
Arrays. stream ( arrayOfLong ) . limit ( 10 ) . forEach (
i - > System. out. print ( i + " " ) ) ;
System. out. println ( ) ;
}
@Test
public void testMetaspace ( ) {
}
}
引用到的相关类:
public interface JDK8Interface1 {
public static void staticMethod ( ) {
System. out. println ( "接口中的静态方法" ) ;
}
public default void defaultMethod ( ) {
System. out. println ( "接口中的默认方法" ) ;
}
}
public interface JDK8Interface2 {
public static void staticMethod ( ) {
System. out. println ( "接口中的静态方法" ) ;
}
public default void defaultMethod ( ) {
System. out. println ( "接口中的默认方法" ) ;
}
}
public class RepeatingAnnotations {
@Target ( ElementType. TYPE )
@Retention ( RetentionPolicy. RUNTIME )
public @interface Filters {
Filter[ ] value ( ) ;
}
@Target ( ElementType. TYPE )
@Retention ( RetentionPolicy. RUNTIME )
@Repeatable ( Filters. class )
public @interface Filter {
String value ( ) ;
String value2 ( ) ;
} ;
@Filter ( value= "filter1" , value2= "111" )
@Filter ( value= "filter2" , value2= "222" )
public interface Filterable {
}
public static void main ( String[ ] args) {
for ( Filter filter: Filterable. class . getAnnotationsByType ( Filter. class ) ) {
System. out. println ( filter. value ( ) + filter. value2 ( ) ) ;
}
}
}
public class Annotations {
@Retention ( RetentionPolicy. RUNTIME )
@Target ( { ElementType. TYPE_USE, ElementType. TYPE_PARAMETER } )
public @interface NonEmpty {
}
public static class Holder < @NonEmpty T > extends @NonEmpty Object {
public void method ( ) throws @NonEmpty Exception {
}
}
public static void main ( String[ ] args) {
final Holder< String > holder = new @NonEmpty Holder< String > ( ) ;
@NonEmpty Collection< @NonEmpty String > strings = new ArrayList < > ( ) ;
}
}
```java
## Java 9 新特性
1. 模块系统
模块是一个包的容器,Java 9 最大的变化之一是引入模块系统。(Jigsaw 项目)
作用:解决大型项目模块化开发的需求。
2. 支持 HTTP/ 2 标准
HTTP/ 2 标准是 HTTP 协议的最新版本,新的 HTTPClient API 支持 Websocket 和 HTTP2 流以及服务器推送特性。
深入理解http1. x、http 2 和https:https: / / segmentfault. com/ a/ 1190000015316332
3. 提供创建不可变集合的静态工厂方法
List、Set、Map 接口中,提供新的静态工厂方法直接创建不可变的集合实例。
作用:创建不可变集合更方便,一行代码就搞定,节省了开销。
4. 私有接口方法
在接口中也允许编写 private 修饰的私有方法了。
作用:增强了接口的功能,提高了可扩展性。
5. 轻量级的 JSON API
内置了一个轻量级的 JSON API。
6. 引入响应式流 API
Java 9 引入了新的响应式流 API。
## Java11的新特性
1 、本地变量类型推断
什么是局部变量类型推断?
var javastack = "javastack" ;
System. out. println ( javastack) ;
大家看出来了,局部变量类型推断就是左边的类型直接使用 var 定义,而不用写具体的类型,编译器能根据右边的表达式自动推断类型,如上面的 String 。
var javastack = "javastack" ;
就等于:
String javastack = "javastack" ;
2 、字符串加强
Java 11 增加了一系列的字符串处理方法,如以下所示。
" " . isBlank ( ) ;
" Javastack " . strip ( ) ;
" Javastack " . stripTrailing ( ) ;
" Javastack " . stripLeading ( ) ;
"Java" . repeat ( 3 ) ;
"A\nB\nC" . lines ( ) . count ( ) ;
3 、集合加强
自 Java 9 开始,Jdk 里面为集合(List/ Set/ Map)都添加了 of 和 copyOf 方法,它们两个都用来创建不可变的集合。
4 、Stream 加强
Stream 是 Java 8 中的新特性,Java 9 开始对 Stream 增加了以下3 个新方法。
1 ) 增加单个参数构造方法,
2 ) 增加 takeWhile 和 dropWhile 方法
3 )iterate重载
这个 iterate 方法的新重载方法,可以让你提供一个 Predicate ( 判断条件) 来指定什么时候结束迭代。
5 、Optional 加强
Opthonal 也增加了几个非常酷的方法,现在可以很方便的将一个 Optional 转换成一个 Stream, 或者当一个空 Optional 时给它一个替代的。
6 、InputStream 加强
InputStream 终于有了一个非常有用的方法:transferTo,可以用来将数据直接传输到 OutputStream,这是在处理原始数据流时非常常见的一种用法。
7 、HTTP Client API
这是 Java 9 开始引入的一个处理 HTTP 请求的的孵化 HTTP Client API,该 API 支持同步和异步,而在 Java 11 中已经为正式可用状态,你可以在 java. net 包中找到这个 API。
8 、化繁为简,一个命令编译运行源代码
在我们的认知里面,要运行一个 Java 源代码必须先编译,再运行,两步执行动作。而在Java 11 版本中,通过一个 java 命令就直接搞定了。