需求:
1.有一组数据集合,数据集合中的数据为动态;
举例如下:
[{province=陕西省, city=西安市}, {province=陕西省, city=咸阳市}, {province=陕西省, city=宝鸡市}, {province=陕西省, city=延安市}, {province=陕西省, city=汉中市}, {province=四川省, city=成都市}, {province=四川省, city=成都市}, {province=四川省, city=广元市}, {province=四川省, city=绵阳市}, {province=陕西省, city=西安市}, {province=陕西省, city=西安市}, {province=陕西省, city=宝鸡市}, {province=江苏省, city=南京市}, {province=江苏省, city=南京市}, {province=江苏省, city=扬州市}, {province=河北省, city=石家庄市}, {province=山东省, city=济南市}, {province=山东省, city=青岛市}, {province=山东省, city=烟台市}, {province=山东省, city=潍坊市}]
2.通过计算得出统计的数据;省按照大小排序,动态展示,城市按照大小排序,动态展示;
最终绘制如下:
备注::以下看到的函数名称大多是自定义封装的。
当前案例代码中需要用到的知识技能:
1.Set集合去重:Set hashSet = new HashSet<>();
2.Set集合转数组:String[] provinceArr = hashSet.toArray(new String[hashSet.size()]);
3.一维双数组排序:BubblSortUtils.bubbleSortDescMultiple(provinceSort,provinceArr);
4.创建集合数组(数组里面的元素为set集合):Set[] setArr = new Set[provinceArr.length];
5.创建二维数组(set集合数组转二维数组):String[][] stringArr = new String[provinceArr.length][];
6.二维数组,双数组根据第二维度排序:BubblSortUtils.bubbleSortDescMultiple2(countArr,stringArr);
7.String,Int数组间的互相转化;
8.百分比计算函数封装;
9.一维数组和二维数组组合:计算一维数组在二维中的中间位置;
10.返回数据封装:二维数组转一维数组;
Demo案例一:代码逻辑比较复杂,需耐心看!
package com.day01;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class day24 {
public static void main(String[] args) {
List<Map<String,Object>> list = new ArrayList<>();
Map<String,Object> map1 = new HashMap<>();
map1.put("province","陕西省");
map1.put("city","西安市");
Map<String,Object> map2 = new HashMap<>();
map2.put("province","陕西省");
map2.put("city","咸阳市");
Map<String,Object> map3 = new HashMap<>();
map3.put("province","陕西省");
map3.put("city","宝鸡市");
Map<String,Object> map4 = new HashMap<>();
map4.put("province","陕西省");
map4.put("city","延安市");
Map<String,Object> map5 = new HashMap<>();
map5.put("province","陕西省");
map5.put("city","汉中市");
Map<String,Object> map6 = new HashMap<>();
map6.put("province","四川省");
map6.put("city","成都市");
Map<String,Object> map7 = new HashMap<>();
map7.put("province","四川省");
map7.put("city","成都市");
Map<String,Object> map8 = new HashMap<>();
map8.put("province","四川省");
map8.put("city","广元市");
Map<String,Object> map9 = new HashMap<>();
map9.put("province","四川省");
map9.put("city","绵阳市");
Map<String,Object> map10 = new HashMap<>();
map10.put("province","陕西省");
map10.put("city","西安市");
Map<String,Object> map11 = new HashMap<>();
map11.put("province","陕西省");
map11.put("city","西安市");
Map<String,Object> map12 = new HashMap<>();
map12.put("province","陕西省");
map12.put("city","宝鸡市");
Map<String,Object> map13 = new HashMap<>();
map13.put("province","江苏省");
map13.put("city","南京市");
Map<String,Object> map14 = new HashMap<>();
map14.put("province","江苏省");
map14.put("city","南京市");
Map<String,Object> map15 = new HashMap<>();
map15.put("province","江苏省");
map15.put("city","扬州市");
Map<String,Object> map16 = new HashMap<>();
map16.put("province","河北省");
map16.put("city","石家庄市");
Map<String,Object> map17 = new HashMap<>();
map17.put("province","山东省");
map17.put("city","济南市");
Map<String,Object> map18 = new HashMap<>();
map18.put("province","山东省");
map18.put("city","青岛市");
Map<String,Object> map19 = new HashMap<>();
map19.put("province","山东省");
map19.put("city","烟台市");
Map<String,Object> map20 = new HashMap<>();
map20.put("province","山东省");
map20.put("city","潍坊市");
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);
list.add(map11);
list.add(map12);
list.add(map13);
list.add(map14);
list.add(map15);
list.add(map16);
list.add(map17);
list.add(map18);
list.add(map19);
list.add(map20);
System.out.println("入参数据:"+list);
// 一:获取一级维度:
// 创建Set集合:获取一级维度数据的数据集:去重
Set<String> hashSet = new HashSet<>();
for (int i = 0; i < list.size(); i++) {
String province = String.valueOf(list.get(i).get("province"));
hashSet.add(province);
}
// 创建一级维度省份数组:
String[] provinceArr = new String[hashSet.size()];
// 创建一级维度省份数量数组:
int[] provinceSort = new int[hashSet.size()];
// Set转数组
if(list.size() > 0){
provinceArr = hashSet.toArray(new String[hashSet.size()]);
}
// 根据一级维度省份数组:计算一级维度省份数量数组;
for (int i = 0; i < provinceArr.length; i++) {
for (int j = 0; j < list.size(); j++) {
if(provinceArr[i].equals(String.valueOf(list.get(j).get("province")))){
provinceSort[i] = provinceSort[i] + 1;
}
}
}
// 排序:一级维度省份数组;
BubblSortUtils.bubbleSortDescMultiple(provinceSort,provinceArr);
// 封装集合测试输出:
System.out.println("--------------------");
List<Map<String,Object>> listFinal= new ArrayList<>();
for (int i = 0; i < provinceArr.length; i++) {
Map<String,Object> map = new HashMap<>();
map.put("title",provinceArr[i]);
map.put("value",provinceSort[i]);
listFinal.add(map);
}
System.out.println("一级维度省份类型和数量:"+listFinal);
// 二:获取二级维度:
// 创建集合数组(数组里面的元素为set集合)
Set[] setArr = new Set[provinceArr.length];
// 创建二维数组(set集合数组转二维数组)
String[][] stringArr = new String[provinceArr.length][];
// 集合数组:获取二级维度城市类型数据的数据集:去重
for (int i = 0; i < provinceArr.length; i++) {
Set<String> hashSet2 = new HashSet<>();
// 获取城市数组;
for (int j = 0; j < list.size(); j++) {
if(provinceArr[i].equals(String.valueOf(list.get(j).get("province")))){
hashSet2.add(String.valueOf(list.get(j).get("city")));
}
}
setArr[i] = hashSet2;
}
// 二维数组:set集合城市数组转二维城市数组;
for (int i = 0; i < setArr.length; i++) {
Set<String> hashSet2 = setArr[i];
String[] provinceArr2 = new String[hashSet2.size()];
if(list.size() > 0){
provinceArr2 = hashSet2.toArray(new String[hashSet2.size()]);
}
stringArr[i] = provinceArr2;
}
// 创建城市数量二维数组;
String[][] countArr = new String[provinceArr.length][];
// 二维数组:根据二维城市类型数组计算:二维城市数量数组;
for (int i = 0; i < stringArr.length; i++) {
String [] s = stringArr[i];
String[] temp = new String[s.length];
for (int j = 0; j < s.length; j++) {
for (int k = 0; k < list.size(); k++) {
if(s[j].equals(String.valueOf(list.get(k).get("city")))) {
if(String.valueOf(temp[j]).equals("null")) {
temp[j] = "1";
}
else {
temp[j] = String.valueOf(Integer.valueOf(temp[j]) + 1);
}
}
}
}
countArr[i] = temp;
}
// 数组排序:城市数组和数量数组排序;
BubblSortUtils.bubbleSortDescMultiple2(countArr,stringArr);
// 三:百分比二维数组:计算百分比
String[][] percentageArr = new String[provinceArr.length][];
int sum = list.size();
for (int i = 0; i < countArr.length; i++) {
String [] s = countArr[i];
String[] temp = new String[s.length];
for (int j = 0; j < s.length; j++) {
if(list.size() > 0) {
temp[j] = percentageConversion(Integer.valueOf(s[j]),sum);
}
}
percentageArr[i] = temp;
}
System.out.println("------------------:");
// 四:数据封装返回:
/*
* 分析:当前得到了四个数组;
* 一维数组:provinceArr
* 二维数组:stringArr,countArr,percentageArr
*
* 需要将一维数组:provinceArr也转成二维数组并且将数据放在中间位置;
*
*/
// 将省份添加到每个二维城市数组的末位;用临时二维数组stringArrTemp接收;
String[][] stringArrTemp = new String[provinceArr.length][];
for (int i = 0; i < stringArr.length; i++) {
String[] s = stringArr[i];
String[] temp = new String[s.length+1];
for (int j = 0; j < s.length; j++) {
temp[j] = s[j];
temp[s.length] = provinceArr[i];
}
stringArrTemp[i] = temp;
}
// 计算一维数组在二维中的中间位置;
String[][] provinceNewArr = new String[provinceArr.length][];
for (int i = 0; i < stringArrTemp.length; i++) {
String[] s = stringArrTemp[i];
String[] temp = new String[s.length-1];
int num = s.length-1;
for (int j = 0; j < s.length-1; j++) {
if(j == num/2) { // 找数组中间位置
if(num % 2 ==0) { // 中间位置为偶数
if(j>0) { // 首位判断>0
temp[j-1] = s[s.length-1]; // 数据向左偏
temp[j] = "#"; // 数据左偏完后下个数据需修正;
}
}
else { // 中间位置为奇数
temp[j] = s[s.length-1];
}
}
else {
temp[j] = "#";
}
}
provinceNewArr[i] = temp;
}
// 返回方式一:
// 最终得到四个二维数组:provinceNewArr,stringArr,countArr,percentageArr
// 二维数组封装Map返回;
Map<String,Object> mapResult1 = new HashMap<>();
mapResult1.put("province", provinceNewArr);
mapResult1.put("cityType", stringArr);
mapResult1.put("cityCount", countArr);
mapResult1.put("cityPercentage", percentageArr);
System.out.println("返回方式一:最终MAP"+mapResult1);
// 最终:测试输出:
for (int i = 0; i < stringArr.length; i++) {
System.out.println(i+":"+stringArr[i]);
String[] s1 = stringArr[i];
String[] s2 = countArr[i];
String[] s3 = percentageArr[i];
String[] s4 = provinceNewArr[i];
for (int j = 0; j < s1.length; j++) {
System.out.println("二级维度城市类型和数量:"+s1[j]+"--"+s2[j]+"--"+s3[j]+"--"+s4[j]);
}
}
// 返回方式二:
// 前端要求:所有的数据把二维数组拼接成一维数组返回;
List<String> listArr1 = new ArrayList<>();
List<String> listArr2 = new ArrayList<>();
List<String> listArr3 = new ArrayList<>();
List<String> listArr4 = new ArrayList<>();
for (int i = 0; i < provinceNewArr.length; i++) {
String[] s1 = provinceNewArr[i];
String[] s2 = stringArr[i];
String[] s3 = countArr[i];
String[] s4 = percentageArr[i];
for (int j = 0; j < s1.length; j++) {
listArr1.add(s1[j]);
listArr2.add(s2[j]);
listArr3.add(s3[j]);
listArr4.add(s4[j]);
}
}
String[] provinceNewArr2 = new String[listArr1.size()];
String[] stringArr2 = new String[listArr2.size()];
String[] countArr2 = new String[listArr3.size()];
String[] percentageArr2 = new String[listArr4.size()];
for (int i = 0; i < listArr1.size(); i++) {
provinceNewArr2[i] = listArr1.get(i);
stringArr2[i] = listArr2.get(i);
countArr2[i] = listArr3.get(i);
percentageArr2[i] = listArr4.get(i);
}
Map<String,Object> mapResult2 = new HashMap<>();
mapResult2.put("province", provinceNewArr2);
mapResult2.put("cityType", stringArr2);
mapResult2.put("cityCount", countArr2);
mapResult2.put("cityPercentage", percentageArr2);
System.out.println("返回方式二:最终MAP"+mapResult2);
}
public static String percentageConversion(long divisor, long dividend) {
String percentage = "";// 接受百分比的值
double y = divisor * 1.0;
double z = dividend * 1.0;
if (y == 0 || z == 0) {
return "0.00%";
}
double result = y / z;
DecimalFormat decimalFormat = new DecimalFormat("##.00%"); // ##.00%
percentage = decimalFormat.format(result);
// ##.00% 使用这种转换方式,整数位如果是0 则会被删除 即0.35% 会出现 .35%的情况
char c = percentage.charAt(0);
if (String.valueOf(c).equals(".")) {
StringBuffer sb = new StringBuffer();
sb.append("0").append(percentage);
return String.valueOf(sb);
}
return percentage;
}
}
代码中数组排序方法查看:【Java 排序】冒泡排序(升降序,Int类型,Double类型,多数组,多维数组)(111)
测试输出:
入参数据:[{province=陕西省, city=西安市}, {province=陕西省, city=咸阳市}, {province=陕西省, city=宝鸡市}, {province=陕西省, city=延安市}, {province=陕西省, city=汉中市}, {province=四川省, city=成都市}, {province=四川省, city=成都市}, {province=四川省, city=广元市}, {province=四川省, city=绵阳市}, {province=陕西省, city=西安市}, {province=陕西省, city=西安市}, {province=陕西省, city=宝鸡市}, {province=江苏省, city=南京市}, {province=江苏省, city=南京市}, {province=江苏省, city=扬州市}, {province=河北省, city=石家庄市}, {province=山东省, city=济南市}, {province=山东省, city=青岛市}, {province=山东省, city=烟台市}, {province=山东省, city=潍坊市}]
--------------------
一级维度省份类型和数量:[{title=陕西省, value=8}, {title=四川省, value=4}, {title=山东省, value=4}, {title=江苏省, value=3}, {title=河北省, value=1}]
------------------:
返回方式一:最终MAP{cityType=[[Ljava.lang.String;@506e6d5e, province=[[Ljava.lang.String;@96532d6, cityCount=[[Ljava.lang.String;@3796751b, cityPercentage=[[Ljava.lang.String;@67b64c45}
0:[Ljava.lang.String;@4411d970
二级维度城市类型和数量:西安市--3--15.00%--#
二级维度城市类型和数量:宝鸡市--2--10.00%--#
二级维度城市类型和数量:延安市--1--5.00%--陕西省
二级维度城市类型和数量:咸阳市--1--5.00%--#
二级维度城市类型和数量:汉中市--1--5.00%--#
1:[Ljava.lang.String;@6442b0a6
二级维度城市类型和数量:成都市--2--10.00%--#
二级维度城市类型和数量:绵阳市--1--5.00%--四川省
二级维度城市类型和数量:广元市--1--5.00%--#
2:[Ljava.lang.String;@60f82f98
二级维度城市类型和数量:青岛市--1--5.00%--#
二级维度城市类型和数量:济南市--1--5.00%--山东省
二级维度城市类型和数量:烟台市--1--5.00%--#
二级维度城市类型和数量:潍坊市--1--5.00%--#
3:[Ljava.lang.String;@35f983a6
二级维度城市类型和数量:南京市--2--10.00%--江苏省
二级维度城市类型和数量:扬州市--1--5.00%--#
4:[Ljava.lang.String;@7f690630
二级维度城市类型和数量:石家庄市--1--5.00%--河北省
返回方式二:最终MAP{cityType=[Ljava.lang.String;@edf4efb, province=[Ljava.lang.String;@2f7a2457, cityCount=[Ljava.lang.String;@566776ad, cityPercentage=[Ljava.lang.String;@6108b2d7}
案例二:使用场景:
public Map<String, Object> getViolateSceneData(HttpServletRequest request, Map<String, Object> paramMap) {
List<Map<String,Object>> list = getCountData(request,paramMap);
if(list.size() == 0){
return null;
}
// 一:获取一级维度:
// 创建Set集合:获取一级维度数据的数据集:去重
Set<String> hashSet = new HashSet<>();
for (int i = 0; i < list.size(); i++) {
String province = String.valueOf(list.get(i).get("businessSegment"));
hashSet.add(province);
}
// 创建一级维度省份数组:
String[] provinceArr = new String[hashSet.size()];
// 创建一级维度省份数量数组:
int[] provinceSort = new int[hashSet.size()];
// Set转数组
if(list.size() > 0){
provinceArr = hashSet.toArray(new String[hashSet.size()]);
}
// 根据一级维度省份数组:计算一级维度省份数量数组;
for (int i = 0; i < provinceArr.length; i++) {
for (int j = 0; j < list.size(); j++) {
if(provinceArr[i].equals(String.valueOf(list.get(j).get("businessSegment")))){
provinceSort[i] = provinceSort[i] + 1;
}
}
}
// 排序:一级维度省份数组;
GetJsonDataUtil.BubbleSort(provinceSort,provinceArr);
// 二:获取二级维度:
// 创建集合数组(数组里面的元素为set集合)
Set[] setArr = new Set[provinceArr.length];
// 创建二维数组(set集合数组转二维数组)
String[][] stringArr = new String[provinceArr.length][];
// 集合数组:获取二级维度城市类型数据的数据集:去重
for (int i = 0; i < provinceArr.length; i++) {
Set<String> hashSet2 = new HashSet<>();
// 获取城市数组;
for (int j = 0; j < list.size(); j++) {
if(provinceArr[i].equals(String.valueOf(list.get(j).get("businessSegment")))){
hashSet2.add(String.valueOf(list.get(j).get("violateScene")));
}
}
setArr[i] = hashSet2;
}
// 二维数组:set集合城市数组转二维城市数组;
for (int i = 0; i < setArr.length; i++) {
Set<String> hashSet2 = setArr[i];
String[] provinceArr2 = new String[hashSet2.size()];
if(list.size() > 0){
provinceArr2 = hashSet2.toArray(new String[hashSet2.size()]);
}
stringArr[i] = provinceArr2;
}
// 创建城市数量二维数组;
String[][] countArr = new String[provinceArr.length][];
// 二维数组:根据二维城市类型数组计算:二维城市数量数组;
for (int i = 0; i < stringArr.length; i++) {
String [] s = stringArr[i];
String[] temp = new String[s.length];
for (int j = 0; j < s.length; j++) {
for (int k = 0; k < list.size(); k++) {
if(s[j].equals(String.valueOf(list.get(k).get("violateScene")))) {
if(String.valueOf(temp[j]).equals("null")) {
temp[j] = "1";
}
else {
temp[j] = String.valueOf(Integer.valueOf(temp[j]) + 1);
}
}
}
}
countArr[i] = temp;
}
// 数组排序:城市数组和数量数组排序;
GetJsonDataUtil.bubbleSortDescMultiple2(countArr,stringArr);
// 三:百分比二维数组:计算百分比
String[][] percentageArr = new String[provinceArr.length][];
int sum = list.size();
for (int i = 0; i < countArr.length; i++) {
String [] s = countArr[i];
String[] temp = new String[s.length];
for (int j = 0; j < s.length; j++) {
if(list.size() > 0) {
temp[j] = DateUtil.percentageConversion(Integer.valueOf(s[j]),sum);
}
}
percentageArr[i] = temp;
}
// 四:数据封装返回:
/*
* 分析:当前得到了四个数组;
* 一维数组:provinceArr
* 二维数组:stringArr,countArr,percentageArr
*
* 需要将一维数组:provinceArr也转成二维数组并且放在中间位置;
*
*/
// 将省份添加到每个二维城市数组的末位;用临时二维数组stringArrTemp接收;
String[][] stringArrTemp = new String[provinceArr.length][];
for (int i = 0; i < stringArr.length; i++) {
String[] s = stringArr[i];
String[] temp = new String[s.length+1];
for (int j = 0; j < s.length; j++) {
temp[j] = s[j];
temp[s.length] = provinceArr[i];
}
stringArrTemp[i] = temp;
}
// 计算一维数组在二维中的中间位置;
String[][] provinceNewArr = new String[provinceArr.length][];
for (int i = 0; i < stringArrTemp.length; i++) {
String[] s = stringArrTemp[i];
String[] temp = new String[s.length-1];
int num = s.length-1;
for (int j = 0; j < s.length-1; j++) {
if(j == num/2) { // 找数组中间位置
if(num % 2 ==0) { // 中间位置为偶数
if(j>0) { // 首位判断>0
temp[j-1] = s[s.length-1]; // 数据向左偏
temp[j] = "#";
}
}
else { // 中间位置为奇数
temp[j] = s[s.length-1];
}
}
else {
temp[j] = "#";
}
}
provinceNewArr[i] = temp;
}
// 最终得到四个二维数组:provinceNewArr,stringArr,countArr,percentageArr
// 封装数据返回前端方式一:
// Map<String,Object> map = new HashMap<>();
// map.put("businessSegment", provinceNewArr);
// map.put("violateSceneType", stringArr);
// map.put("violateSceneCount", countArr);
// map.put("violateScenePercentage", percentageArr);
// return map;
// 封装数据返回前端方式二:
List<String> listArr1 = new ArrayList<>();
List<String> listArr2 = new ArrayList<>();
List<String> listArr3 = new ArrayList<>();
List<String> listArr4 = new ArrayList<>();
for (int i = 0; i < provinceNewArr.length; i++) {
String[] s1 = provinceNewArr[i];
String[] s2 = stringArr[i];
String[] s3 = countArr[i];
String[] s4 = percentageArr[i];
for (int j = 0; j < s1.length; j++) {
listArr1.add(s1[j]);
listArr2.add(s2[j]);
listArr3.add(s3[j]);
listArr4.add(s4[j]);
}
}
String[] provinceNewArr2 = new String[listArr1.size()];
String[] stringArr2 = new String[listArr2.size()];
String[] countArr2 = new String[listArr3.size()];
String[] percentageArr2 = new String[listArr4.size()];
for (int i = 0; i < listArr1.size(); i++) {
provinceNewArr2[i] = listArr1.get(i);
stringArr2[i] = listArr2.get(i);
countArr2[i] = listArr3.get(i);
percentageArr2[i] = listArr4.get(i);
}
Map<String,Object> mapResult2 = new HashMap<>();
mapResult2.put("businessSegment", provinceNewArr2);
mapResult2.put("violateSceneType", stringArr2);
mapResult2.put("violateSceneCount", countArr2);
mapResult2.put("violateScenePercentage", percentageArr2);
return mapResult2;
}
备注:代码中已包含两种返回值的方式:
Postman接口测试:按照二维数组返回:封装数据返回前端方式一
{
"data": {
"businessSegment": [
[
"#",
"#",
"BCG预防",
"#",
"#"
],
[
"#",
"安全岗",
"#"
],
[
"招标/议标",
"#"
],
[
"实验室管理"
]
],
"violateScenePercentage": [
[
"21.05%",
"15.79%",
"10.53%",
"5.26%",
"5.26%"
],
[
"15.79%",
"10.53%",
"5.26%"
],
[
"10.53%",
"5.26%"
],
[
"5.26%"
]
],
"violateSceneType": [
[
"关联关系",
"第二职业",
"代采购",
"项目成员公示",
"吃空饷"
],
[
"工作纪律违规",
"其它",
"门禁管理"
],
[
"其它",
"竞标违规"
],
[
"闲置资产"
]
],
"violateSceneCount": [
[
"4",
"3",
"2",
"1",
"1"
],
[
"3",
"2",
"1"
],
[
"2",
"1"
],
[
"1"
]
]
},
"code": 200,
"message": "OK"
}
Postman接口测试:按照一维数组返回:封装数据返回前端方式二(前端推荐这种方式)
{
"data": {
"businessSegment": [
"#",
"#",
"BCG预防",
"#",
"#",
"#",
"安全岗",
"#",
"招标/议标",
"#",
"实验室管理"
],
"violateScenePercentage": [
"21.05%",
"15.79%",
"10.53%",
"5.26%",
"5.26%",
"15.79%",
"10.53%",
"5.26%",
"10.53%",
"5.26%",
"5.26%"
],
"violateSceneType": [
"关联关系",
"第二职业",
"代采购",
"项目成员公示",
"吃空饷",
"工作纪律违规",
"其它",
"门禁管理",
"其它",
"竞标违规",
"闲置资产"
],
"violateSceneCount": [
"4",
"3",
"2",
"1",
"1",
"3",
"2",
"1",
"2",
"1",
"1"
]
},
"code": 200,
"message": "OK"
}