JAVA SE 8 (March 18, 2014)

 Preface


    Its codename was Spider. Although, codenames have been discontinued, but codename Spider is common among java developer. It includes some feature which proposed for java se 7 but added in java se 8.


The features introduce in java 8


Lambda Expression

Functional Interface

Default method

Stream

Date/Time API

Repeat Annotation

Run javascript on JVM

JavaFx was Lauched


Lambda expression


Lambda expression , you can create function without belong to any class. Java lambda expression are momonly used to implement simple event/callbacks, or functional program with StreamAPI.

1 Lambda expression can have zero, one or more parameters.

2 The type of parameter can be explicitly declared or it can be inferred from the context (scene).

3  if function single line is () -> return a

4 The body of lambda is emple statment or more line

5 if function is more line use: () -> { statement; statment2; }

Scope Use variable: Using variable in Lambda express must variables:


Final varibal: Variable not change content when it was created.

Static Golbal variable: Variable of Class which is used by Class. Variable can call in anywhere in class.

Golbal variable: Variable can call in anywhere in class.

Local variable: is yes but implictly final, when it not change after it created view example

 

image 1 demo variabe lambda




Functional Interface

    Simple abstract method interfaces (SAM interfaces) is not a new concept. it mean interface only single method abstract method. It create it you must add annotation @FunctionalInterface in above a interface, default method that free adds more function but you not OverUse method default similar on two functionalInterface that class implements two functionalInterfaces it error apearance. Default method maybe implements but same name method will apearance error ( Duplicate default methods named defaultMethod with the parameters () and () are inherited from the types Interface_Two and Interface_One )

 

image create functional interface


Use Functinal Interface




Buil-in Functional interface in java

 

Function<T,R>

public interface Function<T,R> {
    public <R> apply(T parameter);
}

Take parameter type T, and return value of type R. You can implement Function or use direct.

Example use direct:

Function<Integer, Integer> sum = (value) -> value + 1;
Integer resultLambda =  sum.apply(1);
System.out.println("resultLambda = " + resultLambda);

Example use implement interface:

Function<Integer, Integer> sum = new ClassImplementFunction(); 

//ClassImplementFunction @Override apply() function
Integer resultLambda =  sum.apply(1);
System.out.println("resultLambda = " + resultLambda);

 

 Predicate<T>

public interface Predicate {
    boolean test(T t);
}
 

Take parameter type T and return value true or false. You can use direct or implement same with Function<T,R>. Exmple check not null.

Predicate<Integer> isNotNull= (value) -> value != null;

isNotNull.test(1000);


UnaryOperator<T>

Represent an operation which take a single parameter and returns a value same type with parameter. Take a specific object as parameter then modifies that object and returns it again.You can use direct or implement same with Function<T,R>.

UnaryOperator<Point> unaryOperator = (point) -> { point.x= 1; point.y= 1 return point; };
unaryOperator.apply(objectPoint);

 

BinaryOperator<T> extends BiFunction<T,T,T> 

Represent an operation which takes two parameters and return a single value. Both parameters type and the return type must be of the same type. Useful function that sum, subtract, divide, multiply. This use in Create collectors in function combiner() merge object in container in parallel process. Example use bacsic. You can use direct or implement same with Function<T,R>

BinaryOperator<Point> binaryOperator = (point1, point2) -> { point1.add(point2); return point1; };

binaryOperator.apply(objectPoint1, object2Point2)


image combiner()


Supplier<T>

 That supplies a value, create value for some do thing. It use in create customize Collectors supplier(), Function supplier() create object for function accumulator() in processing Collectors.

 

image supplier()


 

Supplier<List<String>> supplier = () -> return ArrayList::new;

Supplier<Integer> sss2 = () -> {return Integer.valueOf(0);};

sss2.get();


Consumer<T>

that consumer a value without return any value. A java consumer implementation could be printing value, or writing a file orver network ...

Consumer<Integer> consumer = (value) -> System.out.println(value);

consumer.accept(value);


Default Method

yes it is a keyword default method() use it in interface (interface or functional interface). if you not @Override them which will be invoked by caller classes. You can not implement two interface has same default method() it error complier. so advoid OverUse default method in more interfaces. 


image default method


Stream API

You can refer: 

https://sharecodefull.blogspot.com/2020/12/stream-api-and-collectors-se8.html



Date/Time API

 

Date class has even become obsolete . The new class such as LocalDate, LocalTime, LocalDateTime. 

LocalDate is represents a date, no represent of a time or time-zon. 

LocalTime is repersents a time, no represent of a date or time-zon

LocalDateTiem a represents a date-time. no represent of a time-zon

If you use the date functionally with zone information can use new class with time-zon : OffsetDate, OffsetTime and OffsetDateTime.


You can view doccument refer: https://www.oracle.com/technical-resources/articles/java/jf14-date-time.html

 View source in my GitHub: 

https://github.com/nguyenthinhit996/sharefullcode/blob/master/Learn%20Java/NewFeatureSE8/src/featuresameple/dateandtimeapi/Dateandtimesample.java

 

Repeat Annotation

 

image repeat annotation





Run Js on Java JVM


image run js on JVM



JavaFX 

 JavaFx is a java library used to develop desktop application as well as rich internet. Javafx can run multi platform including web, mobile, desktop.

 JavaFx intended to replace swing java Java applications as a GUI framwork. Like swing Javafx also provides its own components and doest not depend upon the operation system.

Can you view more in: 

https://docs.oracle.com/javase/8/javafx/get-started-tutorial/jfx-overview.htm

 

 

Finish

 

    This section is non commercial mainly sharing and advance knowlage for java.This tutorials has referenced document from the list below if you has complain for license, i will remove all from internet. Thank you all everything.

http://tutorials.jenkov.com/java/lambda-expressions.html

https://howtodoinjava.com/java-8-tutorial/

http://tutorials.jenkov.com/java-functional-programming/functional-interfaces.html#functional-interfaces-can-be-implemented-by-a-lambda-expression

0 comments :

Post a Comment

Cancel Reply

About Me

My photo
Tân An, Long An, Vietnam
Hello everyone, I love programming, I love making friends with people all over the world.