import java.util.Arrays;
import java.util.*;
import java.util.stream.*;
import java.time.*;
public class HelloWorld{
public static void main(String []args){
System.out.println("Hello World");
Formula formula = new Formula() {
@Override
public double calculate(double a, double b) {
return a*b;
}
};
formula.calculate(10.9,93.0);
formula.showValue(29.0);
showLambada();
functionalInterfaceExample();
}
}
//
double calculate(double a, double b);
default void showValue(double sum){
System.out.println("sum value is "+sum);
}
}
public void showLambada(){
List<String> names = Arrays.asList("peter", "anna", "mike", "xenia");
Collections.sort(names, new Comparator<String>() {
@Override
public int compare(String a, String b) {
return b.compareTo(a);
}
});
//with lambada expression.
Collections.sort(names, (String a, String b)-> b.compareTo(a));
Collections.sort(names, (a,b)-> b.compareTo(a));
//Collecting a list in upper case
List aaList = names.stream().map(s->s.toUpperCase()).filter(s->s.length()>3)
.collect(Collectors.toList());
System.out.println(aaList);
}
-------------------------------------------------------------------------------------------------------------
//Functional Interface
Converter<String, Integer> converter = (value)->Integer.valueOf(value);
Integer convertedValue = converter.convert("3838");
System.out.println(convertedValue);
}
//TimeZones : import java.time.*; to get all API's
public void showTimeZones(){
System.out.println(ZoneId.getAvailableZoneIds());
// prints all available timezone ids
ZoneId zone1 = ZoneId.of("Europe/Berlin");
ZoneId zone2 = ZoneId.of("Brazil/East");
System.out.println(zone1.getRules());
System.out.println(zone2.getRules());
}
//Date & Time API
public void showDateTime(){
//Clock
Clock clock = Clock.systemDefaultZone();
long millis = clock.millis();
Instant instant = clock.instant();
Date legacyDate = Date.from(instant); // legacy java.util.Date
System.out.println("millis :"+millis);
System.out.println("instant ::"+ instant);
System.out.println("legacyDate ::"+ legacyDate);
}
import java.util.*;
import java.util.stream.*;
import java.time.*;
public class HelloWorld{
public static void main(String []args){
System.out.println("Hello World");
Formula formula = new Formula() {
@Override
public double calculate(double a, double b) {
return a*b;
}
};
formula.calculate(10.9,93.0);
formula.showValue(29.0);
showLambada();
functionalInterfaceExample();
}
}
//
Default Methods for Interfaces
interface Formula {double calculate(double a, double b);
default void showValue(double sum){
System.out.println("sum value is "+sum);
}
}
public void showLambada(){
//LAMBADA EXPRESSION.
//Old way of sorting.List<String> names = Arrays.asList("peter", "anna", "mike", "xenia");
Collections.sort(names, new Comparator<String>() {
@Override
public int compare(String a, String b) {
return b.compareTo(a);
}
});
//with lambada expression.
Collections.sort(names, (String a, String b)-> b.compareTo(a));
Collections.sort(names, (a,b)-> b.compareTo(a));
//Collecting a list in upper case
List aaList = names.stream().map(s->s.toUpperCase()).filter(s->s.length()>3)
.collect(Collectors.toList());
System.out.println(aaList);
}
-------------------------------------------------------------------------------------------------------------
Functional Interfaces:
@FunctionalInterface
interface Converter<F, T> {
T convert(F from);
}
public void functionalInterfaceExample(){//Functional Interface
Converter<String, Integer> converter = (value)->Integer.valueOf(value);
Integer convertedValue = converter.convert("3838");
System.out.println(convertedValue);
}
//TimeZones : import java.time.*; to get all API's
public void showTimeZones(){
System.out.println(ZoneId.getAvailableZoneIds());
// prints all available timezone ids
ZoneId zone1 = ZoneId.of("Europe/Berlin");
ZoneId zone2 = ZoneId.of("Brazil/East");
System.out.println(zone1.getRules());
System.out.println(zone2.getRules());
}
//Date & Time API
public void showDateTime(){
//Clock
Clock clock = Clock.systemDefaultZone();
long millis = clock.millis();
Instant instant = clock.instant();
Date legacyDate = Date.from(instant); // legacy java.util.Date
System.out.println("millis :"+millis);
System.out.println("instant ::"+ instant);
System.out.println("legacyDate ::"+ legacyDate);
}