import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class UberTest {
List data = new ArrayList<>();
public void fillData(){
//Use integer to denote Time here.. you can change it to DateTime
type.
data.add(new TimedKvPair(new KvPair("name1","Bnn"), 1) );
data.add(new TimedKvPair(new KvPair("name2","Dee"), 2) );
data.add(new TimedKvPair(new KvPair("name1","Alice"), 3) );
data.add(new TimedKvPair(new KvPair("name4","Bob"), 4) );
data.add(new TimedKvPair(new KvPair("name2","Dan"), 5) );
data.add(new TimedKvPair(new KvPair("name1","Jane"), 6) );
data.add(new TimedKvPair(new KvPair("name4","James"), 7) );
data.add(new TimedKvPair(new KvPair("name4","Jimmy"), 7) );
}
public static TreeMap createTreeMap(){
TreeMap map = new TreeMap(new
Comparator() {
@Override
public int compare(Integer o1, Integer o2) {
return o2.compareTo(o1);
}
});
return map;
}
public static Map> createTreeMapHashMap(
List data){
Map> hashMap = new HashMap<>();
for(TimedKvPair d : data){
TreeMap treeMap;
if(hashMap.get(d.kv.key)==null){
treeMap = createTreeMap();
treeMap.put(d.dt, d.kv.value);
hashMap.put(d.kv.key, treeMap);
} else {
treeMap = hashMap.get(d.kv.key);
treeMap.put(d.dt, d.kv.value);
hashMap.put(d.kv.key, treeMap);
}
}
return hashMap;
// map.put(0, "Kid");
// map.put(11, "Teens");
// map.put(20, "Twenties");
// map.put(30, "Thirties");
// map.put(40, "Forties");
// map.put(50, "Senior");
// map.put(100, "OMG OMG OMG!");
// System.out.println(map.get(map.ceilingKey(29)));
// System.out.println(map.get(map.floorKey(13))); // Teens
// System.out.println(map.get(map.floorKey(29))); // Twenties
// System.out.println(map.get(map.floorKey(30))); // Thirties
// System.out.println(map.floorEntry(42).getValue()); // Forties
}
public String getData(Map> map, String
key, int time){
TreeMap curMap = map.get(key);
Integer queryTimeKey = curMap.ceilingKey(time);
System.out.println("queryTimeKey is "+ queryTimeKey);
return map.get(key).get(queryTimeKey);
}
public void dumpMapData(Map> map){
System.out.println(map);
}
public static void main(String[] args){
// createTreeMap();
UberTest ttm = new UberTest();
ttm.fillData();
Map> hashMap = ttm.
createTreeMapHashMap(ttm.data);
System.out.println(" get data " + ttm.getData(hashMap, "name1", 5));
ttm.dumpMapData(hashMap);
}
public class KvPair {
public KvPair(String key, String value) {
super();
this.key = key;
this.value = value;
}
public String key;
public String value;
}
public class TimedKvPair {
public TimedKvPair(KvPair kv, int dt) {
super();
this.kv = kv;
this.dt = dt;
}
KvPair kv;
int dt;
}
}