Java 8 features

Java 8 features


Introduction of JDK 1.8 features.

  • Lambda Expression, a new Java 8 feature has been introduced. They enable you to treat functionality as a method argument, or code as data. Lambda expressions let you express instances of single-method interfaces (known as functional interfaces) more compactly.
  • Default methods enable new functionality to be added to the interfaces of libraries and ensure binary compatibility with code written for older versions of those interfaces.
  • Repeating Annotations provide the ability to apply the same annotation type more than once to the same declaration or type use.
  • Type Annotations provide the ability to apply an annotation anywhere a type is used, not just on a declaration. Used with a pluggable type system, this feature enables improved type checking of your code.
  • Method references provide easy-to-read lambda expressions for methods that already have a name.
  • Improved type interface.
  • Method parameter reflection.




  • Java Time API.

  • Functional Interface and Lambda Expression.

  • Collection API enhancement.

  • Concurrency API enhancement.

  • Default and Static method in Interface.

  • Miscellaneous Core API enhancement.

  • forEach() method in Iterable Interface.

  • Java stream API for bulk data operation on Collection.

  • Java IO enhancement.

  • Arrays Parallel Sort.


Java Time API

java 8 Date-time API is mainly come to finish the drawbacks of old java Date-time API.
  • Local -  this is simple and work on local time zone.
Example: 
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.LocalDateTime;
import java.time.Month;
/**
 * @author Suraj Pratap Singh
 */
public class Test {
   public static void main(String args[]) {
   Test test = new Test();
   test.demoLocalDateTime();
   }

   public void demoLocalDateTime() {
      // Local current date and time
      LocalDateTime currentTime = LocalDateTime.now();
      System.out.println(" Local Current DateTime: " + currentTime);

      LocalDate date1 = currentTime.toLocalDate();
      System.out.println("Local date1: " + date1);

      Month month = currentTime.getMonth();
      int day = currentTime.getDayOfMonth();
      int seconds = currentTime.getSecond();

      System.out.println("Month: " + month +"day: " + day +"seconds: " + seconds);

      LocalDateTime date2 = currentTime.withDayOfMonth(10).withYear(2012);
      System.out.println("Local date2: " + date2);

      //12 december 2014
      LocalDate date3 = LocalDate.of(2014, Month.DECEMBER, 12);
      System.out.println("Local date3: " + date3);

      //22 hour 15 minutes
      LocalTime date4 = LocalTime.of(22, 15);
      System.out.println("Local date4: " + date4);

      //parse a string
      LocalTime date5 = LocalTime.parse("20:15:30");
      System.out.println("Local date5: " + date5);
  
      }
}
Output:
Local Current DateTime: 2019-01-07T22:04:21.224
Local date1: 2019-01-07
Month: JANUARYday: 7seconds: 21
Local date2: 2012-01-10T22:04:21.224
Local date3: 2014-12-12
Local date4: 22:15
Local date5: 20:15:30


java.time.Month class methods and Constants

public final class java.time.Month extends java.lang.Enum<java.time.Month> implements java.time.temporal.TemporalAccessor, java.time.temporal.TemporalAdjuster {
  public static final java.time.Month JANUARY;
  public static final java.time.Month FEBRUARY;
  public static final java.time.Month MARCH;
  public static final java.time.Month APRIL;
  public static final java.time.Month MAY;
  public static final java.time.Month JUNE;
  public static final java.time.Month JULY;
  public static final java.time.Month AUGUST;
  public static final java.time.Month SEPTEMBER;
  public static final java.time.Month OCTOBER;
  public static final java.time.Month NOVEMBER;
  public static final java.time.Month DECEMBER;
  public static java.time.Month[] values();
  public static java.time.Month valueOf(java.lang.String);
  public static java.time.Month of(int);
  public static java.time.Month from(java.time.temporal.TemporalAccessor);
  public int getValue();
  public java.lang.String getDisplayName(java.time.format.TextStyle, java.util.Locale);
  public boolean isSupported(java.time.temporal.TemporalField);
  public java.time.temporal.ValueRange range(java.time.temporal.TemporalField);
  public int get(java.time.temporal.TemporalField);
  public long getLong(java.time.temporal.TemporalField);
  public java.time.Month plus(long);
  public java.time.Month minus(long);
  public int length(boolean);
  public int minLength();
  public int maxLength();
  public int firstDayOfYear(boolean);
  public java.time.Month firstMonthOfQuarter();
  public <R> R query(java.time.temporal.TemporalQuery<R>);
  public java.time.temporal.Temporal adjustInto(java.time.temporal.Temporal);
  static {};
}


  • Zoned - this is used to work with any timezone.
Methods and Constructor


public abstract class java.time.ZoneId implements java.io.Serializable {
  public static final java.util.Map<java.lang.String, java.lang.String> SHORT_IDS;
  public static java.time.ZoneId systemDefault();
  public static java.util.Set<java.lang.String> getAvailableZoneIds();
  public static java.time.ZoneId of(java.lang.String, java.util.Map<java.lang.String, java.lang.String>);
  public static java.time.ZoneId of(java.lang.String);
  public static java.time.ZoneId ofOffset(java.lang.String, java.time.ZoneOffset);
  static java.time.ZoneId of(java.lang.String, boolean);
  public static java.time.ZoneId from(java.time.temporal.TemporalAccessor);
  java.time.ZoneId();
  public abstract java.lang.String getId();
  public java.lang.String getDisplayName(java.time.format.TextStyle, java.util.Locale);
  public abstract java.time.zone.ZoneRules getRules();
  public java.time.ZoneId normalized();
  public boolean equals(java.lang.Object);
  public int hashCode();
  public java.lang.String toString();
  abstract void write(java.io.DataOutput) throws java.io.IOException;
  static {};
}



Example:

import java.time.ZoneId;
/**
 * 
 * @author Suraj Pratap Singh
 *
 */
public class Test{
   public static void main(String args[]) {
      Test test = new Test();
      test.testZonedDateTime();
   }
   public void testZonedDateTime() {    
      ZoneId currentZone = ZoneId.systemDefault();
      System.out.println("CurrentZone: " + currentZone);
   
      ZoneId id = ZoneId.of("Europe/Berlin");
      System.out.println("ZoneId: " + id);
   }
}

Output:
CurrentZone: Asia/Calcutta
ZoneId: Europe/Berlin

Parallel Sorting in Java

  • parallelSort(): In Java 8 a new feature 'parallelSort()' added in the Arrays class of java.util package. This method is introduced to support the parallel sorting of array elements.
The logic behind parallel sorting is:
  1. The given array is divided into the sub-arrays and the sub-arrays are further divided into their sub-arrays, this proceeds until the sub array reaches a minimum level of divisibility.
  2. The sub-arrays are sorted individually by multiple threads.
  3. The sorted sub-arrays are merged.

Advantages of parallelSort() method:

  • Parallel sort uses threading (each thread gets a chunk of the list and sorts it in parallel. Later these sorted chunks are merged into a result.
  • It's faster when there are a lot of elements in the collection. The overhead for parallelization becomes tolerably small on larger arrays, but it is large for smaller ones.
Example: 
import java.util.Arrays; 
  
public class ParallelSort { 
    public static void main(String[] args) 
    { 
        int numbers[] = { 23,53,54,26,73,64,21 }; 
       System.out.print("Normal Array: "); 
        Arrays.stream(numbers) 
            .forEach(n -> System.out.print(n + " ")); 
        System.out.println(); 
        Arrays.parallelSort(numbers); 
        System.out.print("Sorted Array: "); 
        Arrays.stream(numbers) 
            .forEach(n -> System.out.print(n + " ")); 
    } 

Read below posts.
Most Frequently Asked Java Interview Programs. PART-1
Most Frequently Asked Java Interview Programs PART-2

Comments

Popular Posts