Open vasanth0989 opened 9 months ago
Java 8:
Functional Interfaces & Lamda Expressions
Streams
CompletableFuture
What is Functional interface
?
Functional interface is an Interface which has only single abstract method and it can contain n number for Default and static methods.
Streams:
Examples of terminal operation
forEach: Performs an action for each element of the stream.
toArray: Collects the elements of the stream into an array.
reduce: Performs a reduction on the elements of the stream, resulting in a single value.
collect: Performs a mutable reduction operation on the elements of the stream, resulting in a collection.
min: Finds the minimum element of the stream according to a given comparator.
max: Finds the maximum element of the stream according to a given comparator.
count: Returns the count of elements in the stream as a long.
anyMatch: Checks if any element of the stream matches a given predicate.
allMatch: Checks if all elements of the stream match a given predicate.
noneMatch: Checks if none of the elements of the stream match a given predicate.
findFirst: Returns the first element of the stream wrapped in an Optional.
findAny: Returns any element of the stream wrapped in an Optional.
More Streams example using 'groupingBy' and 'joining' from 'Collectors'
import java.util.*;
import java.util.stream.Collectors;
public class TestMain {
public static void main(String[] args) throws Exception {
// Let's do more Streaming
List<Account> accounts = new ArrayList<>();
accounts.add(new Account(1, "CHK", Arrays.asList("A1", "A2")));
accounts.add(new Account(2, "SVG", Arrays.asList("A3", "A4")));
accounts.add(new Account(3, "CHK", Arrays.asList("A5", "A6")));
accounts.add(new Account(4, "SVG", Arrays.asList("A7", "A8")));
// Group the accounts by its Type
// meaning Account type CHK should hold both Account id 1 and 3
// Let's see how we do it before java 8
Map<String, List<Account>> b4Java8Mp = new HashMap<>();
for(Account account: accounts){
String accountType = account.getType();
if(!b4Java8Mp.containsKey(accountType)){
List<Account> accts = new ArrayList<>();
accts.add(account);
b4Java8Mp.put(accountType, accts);
}else{
b4Java8Mp.get(accountType).add(account);
}
}
System.out.println("Using Java8 Map"+b4Java8Mp);
// Now Using Java8 Streams and Collectors we can do everything in single ine
Map<String, List<Account>> java8Mp = accounts.stream().collect(Collectors.groupingBy(Account::getType));
// Help me reduce the List of nick names to single string value, like A1,A2,A3..
String nickNames = accounts.stream().flatMap(acct -> acct.getNickNames().stream()).collect(Collectors.joining(","));
System.out.println("NickNames here:"+nickNames);
}
}
class Account {
private int id;
private String type;
private List<String> nickNames;
public Account() {
}
public Account(int id, String type, List<String> nickNames) {
this.id = id;
this.type = type;
this.nickNames = nickNames;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public List<String> getNickNames() {
return nickNames;
}
public void setNickNames(List<String> nickNames) {
this.nickNames = nickNames;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", type='" + type + '\'' +
", nickNames=" + nickNames +
'}';
}
}
ctrl+alt+l -> is for formatting the code alt+enter -> is to get import suggestion ctrl+alt+o -> to organize or remove unused imports