-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathSetDemo.java
More file actions
47 lines (39 loc) · 835 Bytes
/
SetDemo.java
File metadata and controls
47 lines (39 loc) · 835 Bytes
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
import java.util.HashSet;
class Customer
{
int id;
String name;
Customer(int id , String name){
this.id = id ;
this.name = name;
}
public String toString(){
return "Id "+id +" Name "+name;
}
public boolean equals(Object o){
Customer c = (Customer) o;
if(this.id==c.id && this.name.equals(c.name)){
return true;
}
else
{
return false;
}
}
@Override
public int hashCode(){
return name.length();
}
}
public class SetDemo {
public static void main(String[] args) {
Customer ram = new Customer(1001,"Ram");
HashSet<Customer> hashSet = new HashSet<>();
hashSet.add(ram);
Customer ram2 = new Customer(1001,"Ram");
hashSet.add(ram2);
System.out.println("Ram HashCode "+ram.hashCode());
System.out.println("Ram2 HashCode "+ram2.hashCode());
System.out.println(hashSet);
}
}