-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLambdaTest.java
More file actions
79 lines (67 loc) · 2.91 KB
/
LambdaTest.java
File metadata and controls
79 lines (67 loc) · 2.91 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package Java8;
import java.util.Arrays;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class LambdaTest {
public static void main(String[] args) {
//Prior Java 8 :
List features = Arrays.asList("Lambdas", "Default Method",
"Stream API", "Date and Time API");
for (Object f : features) {
System.out.println(f);
}
//In Java 8:
features.forEach(n -> System.out.println(n));
// List languages = Arrays.asList("Java", "Scala", "C++", "Haskell", "Lisp");
//
// System.out.println("Languages which starts with J :");
// filter(languages, (str)->str.startsWith("J"));
//
// System.out.println("Languages which ends with a ");
// filter(languages, (str)->str.endsWith("a"));
//
// System.out.println("Print all languages :");
// filter(languages, (str)->true);
//
// System.out.println("Print no language : ");
// filter(languages, (str)->false);
//
// System.out.println("Print language whose length greater than 4:");
// filter(languages, (str)->str.length() > 4);
List<Integer> costBeforeTax = Arrays.asList(100, 200, 300, 400, 500);
for (Integer cost : costBeforeTax) {
double price = cost + .12 * cost;
System.out.println(price);
}
// With Lambda expression:
costBeforeTax.stream().map((cost) -> cost + .12 * cost)
.forEach(System.out::println);
List<String> G7 = Arrays.asList("USA", "Japan", "France", "Germany",
"Italy", "U.K.", "Canada");
String G7Countries = G7.stream().map(x -> x.toUpperCase())
.collect(Collectors.joining(", "));
System.out.println(G7Countries);
// Create List of square of all distinct numbers
List<Integer> numbers = Arrays.asList(9, 10, 3, 4, 7, 3, 4);
List<Integer> distinct = numbers.stream().map(i -> i * i).distinct()
.collect(Collectors.toList());
System.out.printf("Original List : %s, Square Without duplicates :"
+ "%s %n", numbers, distinct);
List<Integer> primes = Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19, 23, 29);
IntSummaryStatistics stats = primes.stream().mapToInt((n) -> n)
.summaryStatistics();
System.out.println("Highest prime number in List : " + stats.getMax());
System.out.println("Lowest prime number in List : " + stats.getMin());
System.out.println("Sum of all prime numbers : " + stats.getSum());
System.out.println("Average of all prime numbers : " + stats.getAverage());
}
public static void filter(List names, Predicate condition) {
for (Object name : names) {
if (condition.test(name)) {
System.out.println(name + " ");
}
}
}
}