Java Fundamentalks:
Lambda

public List<String> nombresOrdenadosPorTamaño() {
  var lista = nombres();
  Collections.sort(lista, new Comparator<>() {
    public int compare(String s1, String s2) {
      return Integer.compare(s1.length(), s2.length());
    }
  });

  return lista;
}
public List<String> nombresOrdenadosPorTamaño() {
  var lista = nombres();
  Collections.sort(lista, new Comparator<>() {
    public int compare(String s1, String s2) {
      return Integer.compare(s1.length(), s2.length());
    }
  });

  return lista;
}

Anatomía de una expresión

https://introcs.cs.princeton.edu/java/33design/images/lambda.png

(a, b) -> a + b
s -> s.length()
obj -> Objects.nonNull(obj)
código -> !código.isEmpty()
() -> listaDePrecios(lote)
obj -> System.out.println(obj)
(p1, p2) -> Double.compare(p1.precio(), p2.precio())
(id, precio) -> System.out.println(id + " " + precio)

Method References

(String s) -> s.length() 

(String s) -> s.length()

String::length

(String s) -> s.length()

(a, b) -> Math.max(a, b)

(a, b) -> Math.max(a, b)

Math::max

(a, b) -> Math.max(a, b)

Objects::nonNull
System.out::println
Integer::parseInt
ArrayList::new
this::resolve

Java Fundamentalks

By Carlos Obregón

Java Fundamentalks

  • 730