hashcode comparison in java

Solutions on MaxInterview for hashcode comparison in java by the best coders in the world

showing results for - "hashcode comparison in java"
Boyd
02 Jun 2018
1
2public class EqualsAndHashCodeExample {
3
4    public static void main(String... equalsExplanation) {
5        System.out.println(new Simpson("Homer", 35, 120)
6                 .equals(new Simpson("Homer",35,120)));
7        
8        System.out.println(new Simpson("Bart", 10, 120)
9                 .equals(new Simpson("El Barto", 10, 45)));
10        
11        System.out.println(new Simpson("Lisa", 54, 60)
12                 .equals(new Object()));
13    }
14	
15    static class Simpson {
16
17        private String name;
18        private int age;
19        private int weight;
20
21        public Simpson(String name, int age, int weight) {
22            this.name = name;
23            this.age = age;
24            this.weight = weight;
25        }
26
27        @Override
28        public boolean equals(Object o) {
29            if (this == o) {
30                return true;
31            }
32            if (o == null || getClass() != o.getClass()) {
33                return false;
34            }
35            Simpson simpson = (Simpson) o;
36            return age == simpson.age &&
37                    weight == simpson.weight &&
38                    name.equals(simpson.name);
39        }
40    }
41
42}
Emily
02 Feb 2019
1public class EqualsAndHashCodeExample {
2
3    public static void main(String... equalsExplanation) {
4        System.out.println(new Simpson("Homer", 35, 120)
5                 .equals(new Simpson("Homer",35,120)));
6        
7        System.out.println(new Simpson("Bart", 10, 120)
8                 .equals(new Simpson("El Barto", 10, 45)));
9        
10        System.out.println(new Simpson("Lisa", 54, 60)
11                 .equals(new Object()));
12    }
13	
14    static class Simpson {
15
16        private String name;
17        private int age;
18        private int weight;
19
20        public Simpson(String name, int age, int weight) {
21            this.name = name;
22            this.age = age;
23            this.weight = weight;
24        }
25
26        @Override
27        public boolean equals(Object o) {
28            if (this == o) {
29                return true;
30            }
31            if (o == null || getClass() != o.getClass()) {
32                return false;
33            }
34            Simpson simpson = (Simpson) o;
35            return age == simpson.age &&
36                    weight == simpson.weight &&
37                    name.equals(simpson.name);
38        }
39    }
40
41}