Repository navigation
Expand file tree
/
Copy pathTechnicalInterviewTask.java
More file actions
281 lines (242 loc) · 12.1 KB
/
Copy pathTechnicalInterviewTask.java
File metadata and controls
281 lines (242 loc) · 12.1 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package com.tech;
import java.util.*;
import java.util.stream.Collectors;
/**
* Существует документ прописки и выписки по месту пребывания.
* Документ состоит из полей (documentId, personId, addressId, type).
* Документ прописки по месту пребывания имеет тип 6.
* Документ выписки с места жительства имеет тип 13.
*
* Документ выписки отменяет документ прописки, если у них personId и addressId совпадают друг с другом.
* На вход метода передается список документов и список documentId для фильтрации выходных данных.
*
* Метод должен выдавать только тех, кто остался зарегистрирован для переданных documentId.
*
* Пример передаваемых данных
* Список справок:
* (1, 1, 1, 3), (2, 1, 1, 11), (3, 1, 2, 3), (4, 2, 1, 3), (5, 3, 1, 3)
* Список documentId: 1, 2, 3, 5
* На выходе должны получить
* (3, 1, 2, 3), (5, 3, 1, 3)
*/
public class TechnicalInterviewTask {
private final static Long IN = 6L;
private final static Long OUT = 13L;
public static void main(String[] args) {
// условия задачи
ArrayList<RegistrationAction> rList = new ArrayList<>(Arrays.asList(
new RegistrationAction(1L, 1L, 1L, 3L, 0),
new RegistrationAction(2L, 1L, 1L, 11L, 0),
new RegistrationAction(3L, 1L, 2L, 3L, 0),
new RegistrationAction(4L, 2L, 1L, 3L, 0),
new RegistrationAction(5L, 3L, 1L, 3L, 0)
));
// передаваемые данные по условию
Long[] documentsIds = new Long[4];
documentsIds[0] = 1L;
documentsIds[1] = 2L;
documentsIds[2] = 3L;
documentsIds[3] = 5L;
// решение с циклами
var result = getResult(rList, documentsIds);
System.out.println(result);
// решение через Stream API
var result1 = getResultStreamApi(rList, documentsIds);
System.out.println(result1);
// оптимизированное решение через Stream API
var result2 = getResultStreamApiOptimized(rList, documentsIds);
System.out.println(result2);
// Ещё более короткий вариант на стримах
var result3 = getResultStreamApiOptimized(rList, documentsIds);
System.out.println(result3);
}
public static List<RegistrationAction> getResult(ArrayList<RegistrationAction> list, Long[] documentId) {
HashMap<String, RegistrationAction> processedKeys = new HashMap<>();
Set<Long> docIds = new HashSet<>(Arrays.asList(documentId));
for (int i = 0; i < list.size(); i++) {
Long curDocId = list.get(i).getDocumentId(); // documentId
Long curPersonId = list.get(i).getPersonId(); // personId
Long curAddressId = list.get(i).getAddressId(); // addressId
Long curType = list.get(i).getType(); // type
if (docIds.contains(curDocId)) { // тот документ, который нас интересует
// составной уникальный ключ
String key = "p" + curPersonId + "a" + curAddressId;
if (processedKeys.containsKey(key)) {
var tmp = processedKeys.get(key);
var resT = tmp.getResult();
if (curType.equals(IN)) {
// документ прописки увеличивает на 1 общий рейтинг
resT++;
} else if (curType.equals(OUT)) {
// документ выписки уменьшает на 1 общий рейтинг действия
resT--;
}
RegistrationAction newObj = new RegistrationAction(
curDocId,
curPersonId,
curAddressId,
curType,
resT // в параметр result собираем данные
);
processedKeys.put(key, newObj);
} else {
var resT = list.get(i).getResult();
if (curType.equals(IN)) {
resT++;
} else if (curType.equals(OUT)) {
resT--;
}
list.get(i).setResult(resT);
processedKeys.put(key, list.get(i));
}
}
}
var result = new ArrayList<RegistrationAction>();
for (Map.Entry<String, RegistrationAction> proc: processedKeys.entrySet()) {
if (proc.getValue().getResult() > 0) {
// оставляем только тех, кто в результате всех действий остался зарегистрирован
result.add(proc.getValue());
}
}
return result.stream().toList();
}
public static List<RegistrationAction> getResultStreamApi(ArrayList<RegistrationAction> list, Long[] documentId) {
// множество id документов, которое нас интересует
Set<Long> findedDocs = new HashSet<>(Arrays.asList(documentId));
return list.stream()
.filter( o -> findedDocs.contains(o.getDocumentId()))
.map(entry -> {
String key = "p" + entry.getPersonId() + "a" + entry.getAddressId();
var resT = 0;
if (IN.equals(entry.getType())) {
resT++;
} else if (OUT.equals(entry.getType())) {
resT--;
}
var value = new RegistrationAction(
entry.getDocumentId(),
entry.getPersonId(),
entry.getAddressId(),
entry.getType(),
resT
);
return Map.entry(key, value);
})
.collect(Collectors.groupingBy(
Map.Entry::getKey,
Collectors.collectingAndThen(
Collectors.reducing((obj1, obj2) -> {
obj1.getValue().setResult(
obj1.getValue().getResult() + obj2.getValue().getResult()
);
return obj1;
}),
Optional::get
))
)
.entrySet()
.stream()
.filter(o -> o.getValue().getValue().getResult() > 0)
.collect(
Collectors.toMap(
Map.Entry::getKey,
entry -> entry.getValue().getValue()
)
)
.entrySet()
.stream()
.map(Map.Entry::getValue)
.collect(Collectors.toList());
}
public static List<RegistrationAction> getResultStreamApiOptimized(ArrayList<RegistrationAction> list, Long[] documentId) {
Set<Long> findedDocs = new HashSet<>(Arrays.asList(documentId));
return list.stream()
.filter(entry -> findedDocs.contains(entry.getDocumentId()))
.collect(Collectors.groupingBy(
entry -> "p" + entry.getPersonId() + "a" + entry.getAddressId(),
Collectors.collectingAndThen(
Collectors.reducing((obj1, obj2) -> {
var result = obj1.getResult();
if (IN.equals(obj2.getType())) {
result++;
} else if (OUT.equals(obj2.getType())) {
result--;
}
obj1.setResult(result);
return obj1;
}),
Optional::get
)
))
.values()
.stream()
.filter(entry -> entry.getResult() > 0)
.collect(Collectors.toList());
}
public static List<RegistrationAction> getResultStreamApiOptimized1(ArrayList<RegistrationAction> list, Long[] documentId) {
Set<Long> findedDocs = new HashSet<>(Arrays.asList(documentId));
return list.stream()
.filter(entry -> findedDocs.contains(entry.getDocumentId())) // фильтруем только те документы, которые нужны
.collect(Collectors.toMap( // Создаем мапу, ключи которой строка с уникальным набором данных.
// ключ - признак отмены документа прописки документом выписки
entry -> "p" + entry.getPersonId() + "a" + entry.getAddressId(),
Function.identity(), // возвращает входные аргументы самой себя (маппер значений)
(obj1, obj2) -> { // функция, которая используется при мерже значений, если сопрадает ключ
var result = obj1.getResult();
if (IN.equals(obj2.getType())) { // прописка
result++;
} else if (OUT.equals(obj2.getType())) { // выписка
result--;
}
obj1.setResult(result);
return obj1;
}
)) // Map<String, RegistrationAction>
.values() // Collection<RegistrationAction> (уменьшили размерность с мапы до коллекции)
.stream() // Stream<RegistrationAction>
.filter(entry -> entry.getResult() > 0) // фильтруем только тех, кто остался прописан
.collect(Collectors.toList());
}
/**
* Объект "Регистрационное действие"
*/
static class RegistrationAction {
private final Long documentId;
private final Long personId;
private final Long addressId;
private final Long type;
/**
* Результирующий рейтинг действия
*/
private Long result;
public RegistrationAction(Long documentId, Long personId, Long addressId, Long type, Long result) {
this.documentId = documentId;
this.personId = personId;
this.addressId = addressId;
this.type = type;
this.result = result;
}
public Long getDocumentId() {
return documentId;
}
public Long getPersonId() {
return personId;
}
public Long getAddressId() {
return addressId;
}
public Long getType() {
return type;
}
public Long getResult() {
return result;
}
public void setResult(Long result) {
this.result = result;
}
@Override
public String toString() {
return "{" + this.documentId + ", " + this.personId + ", " + this.addressId + ", " + this.type + "}";
}
}
}