【Java代码优化】for循环,多if条件分支代码优化一(110)

优化前代码:


public class day04 {
	public static void main(String[] args) {
		
		List<Map<String,Object>> list = new ArrayList<>();
		Map<String,Object> map1 = new HashMap<>();
		map1.put("region","西安");
		map1.put("num",3.3);
		Map<String,Object> map2 = new HashMap<>();
		map2.put("region","上海");
		map2.put("num",4.3);
		Map<String,Object> map3 = new HashMap<>();
		map3.put("region","北京");
		map3.put("num",5.4);
		Map<String,Object> map4 = new HashMap<>();
		map4.put("region","西安");
		map4.put("num",6.4);
		list.add(map1);
		list.add(map2);
		list.add(map3);
		list.add(map4);		
		
        int xianCount = 0;
        int shanghaiCount = 0;
        int beijingCount = 0;
        int wuhanCount = 0;
        
        double xianNumber = 0.0;
        double shanghaiNumber = 0.0;
        double beijingNumber = 0.0;
        double wuhanNumber = 0.0;
        
        double xianAvg = 0.0;
        double shanghaiAvg = 0.0;
        double beijingAvg = 0.0;
        double wuhanAvg = 0.0;
        
        for (int i = 0; i < list.size(); i++) {
			String num = String.valueOf(list.get(i).get("num"));
			String region = String.valueOf(list.get(i).get("region")).trim();
			if(region.endsWith("西安")) {
				xianCount = xianCount + 1;
				xianNumber = xianNumber + Double.parseDouble(num);
			}
			else if(region.endsWith("上海")) {
				shanghaiCount = shanghaiCount + 1;
				shanghaiNumber = shanghaiNumber + Double.parseDouble(num);
			}
			else if(region.endsWith("北京")) {
				beijingCount = beijingCount + 1;
				beijingNumber = beijingNumber + Double.parseDouble(num);
			}
			else if(region.endsWith("武汉")) {
				wuhanCount = wuhanCount + 1;
				wuhanNumber = wuhanNumber + Double.parseDouble(num);
			}
		}
        
        xianAvg = xianNumber/xianCount;
        shanghaiAvg = shanghaiNumber/shanghaiCount;
        beijingAvg = beijingNumber/beijingCount; 
        wuhanAvg = wuhanNumber/wuhanCount;
        
        System.out.println("xianAvg:"+xianAvg);
        System.out.println("shanghaiAvg:"+shanghaiAvg);
        System.out.println("beijingAvg:"+beijingAvg);
        System.out.println("wuhanAvg:"+wuhanAvg);
		
	}
}

优化前测试:

xianAvg:4.85
shanghaiAvg:4.3
beijingAvg:5.4
wuhanAvg:NaN

优化后代码:

public class day03 {

	public static void main(String[] args) {
		
		List<Map<String,Object>> list = new ArrayList<>();
		Map<String,Object> map1 = new HashMap<>();
		map1.put("region","西安");
		map1.put("num",3.3);
		Map<String,Object> map2 = new HashMap<>();
		map2.put("region","上海");
		map2.put("num",4.3);
		Map<String,Object> map3 = new HashMap<>();
		map3.put("region","北京");
		map3.put("num",5.4);
		Map<String,Object> map4 = new HashMap<>();
		map4.put("region","西安");
		map4.put("num",6.4);
		list.add(map1);
		list.add(map2);
		list.add(map3);
		list.add(map4);
		
		String[] reginArr = {"西安","上海","北京","武汉"};
        int[] count = new int[reginArr.length];
        double[] number = new double[reginArr.length];
        double[] avg = new double[reginArr.length];
		
		for (int i = 0; i < list.size(); i++) {
			String num = String.valueOf(list.get(i).get("num"));
			String region = String.valueOf(list.get(i).get("region")).trim();
			for (int j = 0; j < reginArr.length; j++) {
				if(region.equals(reginArr[j].trim())){
					count[j] = count[j] + 1;
					number[j] = number[j] + Double.parseDouble(String.valueOf(list.get(i).get("num")));
				}
			}
		}
		for (int i = 0; i < number.length; i++) {
			avg[i] =  number[i]/count[i];
			// double类型的数据当分母的数值趋近0的时候,返回来的数值就是一个NAN:
			if(Double.isNaN(avg[i])){
				avg[i] = 0.0;
			}
		}
		for (int i = 0; i < avg.length; i++) {
			System.out.println("avg:"+avg[i]);
		}
		
	}
}

优化后测试:

avg:4.85
avg:4.3
avg:5.4
avg:0.0

总结:

  1. 优化前:代码需初始化很多变量;优化后:代码用数组存储初始化变量;
  2. 优化前:代码需很多if条件判断; 优化后:代码用循环数组代替;

你可能感兴趣的:(java,优化,java,windows,开发语言)