Java 8 Features
Java 8 Features
• Functional Interfaces
• Lambda Expression
• Method References
• Stream API
• Default Methods
• Static Method
• Base64 Encode and Decode
• Etc.
Advantages of Java8 new features
No parameter Syntax:
( ) -> {
//Body of no parameter lambda
}
One Parameter Syntax
(p1) -> {
//Body of single parameter lambda
}
Two Parameter Syntax
(p1,p2) -> {
//Body of multiple parameter lambda
}
Example of Lambda Expression with one
parameter
@functional interface
interface NumericTest{
boolean test(int n);
}
containingObject::instanceMethodName
Intermediate-
• Methods are chained together.
• Intermediate operations transform a stream into another stream.
• It enables the concept of filtering where one method filters data and passes it to
another method after processing.
Terminal- Terminal Operations are the type of Operations that return the result. These
Operations are not processed further just return a final result value.
Few Intermediate Operations
Method Meaning Syntax
map() The map method is used to return a List number = Arrays.asList(2,3,4,5);
stream consisting of the results of List square = number.stream().map(x-
applying the given function to the >x*x).collect(Collectors.toList());
elements of this stream.
filter() The filter method is used to select List names =
elements as per the Predicate passed as Arrays.asList("Reflection","Collection","St
an argument. ream");
List result = names.stream().filter(s-
>s.startsWith("S")).collect(Collectors.toList
());
sort() The sorted method is used to sort the List names =
stream. Arrays.asList("Reflection","Collection","St
ream");
List result =
names.stream().sorted().collect(Collectors.t
oList());
Few Terminal Operations
Method Meaning Syntax
collect() The collect method is used to return the List number = Arrays.asList(2,3,4,5,3);
result of the intermediate operations Set square = number.stream().map(x-
performed on the stream. >x*x).collect(Collectors.toSet());
forEach() The forEach method is used to iterate List number = Arrays.asList(2,3,4,5);
through every element of the stream. number.stream().map(x-
>x*x).forEach(y-
>System.out.println(y));
reduce() The sorted method is used to sort the List number = Arrays.asList(2,3,4,5);
streThe reduce method is used to reduce int even = number.stream().filter(x->x
the elements of a stream to a single value. %2==0).reduce(0,(ans,i)-> ans+i);
The reduce method takes a
BinaryOperator as a parameter.
Stream Interface
Stream Interface
Stream Interface
Stream Interface
Example Java program to show intermediate
operations
Reduction operations
• Many times, we need to perform operations where a stream reduces to
single resultant value, for example, maximum, minimum, sum, product,
etc.
• Reducing is the repeated process of combining all elements.
• sum(), min(), max(), count() etc. are some examples of reduce
operations.
Base64 Encode and Decode
• Base 64 is an encoding scheme that converts binary data into text format so that
encoded textual data can be easily transported over network un-corrupted and
without any data loss.
• Java provides a class Base64 to deal with encryption.
• We can encrypt and decrypt data by using provided methods.
• java.util.Base64 need to be imported in source file to use its methods.
• There are 3 types of encoding and decoding capabilities as standard.
-Base or Simple Type
-URL Encoding/Decoding
-MIME Encoding/Decoding
Java 8 Basic Base64
• It uses the Base64 alphabet specified by Java in RFC 4648 and RFC 2045 for
encoding and decoding operations.
• The basic encoder keeps things simple and encodes the input as-is, without any
line separation.
• The encoder maps the input to a set of characters in the A-Za-z0-9+/ character set.
• The decoder rejects data that contains characters outside the base64 alphabet.
encoding String--
Decoding String--
Java 8 Base64 Encoding Without Padding
1.@Repeatable(Games.class)
2.@interfaceGame{
3. String name();
4. String day();
5.}
Declare the containing annotation type
• Containing annotation type must have a value element with an array type.
• The component type of the array type must be the repeatable annotation type.
• Example- declaring Games containing annotation type:
1.@interfaceGames{
2. Game[] value();
3.}