You are on page 1of 7

JAVA 8 COMES UP WITH LOT OF NEW FEATURES LIKE

Lambda, Functional Interface , Stream API, Default Methods, ForEach Method

LAMBDA EXPRESSIONS: Lambda expression helps us to write our code in functional style, we can write
better code in less number of line and it is faster also (it is faster because only single .class file will
generated while executing it so complier will load only one class and run it )

METHOD REFERENCES: we can use colon to call method LIKE CLASSNAME:: METHOD NAME

FUNCTIONAL INTERFACES:

An Interface that contains only one abstract method is known as functional interface. It can have any
number of default and static method

EXAMPLE of inbuilt Functional interface: PREDICATE INTERFACE, CONSUMER INTERFACE, SUPPLIER


INTERFACE, AND FUNCTION INTERFACE.

1-PREDICATE INTERFACE:

The Predicate interface has only one single method test(). It may be true or false depending on the
values of its variable

// Creating predicate
     Predicate<Integer> lesserthan = i -> (i < 18); 
  
// Calling Predicate method
        System.out.println(lesserthan.test(10)); 

2-CONSUMER INTERFACE:

The Consumer interface has only one single method called accept(). It accepts a single argument of any
data type and does not return any result.

forEach Loop is exapmle : Arrays.asList(countries).forEach((country) -> System.out.println(country));

3-SUPPLIER INTERFACE

A Supplier interface has only one single method called get(). It does not accept any arguments and
returns an object of any data type.

4- FUNCTION INTERFACE

The Function interface has only one single method apply(). It can accept an object of any data type and
returns a result of any datatype.
STREAM API: Stream is a pipeline of computational operations, calculation happen in memory not on
collection so we will not get Concurrent Modification Exceptions with collection .Stream is lazy and
evaluates code only when required.

DEFAULT METHODS: it provides backward compatibility to our code, we can add methods later in
interface and it will not give any compilation error (for example I have Interface A and it is implemented
by B and C class if I add any new method in interface it will give compilation error in B and C classes
because B,C has to implement new methods but after Java 1.8 we can add methods as a default and it
will not cause any issue )

STATIC METHODS IN INTERFACE: the static method in an interface can be defined in the interface, but
cannot be overridden in Implementation Classes. If we want give something common for all we can use
these static methods.

forEach: Java provides a new method forEach() to iterate the elements. It is defined in Iterable and
Stream interfaces

STREAM INTERMEDIATE AND TERMINAL OPERATION:

INTERMEDIATE OPERATION: The operations which return another stream as a result are called
intermediate operations

Example: map(), filter(), distinct(), sorted(), limit(), skip()

TERMINAL OPERATIONS:

the operations which return non-stream values like primitive or object or collection or return nothing
are called terminal operations.

Example : collect(), min(), max(), count(), anyMatch(), allMatch(), noneMatch(), findFirst(), findAny()

DIFFERENCE BETWEEN MAP() AND FLATMAP() IN JAVA STREAM

Map: map() operation returns a single value for a single input, Only perform the mapping. Produce a
stream of value.

Example :  Stream of Employees to a Stream of Strings with just the employee names.

List<String> mappedList = employeeList.stream().


                     map(emp -> emp.getName()).
                     collect(toList());
  mappedList.forEach(System.out::println);

FlatMap: flatmap() operation returns an arbitrary number of values as the output. Perform mapping as
well as flattening. Produce a stream of stream value.
Example : We can use flatMap to get all names of employee as character Array streamand use them
again to make all UPPERCASE letters.

List<String> nameCharList = employeeList.stream()


            .map(emp-> emp.getName().split(""))
            .flatMap(array->Arrays.stream(array))
            .map(str -> str.toUpperCase())
            .filter(str -> !(str.equals(" ")))
            .collect(toList());

flatMap() method converts Stream<String[]> into first a Stream<Stream<String>> and then flattens it


to Stream<String> i.e. a stream of single character Strings.
map() is applied next, which converts this Stream<String>’s contents to uppercase and then filters
out the space characters using the filter() method.

WHAT IS OPTIONAL CLASS?

Java 8 has introduced a new class Optional in java.util package. It can help in writing a neat code without
using too many null checks. By using Optional, we can specify alternate values to return or alternate
code to run

We have methods in optional class:  

 isPresent() : If a value is present, isPresent() will return true

ofNullable() :It returns an Optional describing the specified value, if non-null, otherwise returns an
empty Optional.

empty() It returns an empty Optional object. No value is present for this Optional.

get() If a value is present in this Optional, returns the value, otherwise throws
NoSuchElementException.
PROGRAMMING:

JAVA 8 Programming Questions:

 Finding Duplicates :
List<String> duplicateCompanies = companies
.stream()
.filter(company -> Collections.frequency(companies, company) > 1)
.collect(Collectors.toList());
 Find Unique elements after removing duplicates
List<String> distinctCompanies = companies
.stream()
.distinct()
.collect(Collectors.toList());

SORTING USING JAVA8:

 Sorting list in natural order :


List < String > sortedList =
fruits.stream().sorted(Comparator.naturalOrder()).collect(Collectors.toList())
 Reverse Order :
List < String > sortedList =
fruits.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());

 Employee with Max Salary :

employeeList.stream() .collect(Collectors.maxBy(Comparator.comparing(Employee::getSa
lary)));

 Employee with Min Age


employeeList.stream() .collect(Collectors.minBy(Comparator.comparing(Employee::getAge)));

 Sorting Employee on Salary with ascending Order


List < Employee > employeesSortedList2 = employees.stream()
.sorted(Comparator.comparingLong(Employee::getSalary)).collect(Collectors.toList());
//ascending order
 Sorting Employee on Salary with decending Order

List < Employee > employeesSortedList2 =


employees.stream()
.sorted(Comparator.comparingLong(Employee::getSalary).reversed()).collect(Collectors.toList(
));

 Sort Employee on Salary and Name :

// Create Comparators for Name and Salary fields respectively

Comparator<Employee> sortByName = (e1, e2) ->


e1.getName().compareToIgnoreCase(e2.getName());

Comparator<Employee> sortBySalary = (e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary());

//Sort by Name then Sort by Salary

employees.stream().sorted(sortByName.thenComparing(sortBySalary)).

forEach(e->System.out.println(e));

 SORTING ON MULTIPLE FIELDS

Comparator<Book> sortByName = (b1, b2) -> b1.getName().compareToIgnoreCase(b2.getName());

Comparator<Book> sortByAuthor = (b1, b2) -> b1.getAuthor().compareToIgnoreCase(b2.getAuthor());

Comparator<Book> sortByCost = (b1, b2) -> Double.compare(b1.getCost(), b2.getCost());

//Sort by Name then Sort by Author then sort by Cost

bookList.stream() .sorted(

sortByName.thenComparing(sortByAuthor).thenComparing(sortByCost)

)
.forEach( book-> System.out.println(book) );

 GroupBy in Java 8
https://stackabuse.com/guide-to-java-8-collectors-groupingby/

DATA:

List<Student> students = Arrays.asList(

new Student("Math", "John", "Smith", "Miami", 19),

new Student("Programming", "Mike", "Miles", "New York", 21),

new Student("Math", "Michael", "Peterson", "New York", 20),

new Student("Math", "James", "Robertson", "Miami", 20),

new Student("Programming", "Kyle", "Miller", "Miami", 20)

);

group our students into groups of students by their subjects:


Map<String, List<Student>> studentsBySubject = students

.stream()

.collect(

Collectors.groupingBy(Student::getSubject)

);

Map<String, List<String>> studentsByCity = students.stream()

.collect(Collectors.groupingBy(
Student::getCity,
Collectors.mapping(Student::getName, Collectors.toList())));

Map<Integer, Long> countByAge = students.stream()

.collect(Collectors.groupingBy(

Student::getAge,

Collectors.counting()));

Map<String, List<String>> namesByCity = students.stream()

.collect(Collectors.groupingBy(

Student::getCity,

TreeMap::new,

Collectors.mapping(Student::getName, Collectors.toList())));

You might also like