check if object is equal to another object java in hashest

Solutions on MaxInterview for check if object is equal to another object java in hashest by the best coders in the world

showing results for - "check if object is equal to another object java in hashest"
Mahamadou
28 Jun 2020
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}