equalをオーバーライドした解決策
java
1import java.util.*;
2
3class Order{
4
5public boolean equals(Object o){
6
7if((o instanceof Food) && (((Food)o).id==this.id) && (((Food)o).name.equals(this.name))){
8return true;
9
10}else return false;
11}
12
13 public int id;
14 public String name;
15 public Order(int id,String name){
16 this.id = id;
17 this.name = name;
18 }
19}
20class Food{
21
22
23
24
25 public int id;
26 public String name;
27 public Food(int id,String name){
28 this.id = id;
29 this.name = name;
30 }
31}
32
33public class App {
34 public static void main(String[] args) {
35
36 List<Order> orderList = new ArrayList<>();
37 List<Food> foodList = new ArrayList<>();
38
39 orderList.add(new Order(1,"アップル"));
40 orderList.add(new Order(2, "バナナ"));
41 orderList.add(new Order(3, "綿菓子"));
42
43 foodList.add(new Food(1,"アップル"));
44 foodList.add(new Food(2, "バナナ"));
45 for (int i2 = 0; i2 < orderList.size(); i2++) {
46
47//比較してくれない
48 if(!(foodList.contains(orderList.get(i2)))){
49 System.out.println(orderList.get(i2).name);
50 }
51
52 }
53 }
54}
反射性等が問題になるという旨指摘がありましたので現在訂正した部分までを下記に記載しておきます
再訂正
java
1
2 import java.util.*;
3
4class Order{
5public boolean equals(Object o){
6if((o instanceof Food) && (((Food)o).id==this.id) && (((Food)o).name.equals(this.name))){
7return true;
8
9}
10else if((o instanceof Order) && (o.hashCode()==this.hashCode())){
11return true;
12
13}
14return false;
15}
16public int hashCode(){
17
18
19char[] cA = name.toCharArray();
20
21int ch=0;
22
23for(char t:cA){
24int g=(int)t;
25
26
27ch+=g;
28
29}
30
31
32return id*1000+ch;
33
34}
35
36public int id;
37public String name;
38public Order(int id,String name){
39this.id = id;
40this.name = name;
41}
42}
43class Food{
44
45
46public int hashCode(){
47
48
49char[] cA = name.toCharArray();
50
51int ch=0;
52
53for(char t:cA){
54
55int g=(int)t;
56ch+=g;
57
58}
59
60
61return id*1000+ch;
62
63}
64
65
66
67public int id;
68public String name;
69public Food(int id,String name){
70this.id = id;
71this.name = name;
72}
73}
74
75public class App {
76public static void main(String[] args) {
77
78List<Order> orderList = new ArrayList<>();
79List<Food> foodList = new ArrayList<>();
80
81orderList.add(new Order(1,"アップル"));
82orderList.add(new Order(2, "バナナ"));
83orderList.add(new Order(3, "綿菓子"));
84
85foodList.add(new Food(1,"アップル"));
86foodList.add(new Food(2, "バナナ"));
87for (int i2 = 0; i2 < orderList.size(); i2++) {
88
89//比較してくれない
90if(!(foodList.contains(orderList.get(i2)))){
91System.out.println(orderList.get(i2).name);
92}
93
94
95
96}
97Order o = new Order(1, "アップル");
98Food f = new Food(1, "アップル");
99System.out.println(o.hashCode() +" :" + f.hashCode());
100System.out.println("o.equals(o) = " + o.equals(o));
101
102System.out.println("f.equals(o) = " + f.equals(o));
103System.out.println("o.equals(f) = " + o.equals(f));
104
105System.out.println("o.hashCode() = " + o.hashCode());
106System.out.println("f.hashCode() = " + f.hashCode());
107Order o2 = new Order(1, "アップル");
108Order p2 = new Order(1, "パップル");
109
110 Food f2 = new Food(1, "アップル");
111System.out.println(p2.hashCode()+" :" +o2.hashCode()+" :" +f2.hashCode());
112System.out.println("p2.equals(o2) = " + p2.equals(o2));
113 System.out.println("o2.equals(f2) = " + o2.equals(f2));
114 System.out.println("p2.equals(f2) = " + p2.equals(f2));
115
116
117}
118}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/11/23 04:29
2016/11/23 10:55
退会済みユーザー
2016/11/23 14:34
2016/11/24 01:31 編集
2016/11/24 01:39 編集
2016/11/24 04:14
退会済みユーザー
2016/11/24 13:55 編集
2016/11/24 12:42