我需要找到最小值和最大值,因为我按元素迭代遍历map元素(" index"是地图键数组的位置):
/**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;
}
上面显示的糟糕代码花费了很长时间并且是一个真正的瓶颈,尤其是每当索引递增时我必须得到地图子集的最小值和最大值。有谁知道我怎么能更快地实现同样的事情(获得每个地图的子集的最高和最低值)?
感谢。
答案 0 :(得分:0)
首先,从更简单的alg开始。
1获取值数组
2使用Arrays
将子数组从index(index-time,index)复制到新数组3使用数组
对子数组进行排序4获取第一个和最后一个值