Advertisement

Sri Lanka's First and Only Platform for Luxury Houses and Apartment for Sale, Rent

Wednesday, March 26, 2014

Java 8 Features Part 1

"Change is the only thing that doesn't change". This statement explains well anything and Java is not an exception in this regard. Java version 8 is released officially and you can download it from Oracle. There are some significant changes introduced in Java 8 and following are some of those;
  1. Parallel Operations - Parallel Operations for JVMs running on top of Multi Core Platforms
  2. Method References - Ability to pass in a Method as a Reference. 
  3. Lambda Expressions - Biggest Syntax Changes to Java Ever
So I'll start off with Parallel Operations and make my way down on each topic. 

Parallel Operations have been introduced to leverage the Multiple CPUs available in almost every modern computer. Operations such as Sorting could become significantly faster if Multiple CPUs were used. The Arrays.parallel* methods provide these functions.

import java.util.Arrays;

public class ParallelOperations {

    public static void main(String[] args) {
        Integer[] ints = Util.generateRandomIntegers();
        sort(ints);
        Util.display(ints);
        ints = Util.generateRandomIntegers();
        parallelSort(ints);
        Util.display(ints);
    }

    public static void sort(Integer[] ints) {
        long startTime = System.currentTimeMillis();
        Arrays.sort(ints);
        long endTime = System.currentTimeMillis();
        System.out.printf("sort() Finished in %d milliseconds\n",
                (endTime - startTime));
    }

    public static void parallelSort(Integer[] ints) {
        long startTime = System.currentTimeMillis();
        Arrays.parallelSort(ints);
        long endTime = System.currentTimeMillis();
        System.out.printf("parallelSort() Finished in %d milliseconds\n",
                (endTime - startTime));
    }

}


And passing method references can be done as following;

import java.util.Arrays;
import java.util.Comparator;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

public class MethodReferences {

    public static void main(String[] args) {
        Integer[] ints = Util.generateRandomIntegers();
        // Passing Arrays.sort static method as an argument
        sort(Arrays::sort, ints);
        Util.display(ints);
        ints = Util.generateRandomIntegers();
        // Passing Arrays.parallelSort static method as an argument
        sort(Arrays::parallelSort, ints);
        Util.display(ints);
        String[] strings = new String[] {"one", "TWO", "tHree"};
        // Passing Arrays.parallelSort static method and String.compareTo instance method as an arguments
        sort(Arrays::sort, String::compareTo, strings);
        Util.display(strings);
        strings = new String[] {"ball", "apple", "cat"};
        // Passing Arrays.parallelSort static method and String.compareToIgnoreCase instance method as an arguments
        sort(Arrays::parallelSort, String::compareToIgnoreCase, strings);
        Util.display(strings);
    }
    
    public static void sort(Consumer<Integer[]> sorter, Integer[] values) {
        sorter.accept(values);
    }
    
    public static void sort(BiConsumer<String[], Comparator<String>> sorter, Comparator<String> comparator, String[] values) {
        sorter.accept(values, comparator);
    }
}

References
  • Java 8 Compiler support for Eclipse Kepler - https://wiki.eclipse.org/JDT/Eclipse_Java_8_Support_For_Kepler

No comments: