· 6 years ago · May 22, 2019, 02:38 PM
1[5/22/2019 1:35:06 PM] Volodymyr Dudas: ready
2
3[5/22/2019 1:38:46 PM] Anton Andreev: OK, let's start with a simple question. In Java we often need to compare two objects to find out whether they are equal or not (using the equals method defined in java.lang.Object). For some types of objects the equality semantics may be pretty straightforward (take java.lang.String for example) but some other types may be less obvious. Can you please tell me in which case two lists (java.util.List) are considered to be equal? What about sets (java.util.Set)?
4
5[5/22/2019 1:43:24 PM] Volodymyr Dudas: Ok.
6
7[5/22/2019 1:44:05 PM] Volodymyr Dudas: I think it's obvious that Lists are equals if their elements equal
8
9[5/22/2019 1:44:41 PM] Volodymyr Dudas: by index. meaning first element of first list equals first element of second list
10
11[5/22/2019 1:45:22 PM] Volodymyr Dudas: For sets it's a bit different
12
13[5/22/2019 1:45:33 PM] Volodymyr Dudas: Set has unique items
14
15[5/22/2019 1:45:50 PM] Volodymyr Dudas: So for comparison we should not compare items in order
16
17[5/22/2019 1:46:16 PM] Volodymyr Dudas: We could just check if set2 contains all items from set 1
18
19[5/22/2019 1:46:35 PM] Volodymyr Dudas: that way we will be sure that both sets are equal
20
21[5/22/2019 1:46:54 PM] Anton Andreev: OK, makes sense
22
23[5/22/2019 1:47:32 PM] Anton Andreev: And what would be the time complexity (in big O notation) for such equals implementations for Lists/Sets?
24
25[5/22/2019 1:48:35 PM] Volodymyr Dudas: For list we need to go through whole list
26meaning we should check if list1.size == list2. size first
27
28[5/22/2019 1:48:57 PM] Volodymyr Dudas: so complexity is gonna be O(list1.size) if sizes equal
29
30[5/22/2019 1:49:46 PM] Volodymyr Dudas: For set we could go through whole set too but we should look for first fail.
31
32[5/22/2019 1:50:13 PM] Volodymyr Dudas: I think it's also gonna be O(n) is worse case
33
34[5/22/2019 1:50:46 PM] Volodymyr Dudas: this is true for set too
35
36[5/22/2019 1:51:21 PM] Volodymyr Dudas: to be honest I'm not sure here
37
38[5/22/2019 1:51:27 PM] Volodymyr Dudas: about right complexity
39
40[5/22/2019 1:53:09 PM] Anton Andreev: This is the correct path to follow (after performing null checks and comparing the sizes, of course). So you would iterate through all elements of one set and attempt to look them up in the second set. I'll let you think a bit more about the overall complexity.
41
42[5/22/2019 1:54:22 PM] Volodymyr Dudas: hmmm
43
44[5/22/2019 1:55:28 PM] Volodymyr Dudas: anyway
45
46[5/22/2019 1:55:55 PM] Volodymyr Dudas: we will have foreach from first set and inside set2.contains(element)
47
48[5/22/2019 1:56:05 PM] Volodymyr Dudas: so each iteration is O(1)
49
50[5/22/2019 1:56:44 PM] Anton Andreev: Why O(1)?
51
52[5/22/2019 1:56:45 PM] Volodymyr Dudas: we have N repeats
53So it's O(N) in worse case
54I'd like to submit this answer
55
56[5/22/2019 1:57:01 PM] Volodymyr Dudas: one comparison is O(1)
57
58[5/22/2019 1:57:06 PM] Volodymyr Dudas: answer is O(N)
59
60[5/22/2019 1:59:23 PM] Anton Andreev: OK, got it. It actually depends on the type of the set. O(1) is true for HashSets but not necessary true for other implementations.
61
62[5/22/2019 1:59:46 PM] Anton Andreev: OK, next question. How do you execute something asynchronously in Java? Let's say a method like this:
63
64public int longComputation() throws Exception {
65return service.doSomeLongComputation();
66}
67
68
69
70[5/22/2019 2:00:00 PM] Volodymyr Dudas: got it. tree set is gonna be different bacause of it's architecture
71
72[5/22/2019 2:00:40 PM] Volodymyr Dudas: I do ExcecutorService and fixed tread pool
73
74[5/22/2019 2:00:53 PM] Volodymyr Dudas: in this case we have return param
75
76[5/22/2019 2:01:24 PM] Volodymyr Dudas: So I'd use Callable<Integer> as return type and "subscribe" for that
77
78[5/22/2019 2:02:03 PM] Volodymyr Dudas: Executors.newFixedThreadPool
79
80[5/22/2019 2:02:38 PM] Anton Andreev: OK, a couple follow-up questions
81
82[5/22/2019 2:02:43 PM] Volodymyr Dudas: ok
83
84[5/22/2019 2:02:56 PM] Anton Andreev: Why a fixed thread pool? And what other types of thread pools are there?
85
86[5/22/2019 2:03:09 PM] Anton Andreev: How do you do that? (subscribe)
87
88[5/22/2019 2:04:17 PM] Volodymyr Dudas: I've been using fixed in most of cases to limit numbers of possible active threads working same time.
89
90[5/22/2019 2:04:26 PM] Volodymyr Dudas: For managing resources
91
92[5/22/2019 2:05:36 PM] Volodymyr Dudas: There are Work stealing, Single thread executor, cached theread pool, scheduled etc
93
94[5/22/2019 2:06:10 PM] Volodymyr Dudas: using callbacks
95
96[5/22/2019 2:06:26 PM] Anton Andreev: Can you provide a simple code example?
97
98[5/22/2019 2:06:52 PM] Volodymyr Dudas: sure
99
100[5/22/2019 2:10:44 PM] Volodymyr Dudas: Callable<Integer> callableTask = () -> {
101TimeUnit.MILLISECONDS.sleep(300);
102return 1
103};
104
105List<Callable<Integer>> callableTasks = new ArrayList<>();
106callableTasks.add(callableTask);
107
108List<Future<Integer>> futures = executorService.invokeAll(callableTasks);
109
110executorService.shutdown();
111
112[5/22/2019 2:11:43 PM] Volodymyr Dudas: Sorry. My bad. I have to check all Future objects and get results
113
114[5/22/2019 2:12:13 PM] Volodymyr Dudas: Like that
115for(Future<Integer> future: futures) {
116System.out.println(future.get());
117}
118
119[5/22/2019 2:12:27 PM] Anton Andreev: OK, I get the idea
120
121[5/22/2019 2:12:32 PM] Anton Andreev: Let's move on
122
123[5/22/2019 2:12:37 PM] Volodymyr Dudas: sure
124
125[5/22/2019 2:12:43 PM] Anton Andreev: I assume you know Java Streams (you definitely should). Let's say we have such a class:
126
127class Car {
128String make;
129String model;
130... // a bunch of other irrelevant stuff that no one cares about
131String color; // BLACK, WHITE, etc.
132}
133
134
135And we have a list of cars:
136
137List<Car> cars = .... // initialized somehow somewhere
138
139
140How do we find out if the list contains a red car (color.equals("RED")) using java streams? Please note that I expect a boolean result.
141
142[5/22/2019 2:14:49 PM] Volodymyr Dudas: definitely I need to use filter
143
144[5/22/2019 2:15:09 PM] Volodymyr Dudas: one second. I think how to return boolean
145
146[5/22/2019 2:15:18 PM] Volodymyr Dudas: I think findFirst()
147
148[5/22/2019 2:15:27 PM] Volodymyr Dudas: and present function of Optional
149
150[5/22/2019 2:16:10 PM] Volodymyr Dudas: cars.stream().filter(c -> c.color.equals("RED")).findFirst().isPresent()
151
152[5/22/2019 2:16:20 PM] Volodymyr Dudas: should be ok
153
154[5/22/2019 2:16:45 PM] Anton Andreev: that, this will work
155
156[5/22/2019 2:17:15 PM] Anton Andreev: There's also findAny method. How is it different from findFirst()?
157
158[5/22/2019 2:18:23 PM] Volodymyr Dudas: the difference is quite small
159
160[5/22/2019 2:18:50 PM] Volodymyr Dudas: they both return Optional<T> or empty if stream empty
161
162[5/22/2019 2:19:35 PM] Volodymyr Dudas: first return first occurrence. any element could be returned if the stream has no encounter order
163
164[5/22/2019 2:19:57 PM] Volodymyr Dudas: any returns some nondeterministic element
165
166[5/22/2019 2:20:24 PM] Volodymyr Dudas: so any allows parallel handling
167
168[5/22/2019 2:20:43 PM] Volodymyr Dudas: because you don't mind about order or first occurrence
169
170[5/22/2019 2:21:05 PM] Volodymyr Dudas: any - means - any found
171first - first found in order as stream is
172
173[5/22/2019 2:21:37 PM] Anton Andreev: right
174
175[5/22/2019 2:22:11 PM] Anton Andreev: OK, since you mentioned Optional. Can you tell me about it in a few words? (what is it, how is it used)
176
177[5/22/2019 2:22:26 PM] Volodymyr Dudas: sure
178
179[5/22/2019 2:22:43 PM] Volodymyr Dudas: Optional is a wrapper for object which we are interested in
180
181[5/22/2019 2:23:01 PM] Volodymyr Dudas: General approach - proxy pattern (from GoF)
182
183[5/22/2019 2:23:41 PM] Volodymyr Dudas: so it contains T value which is exact value and static final EMPTY constant in case is value is null
184
185[5/22/2019 2:24:17 PM] Volodymyr Dudas: so isPresent returns if value not equals null
186
187[5/22/2019 2:25:22 PM] Volodymyr Dudas: also it has some specific functions like orElse, orElseThrow to handle default cases if value is null
188
189[5/22/2019 2:26:04 PM] Volodymyr Dudas: to get value we should use get() method which will throw exception is case if value is not present
190
191[5/22/2019 2:26:11 PM] Anton Andreev: OK
192
193[5/22/2019 2:26:13 PM] Anton Andreev: Bonus question: How would you rewrite the following code using Optional?
194
195int limit = 10; // default value
196if (queryParameters != null && queryParameters.getLimit() != null) {
197limit = queryParameters.getLimit();
198}
199
200
201(let's assume that queryParameters is an object of QueryParameters class which has getLimit() method returning Integer)
202
203[5/22/2019 2:26:42 PM] Volodymyr Dudas: one second
204
205[5/22/2019 2:29:22 PM] Anton Andreev: sure
206
207[5/22/2019 2:30:29 PM] Volodymyr Dudas: limit = Optional.of(queryParameters)
208.ifPresent(value -> Optional.of(value.getLimit())
209.orElse(10).ifPresent(value.getLimit()));
210
211[5/22/2019 2:30:37 PM] Volodymyr Dudas: somethg like that
212
213[5/22/2019 2:31:41 PM] Volodymyr Dudas: ooo. last ifPresent is not needed
214
215[5/22/2019 2:31:56 PM] Volodymyr Dudas: limit = Optional.of(queryParameters)
216.ifPresent(value -> Optional.of(value.getLimit())
217.orElse(10));
218
219[5/22/2019 2:33:46 PM] Anton Andreev: Looks kind of similar to what I expected but not quite it
220
221[5/22/2019 2:33:54 PM] Anton Andreev: OK, last question. How's your SQL?
222
223[5/22/2019 2:34:05 PM] Volodymyr Dudas: fine
224
225[5/22/2019 2:34:40 PM] Anton Andreev: Let's say we have two database tables and a foreign key between them:
226
227CREATE TABLE rooms(
228id INT NOT NULL PRIMARY KEY,
229name VARCHAR(50) NOT NULL
230);
231
232CREATE TABLE messages(
233id INT NOT NULL PRIMARY KEY,
234room_id INT NOT NULL,
235message_text VARCHAR(255) NOT NULL,
236created LONG NOT NULL
237);
238
239ALTER TABLE messages
240ADD FOREIGN KEY (room_id) REFERENCES rooms(id);
241
242
243Having a room ID, how do you fetch the corresponding room (id and name) along with the most recent message in that room, if it exists?
244
245[5/22/2019 2:35:00 PM] Volodymyr Dudas: one sec
246
247[5/22/2019 2:41:06 PM] Volodymyr Dudas: Select
248r.id, r.name, m.message_text, m.created
249from rooms r join messages m on r.id=m.room_id
250where r.id=<ID> ORDER BY m.created DESC LIMIT 1
251
252[5/22/2019 2:41:27 PM] Volodymyr Dudas: I think so
253I did not check
254
255[5/22/2019 2:42:24 PM] Anton Andreev: Almost
256
257[5/22/2019 2:42:55 PM] Anton Andreev: The only problem is that it won't return anything at all if a room doesn't have any messages
258
259[5/22/2019 2:43:20 PM] Volodymyr Dudas: I see
260
261[5/22/2019 2:43:28 PM] Volodymyr Dudas: I probably need subquery
262
263[5/22/2019 2:43:49 PM] Anton Andreev: You actually just need to add a single word
264
265[5/22/2019 2:44:23 PM] Volodymyr Dudas: left join
266
267[5/22/2019 2:44:50 PM] Anton Andreev: :thumbsup:
268
269[5/22/2019 2:45:00 PM] Volodymyr Dudas: :grinning: