Tuesday 26 September 2017

Enum ordinal() method

Java.lang.Enum.ordinal() Method

The java.lang.Enum.ordinal() method returns the ordinal of this enumeration constant (its position in its enum declaration, where the initial constant is assigned an ordinal of zero).

Declaration
Following is the declaration for java.lang.Enum.ordinal() method

public final int ordinal()

Parameters
NA

Return Value
This method returns the ordinal of this enumeration constant.

Exception
NA

Example
The following example shows the usage of java.lang.Enum.ordinal() method.

enum Country{
   India(9), Nepal(8),Shrilanka(5);
   int gdp;
   Mobile(int gdp) {
      this.gdp= gdp;
   }
   int getGdp() {
      return gdp;
   } 
}

public class EnumDemo {

   public static void main(String args[]) {
      System.out.println("CellPhone List:");
      for(Country c : Country.values()) {
         System.out.println("Gdp of"+c+ "is"+ c.getGdp() );
      }
      Country c = Country.India;
      System.out.println("The ordinal is = " + c.ordinal());
      System.out.println("Country= " + c.name());                      
   }
}

Default and Static Methods in Interface

Default and Static Methods in Interface!

Before Java 8, interfaces could have only public abstract methods. It was not possible to add new functionality to the existing interface without forcing all implementing classes to create an implementation of the new methods, nor it was possible to create interface methods with an implementation.

Starting with Java 8, interfaces can have static and default methods that, despite being declared in an interface, have a defined behavior.

Static Method

Consider the following method of the interface (let’s call this interface Person):

static String nationality() {
    return "Indian";
}

The static nationality() method is available only through and inside of an interface. It can’t be overridden by an implementing class.

To call it outside the interface the standard approach for static method call should be used:

String nationality = Person.nationality();

Default Method
Default methods are declared using the new default keyword. These are accessible through the instance of the implementing class and can be overridden.

Let’s add a default method to our Person interface, which will also make a call to the static method of this interface:

default String getRating() {
    return "Good ";
}

Assume that this interface is implemented by the class Employee. For executing the default method an instance of this class should be created:

Person employee = new Employee();
String overview = employee .getRating();

Java 8 features

Java 8 new features

  1. forEach() method in Iterable interface
  2. default and static methods in Interfaces
  3. Functional Interfaces and Lambda Expressions
  4. Java Time API
  5. Collection API improvements
  6. Java IO improvements
  7. Miscellaneous Core API improvements
  8. Concurrency API improvements
  9. Java Stream API for Bulk Data Operations

forEach() method in Iterable interface

Remember ConcurrentModificationException if iterator is not used properly, no need to worry for it .
Java 8 has introduced forEach method in java.lang.Iterable interface so that while writing code we focus on business logic only. forEach method takes java.util.function.Consumer object as argument, so it helps in having our business logic at a separate location that we can reuse.

List<String> names = new ArrayList<>();
items.add("Ashwin");
items.add("Samar");
items.add("Omer");
items.add("Sachin");
items.add("Sandeep");

//lambda
items.forEach(item->System.out.print(item +"\t"));
//Output: Ashwin Samar Omer Sachin Sandeep

//Checking condition
items.forEach(item->{
    if("Ashwin".equals(item)){
        System.out.println(item);
    }
});
//Output : Ashwin

//method reference
items.forEach(System.out::println);
//Output: Ashwin Samar Omer Sachin Sandeep 

//Using Stream and filter
items.stream()
    .filter(s->s.contains("Samar"))
    .forEach(System.out::println);

//Output: Samar

default and static methods in Interfaces

Before Java 8, interfaces could have only public abstract methods. It was not possible to add new functionality to the existing interface without forcing all implementing classes to create an implementation of the new methods, nor it was possible to create interface methods with an implementation.

Starting with Java 8, interfaces can have static and default methods that, despite being declared in an interface, have a defined behavior.

Static Method
Consider the following method of the interface (let’s call this interface Person):

static String nationality() {
    return "Indian";
}

The static producer() method is available only through and inside of an interface. It can’t be overridden by an implementing class.
To call it outside the interface the standard approach for static method call should be used

String nationality = Person.nationality();