-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortedMap.java
More file actions
30 lines (23 loc) · 782 Bytes
/
Copy pathSortedMap.java
File metadata and controls
30 lines (23 loc) · 782 Bytes
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
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SortedMap <K, V>{
private Map<K, IPair<K, V>> map;
private PairFactory<K, V, ? extends IPair<K, V>> factory;
public SortedMap(PairFactory<K, V, ? extends IPair<K, V>> factory){
this.factory = factory;
map = new HashMap<K, IPair<K, V>>();
}
public IPair<K, V> put(K key, V value) {
return map.put(key, factory.create(key, value));
//return map.put(key, new Pair<K, V>(key, value));
//http://stackoverflow.com/questions/75175/create-instance-of-generic-type-in-java
}
public List<IPair<K, V>> sortByValue(){
List<IPair<K, V>> l = new ArrayList<IPair<K, V>>(map.values());
Collections.sort(l);
return l;
}
}