-
Notifications
You must be signed in to change notification settings - Fork 0
/
JavaNotes
65 lines (46 loc) · 2.9 KB
/
JavaNotes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
Modern Java in Action Lambdas, streams, functional and reactive programmin
java previus version
java 1.7
invokedynamic?
java 1.8
- improve in the creaton of comparator
before
Collections.sort(inventory, new Comparator<Apple>() {
public int compare(Apple a1, Apple a2){
return a1.getWeight().compareTo(a2.getWeight());
}
});
current
inventory.sort(comparing(Apple::getWeight));
tip don use synchronized error-prone
before none
after default methods in interfaces.
before alot fors
after functional-style programming
programming concepts
tip stream Processing was inspired cat file1 file2 | tr "[A-Z]" "[a-z]" | sort | tail -
tip Passing code to methods with behavior parameterization
a oportunuty for create a components with mix paradigms funtional and oriented ojects
Java 8 adds functions as new forms of value
Java 8 provides to exploit parallel programming on multicore processors
method references.
remove verbose wraperss for improve onelinecode
In Java 8 you can pass the isHidden function to the listFiles method using the method reference :: syntax.
static <T> Collection<T> filter(Collection<T> c, Predicate<T> p);
Java 8 by default methods.55
an interface can now contain method signatures for which an implementing class doesn’t provide an implementation
Java has mechanisms called anonymous classes, which let you declare and instantiate a class at the same time. They enable
you to improve your code one step further by making it a little more concise.
now also have default methods (a method with a body that provides some default implementation for a
method in case it isn’t implemented by a class). An interface is still a functional interface if it has many default methods as long as it specifies only one
abstract method.
Using functional interfaces
Predicate ==> accepts an object of generic type T and returns a boolean test()
Consumer ==> accept that takes an object of generic type T and returns no result (void). accepts()
Function ==> apply that takes an object of generic type T as input and returns an object of generic type R. apply()
Java 8 code: method references. Think of them as shorthand versions of certain lambdas
Keep in mind that you can consume a stream only once!
When to use findFirst and findAny
You may wonder why we have both findFirst and findAny. The answer is parallelism. Finding the first element is more constraining in parallel. If you don’t care about
which element is returned, use findAny because it’s less constraining when using
parallel streams.