-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathTreeMapExec.java
More file actions
61 lines (46 loc) · 2.16 KB
/
TreeMapExec.java
File metadata and controls
61 lines (46 loc) · 2.16 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
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
public class TreeMapExec {
public static void main(String[] args) {
TreeMap<Car, Owner> carOwners = new TreeMap<Car, Owner>();
for(int i = 0; i < 100; i++){
Car c = new Car("H-"+i);
c.setPrice((int)(Math.random()*100)+1);
carOwners.put(c, new Owner());
}
TreeMapExec.mapIterator(carOwners);
System.out.println("First Entry: "+carOwners.firstEntry());
System.out.println("First Key : "+carOwners.firstKey());
System.out.println("Last Entry: "+carOwners.lastEntry());
System.out.println("Last Key: "+carOwners.lastKey());
System.out.println(carOwners.pollFirstEntry());
System.out.println(carOwners.pollLastEntry());
TreeMapExec.mapIterator(carOwners);
System.out.println("Higher Entry: "+carOwners.higherEntry(new Car("H-1000",40)));
System.out.println("Lower Entry: "+carOwners.lowerEntry(new Car("H-1000",40)));
System.out.println("Head Map==");
Map<Car, Owner> headMap = carOwners.headMap(new Car("H-1000", 40));
TreeMapExec.mapIterator(headMap);
System.out.println("Tail Map==");
Map<Car, Owner> tailMap = carOwners.tailMap(new Car("H-1000", 40));
TreeMapExec.mapIterator(tailMap);
System.out.println("Price Filter ---- Cars with price between 30 to 80");
Map<Car,Owner> priceFilterMap = carOwners.tailMap(new Car("H-200", 30)).headMap(new Car("H-300", 80));
TreeMapExec.mapIterator(priceFilterMap);
SortedMap<Car,Owner> threadSafeMap = Collections.synchronizedSortedMap(new TreeMap<Car,Owner>());
System.out.println("Custom order");
TreeMap<Car, Owner> customOrderMap = new TreeMap<Car, Owner>(new CustomComparator());
customOrderMap.putAll(carOwners);
TreeMapExec.mapIterator(customOrderMap);
}
public static void mapIterator(Map<Car, Owner> map){
Iterator<Map.Entry<Car, Owner>> mapItr = map.entrySet().iterator();
while(mapItr.hasNext()){
Map.Entry<Car, Owner> entry = mapItr.next();
System.out.println("Car:"+entry.getKey()+" Price:"+entry.getKey().getPrice()+" Owner:"+entry.getValue());
}
}
}