使用Map对于两层for循环的一个优化
2017-08-29 16:56
267 查看
使用Map的get方法来对某些情形下的多重for循环进行优化。
优化前代码
List<Map<String, Object>> result = new ArrayList<>();
List<Map<String,Object>> result1 = new ArrayList<>();
List<Map<String,Object>> result2 = new ArrayList<>();
result1 = CITService.queryComplianIndexLastDay();
result2 = CITService.queryComplianIndexNowDay();
for(int i = 0; i < result2.size(); i++){
Map<String,Object> timeNow=new HashMap<>();
timeNow.put("index_order",result2.get(i).get("index_order"));
timeNow.put("ID",result2.get(i).get("ID"));
timeNow.put("index_value",result2.get(i).get("index_value"));
timeNow.put("index_name",result2.get(i).get("index_name"));
timeNow.put("index_id",result2.get(i).get("index_id"));
for (int m = 0; m<result1.size();m++){
if(result2.get(i).get("index_id").equals(result1.get(m).get("index_id"))){
timeNow.put("compare_value",Math.round(Double.parseDouble(result2.get(i).get("index_value") + "")/Double.parseDouble(result1.get(m).get("index_value") + "")* 100) / 100.00 );
}
}
没有优化前通过两层for循环来匹配result1和result2的index_id相等才会对它们进行index_value的计算。这种方法效率是很低下的,如果数据量一大即会崩溃。
优化后代码
List<Map<String, Object>> result = new ArrayList<>();
List<Map<String,Object>> result1 = new ArrayList<>();
List<Map<String,Object>> result2 = new ArrayList<>();
Map<Object,Map> map=new HashMap<>();
for(int j=0;j<result1.size();j++){
map.put(result1.get(j).get("index_id"),result1.get(j));
}
for(int i = 0; i < result2.size(); i++){
Map<String,Object> timeNow=new HashMap<>();
timeNow.put("index_order",result2.get(i).get("index_order"));
timeNow.put("ID",result2.get(i).get("ID"));
timeNow.put("index_value",result2.get(i).get("index_value"));
timeNow.put("index_name",result2.get(i).get("index_name"));
timeNow.put("index_id",result2.get(i).get("index_id"));
if(map.get(result2.get(i).get("index_id")) !=null){
timeNow.put("compare_value",Math.round(Double.parseDouble(result2.get(i).get("index_value") + "")/Double.parseDouble(map.get(result2.get(i).get("index_id")).get("index_value")+ "")* 100) / 100.00 );
}
result.add(timeNow);
}
这个方法可以节约百分之八十的效率。这个效率提升在不再需要使用遍历去匹配result1和result2的index_id相等再进行index_value的计算。而是新创建一个Map将result1中每个元素即每一条数据中的index_id作为其键,然后整个元素又作为和这个键对应的值。然后在for循环中直接调用map.get(result2.get(i).get(“index_id”)).如果在result1中有index_id等于result2.get(i).get(“index_id”)则会直接被查出值即整个元素,最后再调用就好。
优化前代码
List<Map<String, Object>> result = new ArrayList<>();
List<Map<String,Object>> result1 = new ArrayList<>();
List<Map<String,Object>> result2 = new ArrayList<>();
result1 = CITService.queryComplianIndexLastDay();
result2 = CITService.queryComplianIndexNowDay();
for(int i = 0; i < result2.size(); i++){
Map<String,Object> timeNow=new HashMap<>();
timeNow.put("index_order",result2.get(i).get("index_order"));
timeNow.put("ID",result2.get(i).get("ID"));
timeNow.put("index_value",result2.get(i).get("index_value"));
timeNow.put("index_name",result2.get(i).get("index_name"));
timeNow.put("index_id",result2.get(i).get("index_id"));
for (int m = 0; m<result1.size();m++){
if(result2.get(i).get("index_id").equals(result1.get(m).get("index_id"))){
timeNow.put("compare_value",Math.round(Double.parseDouble(result2.get(i).get("index_value") + "")/Double.parseDouble(result1.get(m).get("index_value") + "")* 100) / 100.00 );
}
}
没有优化前通过两层for循环来匹配result1和result2的index_id相等才会对它们进行index_value的计算。这种方法效率是很低下的,如果数据量一大即会崩溃。
优化后代码
List<Map<String, Object>> result = new ArrayList<>();
List<Map<String,Object>> result1 = new ArrayList<>();
List<Map<String,Object>> result2 = new ArrayList<>();
Map<Object,Map> map=new HashMap<>();
for(int j=0;j<result1.size();j++){
map.put(result1.get(j).get("index_id"),result1.get(j));
}
for(int i = 0; i < result2.size(); i++){
Map<String,Object> timeNow=new HashMap<>();
timeNow.put("index_order",result2.get(i).get("index_order"));
timeNow.put("ID",result2.get(i).get("ID"));
timeNow.put("index_value",result2.get(i).get("index_value"));
timeNow.put("index_name",result2.get(i).get("index_name"));
timeNow.put("index_id",result2.get(i).get("index_id"));
if(map.get(result2.get(i).get("index_id")) !=null){
timeNow.put("compare_value",Math.round(Double.parseDouble(result2.get(i).get("index_value") + "")/Double.parseDouble(map.get(result2.get(i).get("index_id")).get("index_value")+ "")* 100) / 100.00 );
}
result.add(timeNow);
}
这个方法可以节约百分之八十的效率。这个效率提升在不再需要使用遍历去匹配result1和result2的index_id相等再进行index_value的计算。而是新创建一个Map将result1中每个元素即每一条数据中的index_id作为其键,然后整个元素又作为和这个键对应的值。然后在for循环中直接调用map.get(result2.get(i).get(“index_id”)).如果在result1中有index_id等于result2.get(i).get(“index_id”)则会直接被查出值即整个元素,最后再调用就好。
相关文章推荐
- map[]的正确使用:自以为是导致map的一个错误使用
- 使用反射机制 来把一个具体的bean 处理成一个map。并且获得其中的field和值
- Orchard:把之前写的Map Content Part专为一个Widget来使用
- Orchard:把之前写的Map Content Part专为一个Widget来使用
- typedef使用大全(转) 一、数组 typedef到处都是,但是能够真正懂得typedef使用的不算太多。对于初学者而言,看别人的源码时对到处充斥的typedef往往不知所错,而参考书又很少,所以在此给出一个源码,供大家参考 #include #
- Orchard:把之前写的Map Content Part专为一个Widget来使用
- 一个hash_map使用错误
- 对于一个频繁使用的短小函数,在C语言中应用什么实现,在C++中应用什么实现?
- 使用Collections.emptyMap()引起的一个奇怪的问题
- 做一个对于很多类都能使用的单例模式类
- SQL优化基础 使用索引(一个小例子)
- SQL优化基础:使用索引(一个小例子)
- 使用反射机制 来把一个具体的bean 处理成一个map。并且获得其中的field和值
- 在使用CompareValidator对于日期进行验证的时候,最好多一个对于日期格式的验证
- 一个使用share memory进行性能优化的实例
- SQL优化基础:使用索引(一个小例子)
- 写出3种遍历一个list的方法,(使用for循环)
- 数据优化性能之 c++ map的使用方法
- Map.containsKey() 的一个使用场景.
- [转] 使用Duff's Device算法优化for循环