Showing posts with label Java8. Show all posts
Showing posts with label Java8. Show all posts

Monday, June 09, 2014

What's New in Java 8 - Method References

Method references provide easy-to-read lambda expressions for methods that already have a name.
We have seen lambda expressions to crate anonymous methods in my post What's New in Java 8 - Lambda Expressions. Sometimes a lambda expression does nothing but call an existing method (will see example in detail). In those cases, we use method references instead lambda expressions. Method references are compact and easy to read lambda expressions for methods that already have a name.

Example  :

To better understand, consider the same car example discussed in my previous post, but here a extra compareByEngineCC static method.

public class Car{
    public enum Color {
        BLACK, WHITE, RED
    }
    public String modelName;
    public Color color;
    public int engineCC;
    public void printModelName() {
        // ...
    }
    
    public static int compareByEngineCC(Car c1, Car c2) {
        if( c1.engineCC  == c2.engineCC) return 0;
        return c1.engineCC > c2.engineCC?1:-1;
    }
}

The requirement is sort the array of cars by engineCC, so that I added a static method to  compare the cars.
We can use sort method which is static in Arrays class to meat this requirement, but in order to use this static method we need to provide Comparator, So we implemented a CarEngineCCComparator class as shown below

Thursday, May 01, 2014

What's New in Java 8

I posted a post with title 'What's New in Java 7', so I am posting the same for Java 8 also but I will post separate on each new feature of Java 8, this post is something introduction of Java 8  features, will discuss each feature separate with detailed examples and usages of the feature.

Java 8  Features:
  • Lambda Expressions: These are the expression like any other expressions and enable us to treat functionality as a method argument, or code as data. Lambda expressions let us express instances of single-method interfaces more compactly. These single abstract method interfaces are also called as functional interfaces. Here you may get one question, Why It is being specified  abstract method though all methods in interface are abstract. Well, these functional interfaces may have implemented methods they are called  default (new feature in java 8).
  • Default Methods: As we discussed  Default methods are methods in an interface that have an implementation. They enable new functionality to be added to the interfaces of libraries. New Package has been added for this functional interfaces to library 
  • Aggregate Operations: Aggregate operations  enable us to perform functional-style operations on streams of elements, in particular, bulk operations on collections, such as sequential or parallel map-reduce transformations. New methods and inter faces added in collection frame work.
  • Date-Time APIs: These new APIs enable us to represent dates and times and manipulate date and time values. They support the International Organization for Standardization (ISO) calendar system as well as other commonly used global calendars.
There are other features added to Java 8 like type annotations, enhanced security etc. But I will be discussing above four features in detail.  In my  next post I will discuss Lambda Expressions. 

Understanding Essential DNS Record Types for Web Administrators

  Understanding Essential DNS Record Types for Web Administrators Introduction The Domain Name System (DNS) acts as the backbone of the inte...