-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtreeSet2.java
More file actions
44 lines (39 loc) · 1.12 KB
/
Copy pathtreeSet2.java
File metadata and controls
44 lines (39 loc) · 1.12 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
import java.util.*;
class Book implements Comparable<Book> {
int id;
String name, author, publisher;
int quantity;
public Book(int id, String name, String author, String publisher, int quantity) {
this.id = id;
this.name = name;
this.author = author;
this.publisher = publisher;
this.quantity = quantity;
}
public int compareTo(Book b) {
if (id > b.id) {
return 1;
} else if (id < b.id) {
return -1;
} else {
return 0;
}
}
public String toString() {
return id + "";
}
}
public class treeSet2 {
public static void main(String args[]) {
TreeSet<Book> set = new TreeSet<Book>();
Book b1=new Book(121,"Let us C","Yashwant Kanetkar","BPB",8);
Book b2=new Book(233,"Operating System","Galvin","Wiley",6);
Book b3=new Book(101,"Data Communications & Networking","Forouzan","Mc Graw Hill",4);
set.add(b1);
set.add(b2);
set.add(b3);
for (Book b : set) {
System.out.println(b);
}
}
}