Let's talk about new features in Java 8 -12, let' start!
Java 8
- Functional Interfaces & Lambda Expressions
- Stream API ( this comes with filter, map, flatmap, reduce, collect methods) for Bulk operations
See here for details on these two.
- forEach() for Iterable implemented Collections
- Default & static Methods inside Interface
- Time API
- Files API
- Files.lines(path)
- Files.list(dir)
- BufferedReader.stream()
- Files.find()
===============================
Java 9
- Modularity
Java API itself has been modularised and you can now modularise your application too. This is good from Containerisation perspective etc. , thin modules with defined exposure/boundary.
Why Modularity:
- Have only what you need, dont need whole JDK
Use jlink ModuleFor example, use following in your java class :module jlinkModule {
requires java.logging;
}
This will have a custom JDK having logging
and default ones ( like Object, class & String)
- Less Space
- Public is also not visible across Modules, even using reflection so better encapsulation
- Better performance
- Better Security & maintainability
Module Types
Application modules, Automated, unnamed & platform modules.
- Jshell
- Use Jlink
- Search facility in JavaDoc :)
- Collection Factory Methods
- Stream API Improvements
dropWhile, takeWhile, ofNullable
- Private Interface methods
- Multi-release JARs
Here you can have 2 version of a class, one say for JDK 9 and another for previous versions.
===============================
Java 10
- Parallel Full GC for G1
- var Type & Local variable Type inference
===============================
Java 11
- Local parameter type inference for Lambda Variables using var (Also allowed @NonNull etc)
- Repeat Method in String to copy
- Path.of(path)
- Files.readString & Files.writeString()
- String.lines
- Optional.isEmpty
- Strip, StripLeading, StripTrailing
- Improved toArray
Employee[] employeeArray = employeeList.toArray(Employee[]::new)
===============================
Java 12
- Improved Switch with expression supported
switch(DAY){
case SUNDAY : return 0; break;
case MONDAY : return 1; break;
---
Now it is :
int day = Switch(DAY) {
case SUNDAY -> 0;
case MONDAY-> 1;
}
- Teeing Collector
To have a Average value of array, typical code could be :
Integer[] array = {6,2,3,4,5,1};
Double average = Arrays.stream(array).map(x->(Double.valueOf(x))).reduce
(0d,(x,y)-> (x + (y/array.length)),(a,b)->a +b);
System.out.println(average);
With Teeing, it is :
Double result = Stream.of(1,2,3,4,5,6).
collect(teeing(
summingDouble(x -> x),
counting(),
(sum, total) -> sum / total);
System.out.println(result); // 3.5
Integer[] array = {6,2,3,4,5,1};
Double average = Arrays.stream(array).map(x->(Double.valueOf(x))).reduce
(0d,(x,y)-> (x + (y/array.length)),(a,b)->a +b);
System.out.println(average);
With Teeing, it is :
Double result = Stream.of(1,2,3,4,5,6).
collect(teeing(
summingDouble(x -> x),
counting(),
(sum, total) -> sum / total);
System.out.println(result); // 3.5
- Improved low pause Shenandoah GC algorithm ( pause time independent of Heap Size)
- Improved G1 (Return unused memory immediately)
No comments:
Post a Comment