Repository navigation
Expand file tree
/
Copy pathExample.java
More file actions
176 lines (144 loc) · 6.02 KB
/
Copy pathExample.java
File metadata and controls
176 lines (144 loc) · 6.02 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package lambda;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.function.Supplier;
public class Example {
static int var1 = 10;
public static void example1() {
System.out.println("Example1");
Button button = new Button("Click me!");
// запись в стиле анонимного класса с функциональным интерфейсом
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Pressed");
}
});
int i = 10; // внешние переменный захватываются лямбдой
// ограничения в Java: переменная должна быть immutable (final)
var1 = 20; // меняется только значение по ссылке, но не сама переменная
AtomicInteger atomicInteger = new AtomicInteger(10);
atomicInteger.set(20); // внутреннее свойство можно менять
button.addActionListener(e -> JOptionPane.showMessageDialog(null, "Pressed" + i));
button.addActionListener(e -> JOptionPane.showMessageDialog(null, "Pressed" + var1));
button.addActionListener(e -> JOptionPane.showMessageDialog(null, "Pressed" + atomicInteger.toString()));
}
@FunctionalInterface
interface FuncInterface <T1, T2, R> {
R apply(T1 arg1, T2 arg2);
}
@FunctionalInterface
interface FuncInterface1 <T1, R> {
R apply(T1 arg1);
}
public static void example2()
{
System.out.println("Example2");
FuncInterface<Integer, Integer, Integer> function = (arg1, arg2) -> arg1 + arg2;
System.out.println(function.apply(1, 5));
}
public static void example3()
{
System.out.println("Example3");
// реализация функционального интерфеса может быть заменена ссылкой на функцию с такой же сигнатурой метода
FuncInterface<Integer, Integer, Integer> myFunction = Integer::sum;
System.out.println(myFunction.apply(3, 5));
FuncInterface1<Integer, Integer> myFunction1 = Integer::reverse;
System.out.println(myFunction1.apply(1));
}
public static void example4()
{
System.out.println("Example4");
ArrayList<Client> clients = new ArrayList<>();
clients.add(new Client("Igor", 5, true));
clients.add(new Client("Vasya", 10, true));
clients.add(new Client("Petya", 20, false));
clients.add(new Client("Masha", 30, true));
clients.add(new Client("Liza", 40, false));
// уменьшение баланса в цикле
for (Client client: clients) {
if (client.getActive()) {
client.setSum(client.getSum() - 1);
System.out.println(client.getSum());
}
}
System.out.println();
// уменьшение баланса с помощью лямбды
clients.forEach(client -> {
client.setSum(client.getSum() - 1);
System.out.println(client.getSum());
});
System.out.println();
// подсчет суммы элементов с одновременной фильтрацией
int balance = clients.stream()
.filter(c -> c.getSum() > 7) // с балансом больше 7
.filter(Client::getActive) // только активные
.reduce(0, ((integer, client) -> integer + client.getSum()), Integer::sum);
System.out.println("balance = " + balance);
}
public static void example5()
{
Client cl = new Client("Igor", 5, true);
// добавляем приветствие к имени, пользуясь ссылками на методы
addHello(cl::getName, cl::setName);
System.out.println(cl.getName());
}
/**
* Передаются ссылки на методы
* @param sup ссылка на метод получения имени клиента
* @param cons ссылка на метод присвоения имени клиенту
*/
public static void addHello(Supplier<String> sup, Consumer<String> cons)
{
cons.accept(String.format("Hello, %s !", sup.get()));
}
/**
* Проверяем, что объект не null и только после этого выводим его имя
* Таким образом избегаем NullPointerException
*/
public static void example6()
{
Client client1 = null;
Client client2 = new Client("Igor", 5, true);
chechForNull(client1, c -> {
System.out.println(client1.getName());
});
chechForNull(client2, c -> {
System.out.println(client2.getName());
});
}
public static <T> void chechForNull(T param, Consumer<T> cons)
{
if (param != null) {
cons.accept(param);
}
}
public static void example7()
{
Client client1 = new Client("Igor", 5, true);
ManagerClient managerClient = new ManagerClient(client1);
managerClient.change(c -> c.setName("New value"));
}
static class ManagerClient
{
private final Client client;
public ManagerClient(Client client) {
this.client = client;
}
public void change(Consumer<Client> consumer)
{
System.out.println("Вызов основного метода");
consumer.accept(client);
updateLog();
}
public void updateLog()
{
System.out.println("Вызов дополнительного метода");
}
}
}