-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashSetTest.java
More file actions
91 lines (64 loc) · 1.91 KB
/
Copy pathHashSetTest.java
File metadata and controls
91 lines (64 loc) · 1.91 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
Author name should be same for 2
*/
import java.util.HashSet;
import java.util.Iterator;
import java.util.Objects;
public class HashSetTest {
public static void main(String[] args) {
Book book1 = new Book("The Alchemist", "Daniel", 101, 1, 50, 100.50f);
Book book2 = new Book("The Alchemist", "John", 102, 2, 100, 150);
Book book3 = new Book("The Alchemist", "Daniel", 101, 1, 50, 100.50f);
HashSet<Book> hs = new HashSet<Book>();
hs.add(book1);
hs.add(book2);
hs.add(book3);
System.out.println("Size of hashSet: "+hs.size());
Iterator<Book> itr = hs.iterator();
while(itr.hasNext())
{
Book obj = itr.next();
System.out.println("Object: "+obj);
}
}
}
class Book
{
String title;
String author;
int bookNo;
int edition;
int noOfPages;
float price;
public Book(String title, String author, int bookNo, int edition, int noOfPages, float price) {
super();
this.title = title;
this.author = author;
this.bookNo = bookNo;
this.edition = edition;
this.noOfPages = noOfPages;
this.price = price;
}
@Override
public String toString() {
return "Book [title=" + title + ", author=" + author + ", bookNo=" + bookNo + ", edition=" + edition
+ ", noOfPages=" + noOfPages + ", price=" + price + "]";
}
@Override
public int hashCode() {
return Objects.hash(author, bookNo, edition, noOfPages, price, title);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Book other = (Book) obj;
return Objects.equals(author, other.author) && bookNo == other.bookNo && edition == other.edition
&& noOfPages == other.noOfPages && Float.floatToIntBits(price) == Float.floatToIntBits(other.price)
&& Objects.equals(title, other.title);
}
}