需求:
- 有一个List<Map<String.Object>>,存储了区域的数据,
数据是根据用户查询条件进行显示的;所以查询的数据是动态的; - 按区域维度统计每个区域出现的次数,并且按照次数的大小排序(升序);
- 区域是动态的,有次数则返回;没有则不返回;
分析:
后端给前端返回值:不能用对象返回,因为数据是动态的,key是不固定的;
可以使用:List<Map<String,Object>>返回;Map中key为区域;value为统计次数;
Demo案例:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;public class day07 {public static void main(String[] args) {List<Map<String,Object>> list = new ArrayList<>();Map<String,Object> map9 = new HashMap<>();map9.put("region","灞桥区");Map<String,Object> map10 = new HashMap<>();map10.put("region","灞桥区");Map<String,Object> map11 = new HashMap<>();map11.put("region","灞桥区");Map<String,Object> map12 = new HashMap<>();map12.put("region","灞桥区");Map<String,Object> map13 = new HashMap<>();map13.put("region","灞桥区");Map<String,Object> map1 = new HashMap<>();map1.put("region","长安区");Map<String,Object> map2 = new HashMap<>();map2.put("region","雁塔区");Map<String,Object> map3 = new HashMap<>();map3.put("region","莲湖区");Map<String,Object> map4 = new HashMap<>();map4.put("region","碑林区");Map<String,Object> map5 = new HashMap<>();map5.put("region","莲湖区");Map<String,Object> map6 = new HashMap<>();map6.put("region","未央区");Map<String,Object> map7 = new HashMap<>();map7.put("region","未央区");Map<String,Object> map8 = new HashMap<>();map8.put("region","未央区");list.add(map9);list.add(map10);list.add(map11);list.add(map12);list.add(map13);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<Map<String,Object>> listResult = new ArrayList<>();List<Map<String,Object>> listFinal= new ArrayList<>();String[] regionlArr = {"灞桥区","长安区","雁塔区","莲湖区","碑林区","未央区","新城区"};int[] arr = new int[regionlArr.length];// 数组接收:统计每个区域出现的次数;for (int i = 0; i < list.size(); i++) {String re = String.valueOf(list.get(i).get("region"));for (int j = 0; j < regionlArr.length; j++) {if(re.equals(regionlArr[j])){arr[j] = arr[j] + 1;}}}// List接收:动态获取数据,过滤掉次数为0的数据;for (int i = 0; i < regionlArr.length; i++) {Map<String,Object> map = new HashMap<>();String title = regionlArr[i];int value = arr[i];if(!String.valueOf(value).equals("0")){map.put("title",title);map.put("value",value);listResult.add(map);}}System.out.println("动态获取匹配后的数据:"+listResult);int[] valueSort = new int[listResult.size()];String[] regionSort = new String[listResult.size()];// 获取次数用于排序for (int i = 0; i < listResult.size(); i++) {valueSort[i] = Integer.valueOf(String.valueOf(listResult.get(i).get("value")));regionSort[i] = String.valueOf(listResult.get(i).get("title"));}// 根据次数排序 bubbleSortAsc(valueSort,regionSort); // 根据排序返回数据for (int i = 0; i < regionSort.length; i++) {String ch = String.valueOf(regionSort[i]);for (int j = 0; j < listResult.size(); j++) {String value = String.valueOf(listResult.get(j).get("title"));if(ch.equals(value)){listFinal.add(listResult.get(j));}}}System.out.println("最终输出数据:"+listFinal); }for (int i = 0; i < listFinal.size(); i++) {listFinal.get(i).put("sortValue",i+1);}/** Int类型:冒泡排序(升序)**/public static void bubbleSortAsc(int[] arr,String[]arr2) {int temp; // 定义一个临时变量String temp2; // 定义一个临时变量for(int i=0;i<arr.length-1;i++){ // 冒泡趟数for(int j=0;j<arr.length-i-1;j++){if(arr[j+1]<arr[j]){temp = arr[j];arr[j] = arr[j+1];arr[j+1] = temp;temp2 = arr2[j];arr2[j] = arr2[j+1];arr2[j+1] = temp2;}}}} }
测试输出:
动态获取匹配后的数据:
[{title=灞桥区, value=5}, {title=长安区, value=1}, {title=雁塔区, value=1}, {title=莲湖区, value=2}, {title=碑林区, value=1}, {title=未央区, value=3}]最终输出数据:
[{sortValue=1, title=长安区, value=1}, {sortValue=2, title=雁塔区, value=1}, {sortValue=3, title=碑林区, value=1}, {sortValue=4, title=莲湖区, value=2}, {sortValue=5, title=未央区, value=3}, {sortValue=6, title=灞桥区, value=5}]
案例二:
需求:前端根据后端的返回数据:画统计图;
X轴是动态的,有对应区域数据则展示;
X轴 区域数据降序排序;
代码逻辑:
// Service List<Map<String,Object>> getRegionData(HttpServletRequest request,Map<String, Object> paramMap);// ServiceImpl @Overridepublic List<Map<String,Object>> getRegionData(HttpServletRequest request, Map<String, Object> paramMap) {List<Map<String,Object>> list = getCountData(request,paramMap); // 封装统计查询的数据(与数据库交互)List<Map<String,Object>> listResult = new ArrayList<>();List<Map<String,Object>> listFinal= new ArrayList<>();String[] problemRevealArr = {"长安区","雁塔区","灞桥区","莲湖区","未央区"};int[] arr = new int[problemRevealArr.length];for (int i = 0; i < list.size(); i++) {String problemReveal = String.valueOf(list.get(i).get("waysOfProblemReveal"));for (int j = 0; j < problemRevealArr.length; j++) {if(problemReveal.equals(problemRevealArr[j])){arr[j] = arr[j] + 1;}}}for (int i = 0; i < problemRevealArr.length; i++) {Map<String,Object> map = new HashMap<>();String title = problemRevealArr[i];int value = arr[i];if(!String.valueOf(value).equals("0")){map.put("title",title);map.put("value",value);listResult.add(map);}}int[] valueSort = new int[listResult.size()];String[] titleSort = new String[listResult.size()];for (int i = 0; i < listResult.size(); i++) {valueSort[i] = Integer.valueOf(String.valueOf(listResult.get(i).get("value")));titleSort[i] = String.valueOf(listResult.get(i).get("title"));}BubbleSort(valueSort,titleSort);for (int i = 0; i < valueSort.length; i++) {String ch = String.valueOf(titleSort[i]);for (int j = 0; j < listResult.size(); j++) {String value = String.valueOf(listResult.get(j).get("title"));if(ch.equals(value)){listFinal.add(listResult.get(j));}}}return listFinal;}/** 冒泡排序(降序)**/public static void BubbleSort(int[] arr,String[]arr2) {int temp;String temp2;for (int i=0;i<arr.length;i++) {for (int j = 0; j < arr.length; j++) {if (arr[i] >= arr[j]) {temp = arr[i];arr[i] = arr[j];arr[j] = temp;temp2 = arr2[i];arr2[i] = arr2[j];arr2[j] = temp2;}}}}
Postman接口测试: