-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashSetDemo.java
More file actions
58 lines (49 loc) · 1.2 KB
/
HashSetDemo.java
File metadata and controls
58 lines (49 loc) · 1.2 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
package Collections.Demos.Set;
import java.util.HashSet;
class Book {
int id;
String name;
String category;
Book(int id, String name, String category) {
this.id = id;
this.name = name;
this.category = category;
}
void print() {
System.out.println("Id " + id);
System.out.println("Name " + name);
System.out.println("Category " + category);
}
@Override
public boolean equals(Object o) {
Book book = (Book) o;
if (this.id == book.id && this.name.equals(book.name) && this.category.equals(book.category)) {
return true;
} else {
return false;
}
}
@Override
public int hashCode() {
if (category.equalsIgnoreCase("programming")) {
return 100;
} else {
return 200;
}
}
}
public class HashSetDemo {
public static void main(String[] args) {
HashSet<Book> bookSet = new HashSet<>();
bookSet.add(new Book(101, "Let Us C", "Programming"));
bookSet.add(new Book(101, "Let Us C", "Programming"));
bookSet.add(new Book(102, "Java", "Programming"));
bookSet.add(new Book(102, "Java", "Programming"));
bookSet.add(new Book(102, "LAN", "Networking"));
for (Book b : bookSet) {
System.out.println(b.hashCode());
b.print();
}
System.out.println(bookSet);
}
}