回答編集履歴

1

再訂正

2016/11/24 13:56

投稿

退会済みユーザー
test CHANGED
@@ -113,3 +113,249 @@
113
113
  }
114
114
 
115
115
  ```
116
+
117
+
118
+
119
+ 反射性等が問題になるという旨指摘がありましたので現在訂正した部分までを下記に記載しておきます
120
+
121
+ 再訂正
122
+
123
+ ```java
124
+
125
+
126
+
127
+     import java.util.*;
128
+
129
+
130
+
131
+ class Order{
132
+
133
+ public boolean equals(Object o){
134
+
135
+ if((o instanceof Food) && (((Food)o).id==this.id) && (((Food)o).name.equals(this.name))){
136
+
137
+ return true;
138
+
139
+
140
+
141
+ }
142
+
143
+ else if((o instanceof Order) && (o.hashCode()==this.hashCode())){
144
+
145
+ return true;
146
+
147
+
148
+
149
+ }
150
+
151
+ return false;
152
+
153
+ }
154
+
155
+ public int hashCode(){
156
+
157
+
158
+
159
+
160
+
161
+ char[] cA = name.toCharArray();
162
+
163
+
164
+
165
+ int ch=0;
166
+
167
+
168
+
169
+ for(char t:cA){
170
+
171
+ int g=(int)t;
172
+
173
+
174
+
175
+
176
+
177
+ ch+=g;
178
+
179
+
180
+
181
+ }
182
+
183
+
184
+
185
+
186
+
187
+ return id*1000+ch;
188
+
189
+
190
+
191
+ }
192
+
193
+
194
+
195
+ public int id;
196
+
197
+ public String name;
198
+
199
+ public Order(int id,String name){
200
+
201
+ this.id = id;
202
+
203
+ this.name = name;
204
+
205
+ }
206
+
207
+ }
208
+
209
+ class Food{
210
+
211
+
212
+
213
+
214
+
215
+ public int hashCode(){
216
+
217
+
218
+
219
+
220
+
221
+ char[] cA = name.toCharArray();
222
+
223
+
224
+
225
+ int ch=0;
226
+
227
+
228
+
229
+ for(char t:cA){
230
+
231
+
232
+
233
+ int g=(int)t;
234
+
235
+ ch+=g;
236
+
237
+
238
+
239
+ }
240
+
241
+
242
+
243
+
244
+
245
+ return id*1000+ch;
246
+
247
+
248
+
249
+ }
250
+
251
+
252
+
253
+
254
+
255
+
256
+
257
+ public int id;
258
+
259
+ public String name;
260
+
261
+ public Food(int id,String name){
262
+
263
+ this.id = id;
264
+
265
+ this.name = name;
266
+
267
+ }
268
+
269
+ }
270
+
271
+
272
+
273
+ public class App {
274
+
275
+ public static void main(String[] args) {
276
+
277
+
278
+
279
+ List<Order> orderList = new ArrayList<>();
280
+
281
+ List<Food> foodList = new ArrayList<>();
282
+
283
+
284
+
285
+ orderList.add(new Order(1,"アップル"));
286
+
287
+ orderList.add(new Order(2, "バナナ"));
288
+
289
+ orderList.add(new Order(3, "綿菓子"));
290
+
291
+
292
+
293
+ foodList.add(new Food(1,"アップル"));
294
+
295
+ foodList.add(new Food(2, "バナナ"));
296
+
297
+ for (int i2 = 0; i2 < orderList.size(); i2++) {
298
+
299
+
300
+
301
+ //比較してくれない
302
+
303
+ if(!(foodList.contains(orderList.get(i2)))){
304
+
305
+ System.out.println(orderList.get(i2).name);
306
+
307
+ }
308
+
309
+
310
+
311
+
312
+
313
+
314
+
315
+ }
316
+
317
+ Order o = new Order(1, "アップル");
318
+
319
+ Food f = new Food(1, "アップル");
320
+
321
+ System.out.println(o.hashCode() +" :" + f.hashCode());
322
+
323
+ System.out.println("o.equals(o) = " + o.equals(o));
324
+
325
+
326
+
327
+ System.out.println("f.equals(o) = " + f.equals(o));
328
+
329
+ System.out.println("o.equals(f) = " + o.equals(f));
330
+
331
+
332
+
333
+ System.out.println("o.hashCode() = " + o.hashCode());
334
+
335
+ System.out.println("f.hashCode() = " + f.hashCode());
336
+
337
+ Order o2 = new Order(1, "アップル");
338
+
339
+ Order p2 = new Order(1, "パップル");
340
+
341
+
342
+
343
+ Food f2 = new Food(1, "アップル");
344
+
345
+ System.out.println(p2.hashCode()+" :" +o2.hashCode()+" :" +f2.hashCode());
346
+
347
+ System.out.println("p2.equals(o2) = " + p2.equals(o2));
348
+
349
+ System.out.println("o2.equals(f2) = " + o2.equals(f2));
350
+
351
+ System.out.println("p2.equals(f2) = " + p2.equals(f2));
352
+
353
+
354
+
355
+
356
+
357
+ }
358
+
359
+ }
360
+
361
+ ```