Beruflich Dokumente
Kultur Dokumente
CONTEXTO
Una de las tareas más comunes en programación, es el trabajo con
arreglos. Y en este contexto, lo que necesitamos es recorrer sus
elementos.
Existen varias opciones para recorrer un arreglo:
Recorrido indexado.
Recorrido tipo colección.
Expresiones Lambda
RECORRIDO INDEXADO
A continuación tienes un ejemplo de como usar el recorrido indexado:
public class Prueba01 {
}
RECORRIDO TIPO COLECCIÓN
Conocido como for each. A continuación tienes un ejemplo de como usar
el recorrido tipo colección:
public class Prueba02 {
}
EXPRESIONES LAMBDA
A continuación tienes un ejemplo de cómo usar las expresiones lambda en
el recorrido de un arreglo:
public class Prueba03 {
}
La ventaja de las expresiones lambda, es que puedes aplicar operaciones,
como por ejemplo filtros. A continuación tienes un ejemplo:
public class Prueba04 {
}
Utilizando expresiones lambda, puedes encontrar muchas mas ventajas.
Lambda Expressions
A block of code that you can pass around so it can be executed later, once
or multiple times.
Anonymous methods.
Reduce verbosity caused by anonymous classes.
- Alonzo Church in his invention of the lambda calculus in 1936. - Lisp 1958
- Supported in C#, JavaScript, Python, Ruby, C++11
How are they different from Java methods?
Lambdas
() -> 42
Lambdas
Examples
Before
After
Lambdas
Examples
Functional Interfaces
Example
@FunctionalInterface
public interface Runnable {
public void run();
}
java.util.function
Method References
Treating an existing method as an instance of a Functional Interface
- Object oriented way of attributing a method to a variable
Method References
Examples
class Person {
private String name;
private int age;
- :: operator
Method References
Method References
More Examples
Consumer<Integer> b1 = System::exit;
Consumer<String[]> b2 = Arrays::sort;
Consumer<String> b3 = MyProgram::main;
Runnable r = MyProgram::main;
Default Methods
Add default behaviours to interfaces
There's a problem
The forEach method isn’t declared by java.util.List nor the
java.util.Collection interface because doing so would break existing
implementations.
Default Methods
We have lambdas, but we can't force new behaviours into the current
libraries.
Solution: default methods.
Default Methods
Examples
Example 1
Basics
public interface A {
default void foo() {
System.out.println("Calling A.foo()");
}
}
Client code
Output
"Calling A.foo()"
Example 2
Getting messy
public interface A {
public interface B {
}
public class Clazz implements A, B {
}
@FunctionalInterface
public interface Iterable {
Iterator iterator();
default void forEach(Consumer<? super T> action) {
Objects.requireNonNull(action);
for (T t : this) {
action.accept(t);
}
}
}
This is also valid for Sets and Queues, for example, since both classes
implement the Iterable interface.
Summary
Accumulators
Scalable updatable variables
Accumulators
The continual evolution of uses of concurrency and parallelism in
applications requires continual evolution in library support. For this
purpose, the Accumulators were introduced.
Accumulators
Maintaining a single count, sum, etc., that is updated by possibly many
threads is a common scalability problem.
A set of new classes were created for that purpose:
DoubleAccumulator
DoubleAdder
LongAccumulator
LongAdder
Accumulators
Package java.util.concurrent mechanisms synchronized operations
between threads, however if all you want to do is increment a variable
across threads, it was overkill and then some.
These classes are usually preferable to alternatives when multiple threads
update a common value that is used for purposes such as summary
statistics that are frequently updated but less frequently read.
Accumulators
Both the DoubleAdder and LongAdder classes can be seen as specific
subsets of the DoubleAccumulator and LongAccumulator functionality.
The call new DoubleAdder() is equivalent to:
new DoubleAccumulator((x, y) -> x + y, 0.0).
The call new LongAdder() is equivalent to:
new LongAccumulator((x, y) -> x + y, 0L).
Examples
Output:
Result: 20
LongAdder la = new LongAdder();
List<Long> longs = Arrays.asList(10L, 20L, 30L, 40L, 100L);
longs.forEach(la::add);
Output:
Result: 200