-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathTreeSetExec.java
More file actions
55 lines (46 loc) · 1.61 KB
/
TreeSetExec.java
File metadata and controls
55 lines (46 loc) · 1.61 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
import java.util.Iterator;
import java.util.TreeSet;
public class TreeSetExec {
public static void main(String[] args) {
TreeSet<Car> treeSet = new TreeSet<Car>();
for(int i = 0; i<100; i++){
Car c = new Car("H-"+i);
c.setPrice((int)(Math.random()*100)+1);
treeSet.add(c);
}
Car c3 = new Car("H-499");
c3.setPrice(50);
treeSet.add(c3);
Iterator<Car> carIterator = treeSet.iterator();
while(carIterator.hasNext()){
Car c = carIterator.next();
System.out.println(c+" - Price - "+c.getPrice());
}
System.out.println("First:-"+treeSet.first());
System.out.println("Last:-"+treeSet.last());
Iterator<Car> headSetIterator = treeSet.headSet(c3).iterator();
while (headSetIterator.hasNext()){
Car c2 = headSetIterator.next();
System.out.println(c2+" Price :- "+c2.getPrice());
}
System.out.println("--------");
Iterator<Car> tailSetIterator = treeSet.tailSet(c3).iterator();
while(tailSetIterator.hasNext()){
Car x = tailSetIterator.next();
System.out.println(x+" Price :- "+x.getPrice());
}
System.out.println("Navigable Set methods");
System.out.println(treeSet.ceiling(c3));
System.out.println(treeSet.floor(c3));
System.out.println(treeSet.higher(c3));
System.out.println(treeSet.lower(c3));
System.out.println("Custom Comparator");
TreeSet<Car> cars = new TreeSet<Car>(new CustomComparator());
cars.addAll(treeSet);
Iterator<Car> carsIterator2 = cars.iterator();
while(carsIterator2.hasNext()){
Car c = carsIterator2.next();
System.out.println(c+" Price :- "+c.getPrice());
}
}
}