[英]Min & Max of subset of map
如图所示,我需要逐个元素遍历map来找到最小值和最大值(“ index”是map的键Array的位置):
/**even: lowest, odd: highest*/
private Double[] getLowestAndHighestValues(final int index, final int timeRange, ConcurrentSkipListMap<String, Double> ... maps){
if(maps != null && maps.length > 0){
ArrayList<Object[]> keyArrayList = new ArrayList<>();
for(int i = 0; i < maps.length; i++){
try{
ConcurrentSkipListMap<String, Double> map = maps[i];
keyArrayList.add(map.keySet().toArray());
}
catch(Exception e){
}
}
Double[] values = new Double[maps.length * 2];
for(int i = index - timeRange; i < index; i++){
if(i >= 0){
for(int x = 0; x < maps.length; x++){
try{
if(x >= 0){
ConcurrentSkipListMap<String, Double> map = maps[x];
if(map != null){
final Object[] keyArray = keyArrayList.get(x);
final String key = (String) keyArray[i];
Double value = map.get(key);
if(value != null && !Double.isNaN(value)){
if(values[x * 2] == null){
values[x * 2] = value;
}
else{
if(values[x * 2] > value){
values[x * 2] = value;
}
}
if(values[(x * 2) + 1] == null){
values[(x * 2) + 1] = value;
}
else{
if(values[(x * 2) + 1] < value){
values[(x * 2) + 1] = value;
}
}
}
}
}
}
catch(Exception e){}
}
}
}
return values;
}
return null;
}
上面显示的糟糕代码花费了很长时间,这是一个真正的瓶颈,尤其是当索引每次增加时我必须获取地图子集的最小值和最大值时。 有谁知道我可以更快地完成同一件事(获得每个地图的子集的最高和最低值)?
谢谢。
首先,从一个简单的算法开始。
1获取值数组
2使用数组将子数组从索引(索引时间,索引)复制到新数组
3使用数组对子数组进行排序
4获取第一个和最后一个值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.