package pt.joaobrito.java8;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.Month;
import java.time.Period;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
public class Main {
public static void main(String[] args) throws InterruptedException {
// simple lambda example
print("Hello Lambda", s -> s.length());
// another lambda example
new Thread(() -> System.out.println("Hello Runnable")).start();
List<Person> people = new ArrayList<>(Arrays.asList(
new Person("John Doe", LocalDate.of(1978, Month.JUNE, 14)),
new Person("Jane Doe", LocalDate.of(1983, Month.FEBRUARY, 16)),
new Person("Steven Smith", LocalDate.of(2008, Month.SEPTEMBER, 23)),
new Person("Martin", LocalDate.of(2010, Month.NOVEMBER, 27)),
new Person("Mike", LocalDate.of(2012, Month.NOVEMBER, 3)),
new Person("Louis", LocalDate.of(2014, Month.NOVEMBER, 6)),
new Person("Mary", LocalDate.of(2016, Month.FEBRUARY, 16))
));
System.out.println("---- All people sorted by name -----");
people.sort((p1, p2) -> p1.getName().compareTo(p2.getName()));
people.forEach(p -> System.out.println(p.getName()));
System.out.println("---- All people -----");
people = new ArrayList<>(Arrays.asList(
new Person("John Doe", LocalDate.of(1978, Month.JUNE, 14)),
new Person("Jane Doe", LocalDate.of(1983, Month.FEBRUARY, 16)),
new Person("Steven Smith", LocalDate.of(2008, Month.SEPTEMBER, 23)),
new Person("Martin", LocalDate.of(2010, Month.NOVEMBER, 27)),
new Person("Mike", LocalDate.of(2012, Month.NOVEMBER, 3)),
new Person("Louis", LocalDate.of(2014, Month.NOVEMBER, 6)),
new Person("Mary", LocalDate.of(2016, Month.FEBRUARY, 16))
));
people.stream().map(Person::getName).forEach(System.out::println); // using method reference
System.out.println("---- People older than 9 yrs -----");
people
.stream() // convert to stream
.filter(p -> Period.between(p.getDob(), LocalDate.now()).getYears() > 9) // get only the people thaht matches the given condition
.map(Person::getName) // get a new stream with only the names => returns Stream<String>
.forEach(System.out::println); // iterates the entire stream and prints out each element
System.out.println("---- Remove items from list -----");
people.removeIf(i -> i.getAge() < 2); // remove if the given condition is true
people.
stream()
.map(Person::getName)
.sorted(Comparator.reverseOrder()) // order the list in reverse order
.forEach(System.out::println);
// calculate time between two dates
LocalDate dob = LocalDate.of(1978, 6, 14);
LocalDate now = LocalDate.now();
Period p = Period.between(dob, now);
System.out.println("Priod of " + p.getYears() + " years; " + p.getMonths() + " months, " + p.getDays() + " days.");
// calculate time between two instants
Instant start = Instant.now();
Thread.sleep(1000);
Instant end = Instant.now();
System.out.println("The time is... " + Duration.between(start, end).toMillis());
// Optional<T>
Optional<String> opt = Optional.ofNullable("some string"); // not null value
System.out.println(opt.orElse("or else"));
opt.orElseThrow(UnsupportedOperationException::new);
opt = Optional.ofNullable(null); // notice that here we pass a null value
System.out.println(opt.orElse("or else"));
opt.orElseThrow(UnsupportedOperationException::new);
}
public static void print(String s, MyLambda l) {
System.out.println(l.getLength(s));
}
}
@FunctionalInterface
interface MyLambda {
int getLength(String s);
}