-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeSetTest.java
More file actions
76 lines (54 loc) · 1.58 KB
/
Copy pathTreeSetTest.java
File metadata and controls
76 lines (54 loc) · 1.58 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
package TestSet;
import java.util.*;
/**
* @author Administrator
*
*/
public class TreeSetTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
SortedSet<Item> parts=new TreeSet<>();
parts.add(new Item("Toaster",1234));
parts.add(new Item("Widget",4562));
parts.add(new Item("Modem",9912));
System.out.println(parts);
SortedSet<Item> sortByDescription=new TreeSet<>(new Comparator<Item>(){
public int compare(Item a,Item b){
String descrA=a.getDescription();
String descrB=b.getDescription();
return descrA.compareTo(descrB);
}
});
sortByDescription.addAll(parts);
System.out.println(sortByDescription);
}
}
class Item implements Comparable<Item>{
private String description;
private int partNumber;
public Item(String aDescription,int aPartNumber){
this.description=aDescription;
this.partNumber=aPartNumber;
}
public String getDescription(){
return this.description;
}
public String toString(){
return "[descripton="+this.description+", partNumber="+partNumber+"]";
}
public boolean equals(Object otherObject){
if(this==otherObject) return true;
if(otherObject==null) return false;
if(getClass()!=otherObject.getClass()) return false;
Item other=(Item)otherObject;
return Objects.equals(description,other.description)&&partNumber==other.partNumber;
}
public int hashCode(){
return Objects.hash(description,partNumber);
}
@Override
public int compareTo(Item other) {
// TODO Auto-generated method stub
return Integer.compare(partNumber, other.partNumber);
}
}