-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeSetTest.java
More file actions
53 lines (39 loc) · 1.22 KB
/
Copy pathTreeSetTest.java
File metadata and controls
53 lines (39 loc) · 1.22 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
import java.util.Iterator;
import java.util.TreeSet;
public class TreeSetTest {
public static void main(String[] args)
{
ChemicalElements ce1 = new ChemicalElements("Hydrogen", 4);
ChemicalElements ce2 = new ChemicalElements("Calcium", 5);
ChemicalElements ce3 = new ChemicalElements("Helium", 3);
ChemicalElements ce4 = new ChemicalElements("Oxygen", 6);
TreeSet<ChemicalElements> ts = new TreeSet<ChemicalElements>();
ts.add(ce1);
ts.add(ce2);
ts.add(ce3);
ts.add(ce4);
Iterator<ChemicalElements> iterator = ts.iterator();
while(iterator.hasNext())
{
ChemicalElements ce = iterator.next();
System.out.println("Chemical elemets : "+ce);
}
}
}
class ChemicalElements implements Comparable<ChemicalElements>
{
String atomicName;
int atomicNo;
public ChemicalElements(String atomicName, int atomicNo) {
super();
this.atomicName = atomicName;
this.atomicNo = atomicNo;
}
@Override
public String toString() {
return "ChemicalElements [atomicName=" + atomicName + ", atomicNo=" + atomicNo + "]";
}
public int compareTo(ChemicalElements o) {
return Integer.compare(atomicNo, o.atomicNo);
}
}