java- updates in java8-mazenet solution

Post on 19-Jan-2017

77 Views

Category:

Education

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Updates in jdk 1.8

BySharmilee

9894303344Java Trainer

Mazenet Solution

Objectives

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

What’s new with JAVA -8

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

Sorting in Java 8

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)); }

1) Lambda Introduction (new to jdk 1.8)

What is lambda?

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

Syntax of lambda expression

• “ -> ” is known as lambda expression.

parameter -> expression body

Characteristics of Lambda expression

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

2) Method References (new to jdk 1.8)

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)

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

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

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

3) Functional Interfaces (new to jdk 1.8)

• 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.

4) Default Methods (new to jdk 1.8)

• 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.

Syntax

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

Multiple Defaults

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

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!"); } }

5) Optional (new to jdk 1.8)

• 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.

Class Declaration for java.util.Optional<T>

public final class Optional<T> extends Object

6) Parallel Sort (new to jdk 1.8)

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.

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().

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.

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

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

• Each set method added as a separate statement.

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();

• 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.

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)); }

Other new features

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

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.

Thank you!

top related