-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctionalinterface.html
More file actions
234 lines (206 loc) · 8.17 KB
/
Copy pathfunctionalinterface.html
File metadata and controls
234 lines (206 loc) · 8.17 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Functional Interface - Quick Revision Guide</title>
<style>
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
font-family: "Segoe UI", Arial, sans-serif;
background: #f5f7fb;
color: #1f2937;
line-height: 1.6;
padding: 24px;
}
.container {
max-width: 1100px;
margin: 0 auto;
background: #fff;
border-radius: 14px;
box-shadow: 0 6px 20px rgba(0,0,0,0.08);
overflow: hidden;
}
header {
background: linear-gradient(135deg, #1d4ed8, #2563eb);
color: #fff;
padding: 28px;
}
header h1 { font-size: 1.9rem; margin-bottom: 8px; }
header p { opacity: 0.95; }
main { padding: 24px; }
section {
margin-bottom: 20px;
padding: 18px;
border: 1px solid #e5e7eb;
border-radius: 10px;
background: #fff;
}
section h2 { color: #1d4ed8; margin-bottom: 10px; font-size: 1.3rem; }
section h3 { margin: 12px 0 8px; color: #111827; }
p { margin-bottom: 10px; }
ul, ol { margin-left: 20px; }
li { margin-bottom: 8px; }
.analogy-box, .problem-box, .solution-box, .usage-example {
border-left: 4px solid #2563eb;
background: #eff6ff;
padding: 12px;
border-radius: 8px;
margin-top: 10px;
}
.problem-box { border-left-color: #d97706; background: #fff7ed; }
.solution-box { border-left-color: #059669; background: #ecfdf5; }
code {
padding: 2px 6px;
border-radius: 6px;
font-family: Consolas, "Courier New", monospace;
font-size: 0.92rem;
}
pre {
margin-top: 10px;
background: #111827;
color: #e5e7eb;
padding: 14px;
border-radius: 10px;
overflow-x: auto;
font-size: 0.88rem;
}
.highlight { background: #fef3c7; padding: 2px 4px; border-radius: 5px; }
footer {
text-align: center;
color: #6b7280;
padding: 16px;
border-top: 1px solid #e5e7eb;
font-size: 0.9rem;
}
.icon { margin-right: 6px; }
@media (max-width: 768px) {
body { padding: 12px; }
header h1 { font-size: 1.45rem; }
main { padding: 14px; }
}
</style>
</head>
<body>
<div class="container">
<header>
<h1>Java Functional Interfaces - Real-World Revision Guide</h1>
<p>SAM, lambdas, built-in interfaces, and custom domain interfaces.</p>
</header>
<main>
<section>
<h2><span class="icon">🧠</span>Core Concepts</h2>
<ul>
<li><strong>Functional Interface:</strong> Exactly <span class="highlight">one abstract method</span> (SAM).</li>
<li><strong>@FunctionalInterface:</strong> Enforces SAM at compile-time and improves readability.</li>
<li><strong>Can still have:</strong> Multiple <code>default</code> and <code>static</code> methods.</li>
<li><strong>Main benefit:</strong> Enables passing behavior as lambda expressions.</li>
</ul>
</section>
<section>
<h2><span class="icon">❓</span>Why Not Classes?</h2>
<div class="solution-box">
<p><code>@FunctionalInterface</code> applies only to interfaces. Classes represent structure/state, while functional interfaces define behavior contracts that lambdas implement dynamically.</p>
</div>
</section>
<section>
<h2><span class="icon">📦</span>Example 1: Built-in Interface (<code>Function<T, R></code>)</h2>
<p><strong>Use case:</strong> Convert <code>UserEntity</code> (DB model) into <code>UserDTO</code> (API-safe model).</p>
<p><strong>Key method:</strong> <code>R apply(T t)</code> — called directly or by <code>Stream.map()</code> internally.</p>
<pre><code>import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
class UserEntity {
private final Long id;
private final String firstName;
private final String lastName;
private final String email;
private final String passwordHash;
public UserEntity(Long id, String firstName, String lastName, String email, String passwordHash) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.passwordHash = passwordHash;
}
public Long getId() { return id; }
public String getFirstName() { return firstName; }
public String getLastName() { return lastName; }
public String getEmail() { return email; }
}
class UserDTO {
private final Long id;
private final String fullName;
private final String email;
public UserDTO(Long id, String fullName, String email) {
this.id = id;
this.fullName = fullName;
this.email = email;
}
@Override
public String toString() {
return "UserDTO{id=" + id + ", fullName='" + fullName + "', email='" + email + "'}";
}
}
public class DtoMappingExample {
private static final Function<UserEntity, UserDTO> convertToDTO = entity ->
new UserDTO(
entity.getId(),
entity.getFirstName() + " " + entity.getLastName(),
entity.getEmail().toLowerCase()
);
public static void main(String[] args) {
UserEntity singleDbUser = new UserEntity(101L, "Alice", "Wonderland", "ALICE@example.com", "$2a$12$pPlk8..");
UserDTO singleUserDTO = convertToDTO.apply(singleDbUser);
System.out.println(singleUserDTO);
List<UserEntity> databaseUsers = Arrays.asList(
new UserEntity(1L, "John", "Doe", "John.Doe@example.com", "$2a$12$eImiTx.."),
new UserEntity(2L, "Jane", "Smith", "JANE.SMITH@example.com", "$2a$12$Kj9xY..")
);
List<UserDTO> apiResponse = databaseUsers.stream()
.map(convertToDTO)
.collect(Collectors.toList());
apiResponse.forEach(System.out::println);
}
}</code></pre>
</section>
<section>
<h2><span class="icon">🧩</span>Example 2: Custom Functional Interface (Domain Logic)</h2>
<p><strong>Use case:</strong> Credential validation with explicit business intent.</p>
<pre><code>@FunctionalInterface
public interface IdentityValidator {
boolean validate(String username, String password);
}
public class AuthenticationRegistry {
public void executeValidation() {
IdentityValidator prodCheck = (user, pass) ->
user.startsWith("admin_") && pass.length() > 12;
boolean result = prodCheck.validate("admin_root", "SecurePassword123!");
System.out.println("Authentication validation passing status: " + result);
}
public static void main(String[] args) {
new AuthenticationRegistry().executeValidation();
}
}</code></pre>
<div class="analogy-box">
<strong>Key takeaway:</strong> <code>IdentityValidator</code> is clearer and more domain-driven than generic <code>BiPredicate<String, String></code>.
</div>
</section>
<section>
<h2><span class="icon">✅</span>Quick Revision Checklist</h2>
<ul>
<li>SAM = one abstract method only.</li>
<li>Prefer <code>@FunctionalInterface</code> for compiler checks.</li>
<li>Use built-ins for common transformations/filtering.</li>
<li>Create custom interfaces when business meaning matters.</li>
<li>Lambdas make code concise, composable, and test-friendly.</li>
</ul>
</section>
</main>
<footer>
Functional Interface Revision Page • Java
</footer>
</div>
</body>
</html>