Tuesday 22 September 2015

Java 1.8 features....


1. One of the main difference is introduction of static and default methods in interface. This will gradually obsolete  the use of abstract classes in java


reference ; http://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html

example :

public interface HasFieldValue<T>
{
    T getFieldValue();

    static <T> String getFieldAsString( HasFieldValue<T> hasFieldValue )
    {
        return ( hasFieldValue != null ) ? hasFieldValue.getFieldValue().toString() : "";
    }

    static <R extends HasFieldValue<V>, V> R fromValue( R[] possibleResultValues, V fieldValue )
    {
        if ( fieldValue != null )
        {
            for ( R resultValue : possibleResultValues )
            {
                if ( resultValue.getFieldValue().toString().equals( fieldValue.toString() ) )
                {
                    return resultValue;
                }
            }
        }
       
    

    default Object getDefaultForEmptyField()
    {
        throw new IllegalArgumentException( "Field cannot be blank" );
    }
}

2. lamba expressions..

1 comment:

bala said...

Thanks for sharing this interesting code piece.

couldnt understand following code fragments. can you please explain ?
a. static String getFieldAsString( HasFieldValue hasFieldValue )
b. static , V> R fromValue( R[] possibleResultValues, V fieldValue )