Java 8
Java 8
Java 8
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
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.
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
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()
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.
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.
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
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:
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());
employeeList.stream() .collect(Collectors.maxBy(Comparator.comparing(Employee::getSa
lary)));
employees.stream().sorted(sortByName.thenComparing(sortBySalary)).
forEach(e->System.out.println(e));
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:
);
.stream()
.collect(
Collectors.groupingBy(Student::getSubject)
);
.collect(Collectors.groupingBy(
Student::getCity,
Collectors.mapping(Student::getName, Collectors.toList())));
.collect(Collectors.groupingBy(
Student::getAge,
Collectors.counting()));
.collect(Collectors.groupingBy(
Student::getCity,
TreeMap::new,
Collectors.mapping(Student::getName, Collectors.toList())));