Java- Updates in java8-Mazenet solution

36
Updates in jdk 1.8 By Sharmilee 9894303344 Java Trainer Mazenet Solution

Transcript of Java- Updates in java8-Mazenet solution

Page 1: Java- Updates in java8-Mazenet solution

Updates in jdk 1.8

BySharmilee

9894303344Java Trainer

Mazenet Solution

Page 2: Java- Updates in java8-Mazenet solution

Objectives

• What’s new with JAVA -8• What’s been modified with JAVA – 8• What’s gone

Page 3: Java- Updates in java8-Mazenet solution

What’s new with JAVA -8

Page 4: Java- Updates in java8-Mazenet solution

1. Lambda Expression2. Method references3. Functional Interface4. Default method5. Optional6. Parallel sort7. Calender.Builder

Page 5: Java- Updates in java8-Mazenet solution

Sorting in Java 8

Page 6: Java- Updates in java8-Mazenet solution

private void sortUsingJava7(List<String> names) { Collections.sort(names, new Comparator<String>() { @Override public int compare(String s1, String s2) { return s1.compareTo(s2); } }); }

private void sortUsingJava8(List<String> names) { Collections.sort(names, (s1, s2) -> s1.compareTo(s2)); }

Page 7: Java- Updates in java8-Mazenet solution

1) Lambda Introduction (new to jdk 1.8)

Page 8: Java- Updates in java8-Mazenet solution

What is lambda?

• Lambda expression facilitates functional programming, and simplifies the development a lot

Page 9: Java- Updates in java8-Mazenet solution

Syntax of lambda expression

• “ -> ” is known as lambda expression.

parameter -> expression body

Page 10: Java- Updates in java8-Mazenet solution

Characteristics of Lambda expression

• Optional type declaration• Optional parenthesis around parameter• Optional curly braces• Optional return keyword

Page 11: Java- Updates in java8-Mazenet solution

2) Method References (new to jdk 1.8)

Page 12: Java- Updates in java8-Mazenet solution

Method References

• It help to point to methods by their names. • A method reference is described

using :: (double colon) symbol. • A method reference can be used to point the

following types of methods −Static methodsInstance methodsConstructors using new operator (TreeSet::new)

Page 13: Java- Updates in java8-Mazenet solution

Types of method referenceType Example Syntax

1. Reference to a static method ContainingClass::staticMethodName Class::staticMethodName

2. Reference to a constructor ClassName::new ClassName::new

3. Reference to an instance method of an arbitrary object of a particular type

ContainingType::methodName Class::instanceMethodName

4. Reference to an instance method of a particular object containingObject::instanceMethodName object::instanceMethodName

Page 14: Java- Updates in java8-Mazenet solution

You can call the methods with their names (references) with :: operator.

Arrays.sort(names, Test::matchStringLength);

Page 15: Java- Updates in java8-Mazenet solution

3) Functional Interfaces (new to jdk 1.8)

Page 16: Java- Updates in java8-Mazenet solution

• Functional Interface have a single functionality to exhibit.

• For example, a Comparable interface with a single method ‘compareTo’ is used for comparison purpose.

• Java 8 has defined a lot of functional interfaces to be used extensively in lambda expressions.

Page 17: Java- Updates in java8-Mazenet solution

4) Default Methods (new to jdk 1.8)

Page 18: Java- Updates in java8-Mazenet solution

• Java 8 introduces default method so that List/Collection interface can have a default implementation of forEach method,

• The class implementing these interfaces need not implement the same.

Page 19: Java- Updates in java8-Mazenet solution

Syntax

public interface vehicle { default void print(){ System.out.println("I am a vehicle!"); } }

Page 20: Java- Updates in java8-Mazenet solution

Multiple Defaults

• With default functions in interfaces, there is a possibility that a class is implementing two interfaces with same default methods.

Page 21: Java- Updates in java8-Mazenet solution

public interface vehicle { default void print(){System.out.println("I am a vehicle!");} } public interface fourWheeler { default void print(){ System.out.println("I am a four wheeler!"); } }

Page 22: Java- Updates in java8-Mazenet solution

5) Optional (new to jdk 1.8)

Page 23: Java- Updates in java8-Mazenet solution

• Optional is a container object which is used to contain not-null objects.

• Optional object is used to represent null with absent value.

• This class has various utility methods to facilitate code to handle values as ‘available’ or ‘not available’ instead of checking null values.

Page 24: Java- Updates in java8-Mazenet solution

Class Declaration for java.util.Optional<T>

public final class Optional<T> extends Object

Page 25: Java- Updates in java8-Mazenet solution

6) Parallel Sort (new to jdk 1.8)

Page 26: Java- Updates in java8-Mazenet solution

Parallel sort

• Arrays#parallelSort uses Fork/Join framework introduced in Java 7 to assign the sorting tasks to multiple threads available in the thread pool.

Page 27: Java- Updates in java8-Mazenet solution

Difference between Arrays.sort & Arrays.ParallelSort

1. Arrays.sort() : is a sequential sorting.The API uses single thread for the operation.The API takes bit longer time to perform the

operation.2. Arrays.ParallelSort() : is a parallel sorting.

The API uses multiple threads.The API takes lesser the time compared to Sort().

Page 28: Java- Updates in java8-Mazenet solution

Why Parallel Sort?

• Both sort() and parallelSort() sorts the array.

• The performance with parallelSort() can be seen when the number of arrays to sort are very many.

Page 29: Java- Updates in java8-Mazenet solution

7) Addition of Calender.Builder (new to jdk 1.8)

Page 30: Java- Updates in java8-Mazenet solution

• Before JDK 1.8, each date field is set separately with individual methods.

• Each set method added as a separate statement.

Page 31: Java- Updates in java8-Mazenet solution

Calendar.Builder in jdk 1.8

Calendar calendar1 = new Calendar.Builder() .set(Calendar.YEAR, 2013) .set(Calendar.MONTH, 3) .set(Calendar.DATE, 10) .set(Calendar.HOUR, 8) .set(Calendar.MINUTE, 56) .set(Calendar.SECOND, 14) .build();

Page 32: Java- Updates in java8-Mazenet solution

• In JDK 1.8, Calendar.Builder is used to instantiate calendar1 instance and all set methods are used as a single statement.

• Semicolon is given only one after build() method.

Page 33: Java- Updates in java8-Mazenet solution

public static void DemoCalendarWithSingleSet() { final Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone(timeZoneId), ENGLISH); calendar.set(2013, APRIL, 6, 15, 45, 22); out.println("Calendar via Constructor: " + stringifyCalendar(calendar)); }

Page 34: Java- Updates in java8-Mazenet solution

Other new features

• Streams• New Data/Time API• Nashorn JavaScript• Base 64

Page 35: Java- Updates in java8-Mazenet solution

What’s gone ??• Javax.swing.ImageIcon.Componentcomponent@Deprecated • protected static final Component component• Deprecated. since 1.8• Do not use this shared component, which is used to track image

loading. It is left for backward compatibility only.tracker

@Deprecated • protected static final MediaTracker tracker• Deprecated. since 1.8• Do not use this shared media tracker, which is used to load images. It

is left for backward compatibility only.

Page 36: Java- Updates in java8-Mazenet solution

Thank you!