【Java 动态数据统计图】动态数据统计思路Demo(动态,排序,containsKey)三(115)

上代码:


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class day10 {
	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.0);
		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);
		Map<String,Object> map5 = new HashMap<>();
		map5.put("region","西安");
		map5.put("num",6.4);
		Map<String,Object> map6 = new HashMap<>();
		map6.put("region","北京");
		map6.put("num",6.4);
		Map<String,Object> map7 = new HashMap<>();
		map7.put("region","成都");
		map7.put("num",6.4);
		Map<String,Object> map8 = new HashMap<>();
		map8.put("region","上海");
		map8.put("num",2.0);
		Map<String,Object> map9 = new HashMap<>();
		map9.put("region","上海");
		map9.put("num",2.0);
		Map<String,Object> map10 = new HashMap<>();
		map10.put("region","上海");
		map10.put("num",2.0);
		list.add(map1);
		list.add(map2);
		list.add(map3);
		list.add(map4);
		list.add(map5);
		list.add(map6);
		list.add(map7);
		list.add(map8);
		list.add(map9);
		list.add(map10);
		
		System.out.println("入参list:"+list);
		
		
		/*
		 * 思路:
		 * 创建两个Map:
		 * Map1(地域,相同地域num值加和);
		 * Map2(地域,相同地域统计次数累计);
		 * 
		 * 循环数据,通过containsKey判断是否包含key,
		 * 包含:Map1 相同地域num值加和存进去;Map2 相同地域统计次数累计存进去;
		 * 不包含:Map1和Map2直接存;
		 * 
		 * 
		 */
		
		// Map存储:(region,num),num为相同region的加和;
		Map<String,Object> regionMap = new HashMap<>();
		
		// Map存储:(region,统计次数),统计次数为相同region的次数;
		Map<String,Object> countMap = new HashMap<>();
		
		for (int i = 0; i < list.size(); i++) {
			String region = String.valueOf(list.get(i).get("region"));
			String num = String.valueOf(list.get(i).get("num"));
			// regionMap包含region:将region当作key;
			if(regionMap.containsKey(region)) {
		    	String key = region;
		    	String value = String.valueOf(regionMap.get(key));  // 从regionMap获取value值;即地域对应的num值;
		    	regionMap.put(region, Double.parseDouble(value) + Double.parseDouble(num));
		     }
		     else {  // 不包含,直接存进去
		    	 regionMap.put(region, num);
		     }
			// countMap包含region:将region当作key;
			if(countMap.containsKey(region)) {
	    		String key2 = region;
	    		String value2 = String.valueOf(countMap.get(key2));  // 从countMap获取value值;即地域对应的统计次数值;
	    		countMap.put(region, Integer.valueOf(value2) + 1);
		     }
		     else {  // 不包含,直接存进去
		    	 countMap.put(region, 1);
		     }
		}
		
		System.out.println("regionMap:"+regionMap);
		System.out.println("countMap:"+countMap);
		
		List<String> regionList = new LinkedList<String>();  // 地域
		List<String> numberList = new LinkedList<String>();  // 数值
		List<String> countList = new LinkedList<String>();  // 统计次数
		List<String> avgList = new LinkedList<String>();  // 平均值
		// 封装地域集合:(北京,5.9)
		Iterator<Map.Entry<String, Object>> it = regionMap.entrySet().iterator();
        while(it.hasNext()){
            Map.Entry<String, Object> entry = it.next();
            String key = entry.getKey();
            String value = String.valueOf(entry.getValue());
            regionList.add(key);
            numberList.add(value);
        }
		// 封装统计次数集合:(北京,1)
        Iterator<Map.Entry<String, Object>> it2 = countMap.entrySet().iterator();
        while(it2.hasNext()){
            Map.Entry<String, Object> entry = it2.next();
            String key = entry.getKey();
            String value = String.valueOf(entry.getValue());
            countList.add(value);
        }
        // 计算平均值:封装集合avgList
        if(numberList.size() == countList.size()) {
        	for (int i = 0; i < numberList.size(); i++) {
        		double fenzi = Double.parseDouble(numberList.get(i));
        		int fenmu = Integer.valueOf(countList.get(i));
        		if(fenmu > 0) {
        			Double x =  fenzi/fenmu;
        			// double类型的数据当分母的数值趋近0的时候,返回来的数值就是一个NAN:
        			if(Double.isNaN(x)){
        				x = 0.0;
        			}
        			avgList.add(String.valueOf(x));
        		}
			}
        }
        	
        System.out.println("regionList:"+regionList);
        System.out.println("numberList:"+numberList);
        System.out.println("countList:"+countList);
        System.out.println("avgList:"+avgList);
        
        // 最终输出集合:
        List<Map<String,Object>> resultList = new ArrayList<>();
        // 根据地域集合与平均值集合封装新集合resultList:
        if(regionList.size() == avgList.size()) {
        	for (int i = 0; i < regionList.size(); i++) {
				Map<String,Object> map = new HashMap<>();
				map.put("title",regionList.get(i));
				map.put("value",avgList.get(i).substring(0,avgList.get(i).indexOf(".")+2));
				resultList.add(map);
			}  	
        }
        System.out.println("最终输出集合resultList:"+resultList);

        //排序:value值大的Map往前排,斌且插入新字段:sortValue,代表序列;
       
        Double[] valueSort = new Double[resultList.size()];
        String[] titleSort = new String[resultList.size()];
        // 排序后最终输出集合:
        List<Map<String,Object>> listFinal= new ArrayList<>();
        // 获取排序的数组:
        for (int i = 0; i < resultList.size(); i++) {
            valueSort[i] = Double.parseDouble(String.valueOf(resultList.get(i).get("value")));
            titleSort[i] = String.valueOf(resultList.get(i).get("title"));
        }
        // 数组排序:
        BubblSortUtils.bubbleSortDescMultipleDouble(valueSort,titleSort);
        // 数组封装listFinal:
        for (int i = 0; i < valueSort.length; i++) {
            String ch = String.valueOf(titleSort[i]);
            for (int j = 0; j < resultList.size(); j++) {
                String value = String.valueOf(resultList.get(j).get("title"));
                if(ch.equals(value)){
                    listFinal.add(resultList.get(j));
                }
            }
        }
        // listFinal中添加排序字段:sortValue
        for (int i = 0; i < listFinal.size(); i++) {
            listFinal.get(i).put("sortValue",i+1);
        }   
        System.out.println("最终排序后输出集合listFinal:"+listFinal);
                
	}
}

测试输出:

入参list:[{num=3.3, region=西安}, {num=4.0, region=上海}, {num=5.4, region=北京}, {num=6.4, region=西安}, {num=6.4, region=西安}, {num=6.4, region=北京}, {num=6.4, region=成都}, {num=2.0, region=上海}, {num=2.0, region=上海}, {num=2.0, region=上海}]
regionMap:{成都=6.4, 上海=10.0, 西安=16.1, 北京=11.8}
countMap:{成都=1, 上海=4, 西安=3, 北京=2}
regionList:[成都, 上海, 西安, 北京]
numberList:[6.4, 10.0, 16.1, 11.8]
countList:[1, 4, 3, 2]
avgList:[6.4, 2.5, 5.366666666666667, 5.9]
最终输出集合resultList:[{title=成都, value=6.4}, {title=上海, value=2.5}, {title=西安, value=5.3}, {title=北京, value=5.9}]
最终排序后输出集合listFinal:[{sortValue=1, title=成都, value=6.4}, {sortValue=2, title=北京, value=5.9}, {sortValue=3, title=西安, value=5.3}, {sortValue=4, title=上海, value=2.5}]

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