forked from hussien89aa/DataStructureAndAlgorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeSetExample.java
More file actions
53 lines (44 loc) · 1.09 KB
/
TreeSetExample.java
File metadata and controls
53 lines (44 loc) · 1.09 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
package collection.com;
import java.util.Iterator;
import java.util.PriorityQueue;
import java.util.TreeSet;
public class TreeSetExample {
public static void main(String args[]){
TreeSet<String> al=new TreeSet<String>();
al.add("Ravi");
al.add("Vijay");
al.add("Ravi");
al.add("Ajay");
Iterator<String> itr=al.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
// object
System.out.println("*************** object");
TreeSet<NodeS> s= new TreeSet<NodeS>();
s.add(new NodeS(61,null));
s.add(new NodeS(3,null));
s.add(new NodeS(6,null));
s.add(new NodeS(4,null));
Iterator<NodeS> itr2=s.iterator();
while(itr2.hasNext()){
System.out.println(itr2.next().cost);
}
}
}
class NodeS implements Comparable<NodeS>{
int cost;
NodeS next;
public NodeS(int cost, NodeS next){
this.cost=cost;
this.next=next;
}
@Override
public int compareTo(NodeS o) {
// TODO Auto-generated method stub
if (this.cost>o.cost )
return 1;
else
return 0;
}
}