Skip to content

Commit a3745b9

Browse files
committed
1 parent 6651b75 commit a3745b9

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* #%L
3+
* lambda-tutorial
4+
* %%
5+
* Copyright (C) 2013 Adopt OpenJDK
6+
* %%
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as
9+
* published by the Free Software Foundation, either version 2 of the
10+
* License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public
18+
* License along with this program. If not, see
19+
* <http://www.gnu.org/licenses/gpl-2.0.html>.
20+
* #L%
21+
*/
22+
23+
package org.jcrete.lambdas.examples;
24+
25+
import java.util.Arrays;
26+
import java.util.Comparator;
27+
import java.util.List;
28+
import java.util.Optional;
29+
import java.util.stream.Collectors;
30+
31+
/**
32+
* @see {@link "http://www.functionaljava.org" }
33+
*/
34+
public class FunctionalJavaExamples {
35+
private static final List<Integer> ints = Arrays.asList(97, 44, 67, 3, 22, 90, 1, 77, 98, 1078, 6, 64, 6, 79, 42);
36+
private static final List<String> strings = Arrays.asList("Hello", "There", "what", "DAY", "iS", "iT");
37+
38+
/**
39+
* @return a list of lowercase only strings.
40+
*/
41+
private static List<String> lowerCaseOnly(List<String> list) {
42+
return list.stream().filter(s -> s.matches("\\p{javaLowerCase}*")).collect(Collectors.toList());
43+
}
44+
45+
/**
46+
* Removes elements from an array that do not meet a certain criteria.
47+
*
48+
* @param list
49+
*/
50+
private static List<Integer> removeAll(List<Integer> list) {
51+
return list.stream().filter(i -> i % 2 == 0).collect(Collectors.toList());
52+
}
53+
54+
/**
55+
* Removes elements from an array that do not meet a certain criteria.
56+
*
57+
* @param list
58+
*/
59+
private static int foldLeft(List<Integer> list) {
60+
return list.stream().reduce(0, (x, y) -> x + y);
61+
}
62+
63+
64+
/**
65+
* Maps a function across the array of integers. Adds 42 to each element of the array to produce a new array.
66+
*
67+
* @param list
68+
*/
69+
private static List<Integer> map42(List<Integer> list) {
70+
return list.stream().map(i -> i + 42).collect(Collectors.toList());
71+
}
72+
73+
74+
/**
75+
* Sorts the given list by producing a new list using a merge-sort algorithm.
76+
*
77+
* @param list
78+
*/
79+
private static List<Integer> sort(List<Integer> list) {
80+
list.sort(Comparator.<Integer>naturalOrder());
81+
return list;
82+
}
83+
84+
/**
85+
* Binds a function across the optional value type.
86+
* The function checks if the contained value is even and if it is multiples that value by 3 and returns that new value.
87+
* If the contained value is odd (or if there is no value), then no value is returned (none).
88+
*
89+
* @param list
90+
*/
91+
private static void option(List<Optional<Integer>> list) {
92+
for (Optional<Integer> o : list) {
93+
o.filter(i -> i % 2 == 0).map(i -> i * 3).ifPresent(System.out::print);
94+
}
95+
}
96+
97+
public static void main(String[] args) {
98+
System.out.println(lowerCaseOnly(strings));
99+
System.out.println(removeAll(ints));
100+
System.out.println(foldLeft(ints));
101+
System.out.println(map42(ints));
102+
System.out.println(sort(ints));
103+
List<Optional<Integer>> optionals = Arrays.asList(Optional.<Integer>of(7), Optional.<Integer>of(8), Optional.<Integer>empty());
104+
option(optionals); // output: 8 x 3 = 24
105+
}
106+
107+
}

0 commit comments

Comments
 (0)