-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSolution.java
More file actions
126 lines (100 loc) · 4.34 KB
/
Copy pathSolution.java
File metadata and controls
126 lines (100 loc) · 4.34 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
//Greet Apelles, the approved in Christ. Greet those who are of the household of Aristobulus. (Romans 16:10)
package com.javarush.task.task21.task2106;
import java.util.Date;
/*
Ошибка в equals/hashCode
*/
public class Solution {
private int anInt;
private String string;
private double aDouble;
private Date date;
private Solution solution;
public Solution(int anInt, String string, double aDouble, Date date, Solution solution) {
this.anInt = anInt;
this.string = string;
this.aDouble = aDouble;
this.date = date;
this.solution = solution;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Solution)) return false;
Solution solution1 = (Solution) o;
if (Double.compare(solution1.aDouble, aDouble) != 0) return false;
if (anInt != solution1.anInt) return false;
if (date != null ? !date.equals(solution1.date) : solution1.date != null) return false;
if (solution != null ? !solution.equals(solution1.solution) : solution1.solution != null) return false;
if (string != null ? !string.equals(solution1.string) : solution1.string != null) return false;
return true;
}
@Override
public int hashCode() {
int result;
long temp;
result = anInt;
temp = aDouble != +0.0d ? Double.doubleToLongBits(aDouble) : 0L;
result = 31 * result + (int) (temp ^ (temp >>> 32));
result = 31 * result + (solution != null ? solution.hashCode() : 0);
result = 31 * result + (date != null ? date.hashCode() : 0);
result = 31 * result + (string != null ? string.hashCode() : 0);
return result;
}
public static void main(String[] args) {
}
}
/*
Ошибка в equals/hashCode
Исправьте ошибки реализаций методов equals и hashCode для класса Solution.
Требования:
1. Хешкоды одинаковых объектов должны быть равны.
2. Метод equals должен проверять равен ли переданный объект равен текущему (сравнение через ==).
3. Метод equals должен проверять является ли переданный объект объектом класса Solution.
4. Метод equals должен проверять значения всех полей у переданного объекта и текущего (учти что некоторые из них могут быть равны null).
5. Должно быть обеспечено корректное поведение HashSet с типом элементов Solution.
6. В классе Solution должен быть реализован метод hashCode.
package com.javarush.task.task21.task2106;
import java.util.Date;
*
Ошибка в equals/hashCode
*
public class Solution {
private int anInt;
private String string;
private double aDouble;
private Date date;
private Solution solution;
public Solution(int anInt, String string, double aDouble, Date date, Solution solution) {
this.anInt = anInt;
this.string = string;
this.aDouble = aDouble;
this.date = date;
this.solution = solution;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o instanceof Solution) return false;
Solution solution1 = (Solution) o;
if (Double.compare(solution1.aDouble, aDouble) != 0) return false;
if (anInt != solution1.anInt) return false;
if (date != null ? !date.equals(solution1.date) : solution1.date == null) return false;
if (solution != null ? !solution.equals(solution1.solution) : solution1.solution == null) return false;
if (string != null ? !string.equals(solution1.string) : solution1.string == null) return false;
return true;
}
@Override
public int hashCode() {
int result;
long temp;
result = anInt;
temp = aDouble != +0.0d ? Double.doubleToLongBits(aDouble) : 0L;
result = 31 * result + (int) (temp ^ (temp >>> 32));
result = 31 * result + (solution != null ? solution.hashCode() : 0);
return result;
}
public static void main(String[] args) {
}
}
*/