Collections API : Supports Lambda expressions

Most of Java developers will be unfamiliar with the concepts behind lambdas and with how designs incorporating lambdas look and behave. Java Lambda Expressions would surely change of our programming habits and also the way we look at the language, including the various Java APIs. When Lambda Expression is added to a programming language, it becomes very important to utilize the new feature to empower the overall programming model along with the existing set of libraries .

Collections and Algorithms

Java Collections framework was introduced in JDK 1.2, and since then its core interfaces have never been changed. This is because, the Java Collections framework is so widely used, that any changes to it will surely broke many existing functionalities, and that’s why it is not easy to completely rewrite the Java Collections API. Algorithms, a more functional-centric way of interacting with collections.

To overcome these challenges, Java 8 has added new set of methods to the existing collection classes and interfaces. Java Collections framework will work as usual and will also have an additional potential to support Lambda Expressions.

Changes in the Collections API

Comparisons

One of the drawbacks to the Comparator implementation. Suppose We have a class Car. Which have properties name and number. If we write code for comparisons , names are compared first and number is compared only if the names are identical. After implementation, If our requirements changed that sorting be done by number first then names, a new Comparator must be written.

But lambda, It would make easy.

public static final Comparator<Car> BY_NAME =

(lhs, rhs) -> lhs.name.compareTo(rhs.name);

public static final Comparator<Car> BY_NUMBER =

(lhs, rhs) -> lhs.number – rhs.number;


=====================================================================


public static final Comparator<Car> BY_NAME =

Comparators.comparing(Car::getName);

public static final Comparator<Car> BY_NUMBER =

Comparators.comparing(Car::getNumber);

Composition

In Java 8, the Comparator interface comes with several methods.

For example: Comparator .thenComparing() method takes a Comparator to use for comparison after the first one compares.

Collections.sort(car, CAR.BY_NAME.thenComparing(CAR.BY_NUMBER));

====================================================================

Collections.sort(car,Comparators.comparing(CAR::getName) .thenComparing(CAR::getNumber));

Iteration

Another change is introduce in Java 8 for Iteration. It will bring to collections a change via the forEach() default method defined on the Iterator and Iterable interfaces . It makes to easy for collection Iteration.

import java.util.ArrayList;

import java.util.List;

public class JavaOopsList {

public static void main(String[] args) {

List<String> list = new ArrayList<String>();

list.add("java");

list.add("oops");

list.add("list");

list.forEach((it) -> System.out.println("List: " + it));

}

}


Output :

List: java

List: oops

List: list

Stream

the Stream interface is a fundamental interface that is use in number of scenarios, including the Collections API. It represents a stream of objects and behave like to Iterator to gives us access on object at a time through a collection. when a Collection converts to a Stream then stream() method introduce in the Collection interface and filter method can be used to produce a new stream through which only the filtered objects come. A stream should be operated on only once.

public class JavaOopsStream {

public static void main(String[] args) {

List<String> list = new ArrayList<String>();

list.add("java");

list.add("oops");

list.add("list");

// steam and filter

list.stream().filter(x->x.equals("oops"))

.forEach(x->System.out.print(x));

}

}

Output: oops

filter() method is lazy. it will filter only on demand.