Sunday, July 2, 2017

Venkat Subramanian, Lazy evaluation in Streams




import java.util.Arrays;
import java.util.List;

public class Lazy {
 public static void main(String[] args) {
  List values = Arrays.asList(1, 2, 3, 5, 4, 6, 7, 8, 9);
  System.out.println(values.stream().filter(Lazy::isGT3).filter(Lazy::isEven).map(Lazy::doubleIt).findFirst().orElse(0));

 }

 public static boolean isGT3(int number) {
  System.out.println("isGT3 " + number);
  return number > 3;
 }
 

 public static boolean isEven(int number) {
  System.out.println("isEven " + number);
  return number % 2 == 0;
 }
 
 public static int doubleIt(int number) {
  System.out.println("doubleIt " + number);
  return number * 2;
 }


}




import java.util.stream.Stream;

public class SumEvenNumbers {
 public static int compute(int start, int count) {
  return Stream.iterate(start, e -> e + 1).filter(e -> e % 2 == 0).mapToInt(e -> e * 2).limit(count).sum();
 }
 
 public static void main(String[] args) {
  System.out.println(compute(51, 101));
 }

}




AWS tutorial

Entertaining non-technical presentation:





This one is extensive and sort-of-technical but boring and not hands-on (only slides):





Disappointed by the available free training, I have eventually purchased from https://www.udemy.com/ the aws-certified-solutions-architect-associate course by A Cloud Guru, and created a free AWS account https://eu-west-2.console.aws.amazon.com/console/home?region=eu-west-2#



Venkat Subramanian, Java 8 hidden treasures



Lot of examples on Collectors here https://docs.oracle.com/javase/8/docs/api/java/util/stream/Collectors.html

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class CSV {
 public static void main(String[] args) {

  List<String> names = Arrays.asList("a", "b", "c");
  System.out.println(names.stream().map(String::toUpperCase).collect(Collectors.toList()));
  System.out.println(names.stream().map(String::toUpperCase).collect(Collectors.joining(",")));

 }
}




interface Util {
 public static int numberOfCores() {
  return Runtime.getRuntime().availableProcessors();
 }
}

public class IFWithStaticMethod {
 public static void main(String[] args) {
  System.out.println(Util.numberOfCores());

 }
}


interface Fly {
 default void takeoff() {
  System.out.println("Fly:takeoff");
  getState();
 }

 void getState();
}

public class IFDefault implements Fly {
 public static void main(String[] args) {
  IFDefault default1 = new IFDefault();
  default1.takeoff();
 }

 @Override
 public void getState() {
  System.out.println("IFDefault:getState");

 }

}





import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.Comparator;

enum GENDER {
 MALE, FEMALE
}

class Person {
 @Override
 public String toString() {
  return "Person [name=" + name + ", gender=" + gender + ", age=" + age + "]";
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public GENDER getGender() {
  return gender;
 }

 public void setGender(GENDER gender) {
  this.gender = gender;
 }

 public int getAge() {
  return age;
 }

 public void setAge(int age) {
  this.age = age;
 }

 String name;
 GENDER gender;
 int age;

 public Person(String name, GENDER gender, int age) {
  super();
  this.name = name;
  this.gender = gender;
  this.age = age;
 }

}

public class Sorting {
 public static List<Person> getPersons() {
  return Arrays.asList(new Person("Sara", GENDER.FEMALE, 20), new Person("Sara", GENDER.FEMALE, 22),
    new Person("Bob", GENDER.MALE, 20), new Person("Paula", GENDER.FEMALE, 32),
    new Person("Paul", GENDER.MALE, 32), new Person("Jack", GENDER.MALE, 2),
    new Person("Jack", GENDER.MALE, 72), new Person("Jill", GENDER.FEMALE, 12));
 }
 
 public static void main(String[] args) {
  System.out.println(getPersons().stream().sorted(Comparator.comparing(Person::getName)).collect(Collectors.toList()));
 }

}





import java.util.function.Predicate;

public class TestPredicate {
 public static void main(String[] args) {
  Predicate<Integer> isOdd = (param) -> param % 2 == 0;
  Predicate<Integer> isOddAndGreaterThan5 = isOdd.and((param) -> param.intValue() > 5);
  
  System.out.println(isOdd.test(2));
  System.out.println(isOdd.test(3));
  System.out.println(isOddAndGreaterThan5.test(3));
  System.out.println(isOdd.test(6));
 }
}