矩阵数据_树数据结构


1、前言

  1. 我不确定是否是矩阵数据, 烦请各位留言指正. 但该数据的处理方案自我觉得确实可以记录为一篇博客

  2. 矩阵数据 转换为 树数据结构

  3. 代码地址


2、预处理数据

  1. 多个json数组
  2. 不确定的: key, value, sort值不固定, 数组长度不固定
  3. 确定的: json数组的key值固定, sort值固定. 第一个json数组sort=1, key=充值类型, 第二个json数组sort=1, key=充值类型
[{"key":"充值类型","sort":1,"value":"铂金会员"},{"key":"时间","sort":2,"value":"3个月"},{"key":"套餐","sort":3,"value":"赠送QQ会员"}],
[{"key":"充值类型","sort":1,"value":"钻石会员"},{"key":"时间","sort":2,"value":"3个月"},{"key":"套餐","sort":3,"value":"赠送网易会员"}],
[{"key":"充值类型","sort":1,"value":"铂金会员"},{"key":"时间","sort":2,"value":"3个月"},{"key":"套餐","sort":3,"value":"赠送腾讯会员"}],
[{"key":"充值类型","sort":1,"value":"铂金会员"},{"key":"时间","sort":2,"value":"3个月"},{"key":"套餐","sort":3,"value":"赠送京东会员"}],
[{"key":"充值类型","sort":1,"value":"铂金会员"},{"key":"时间","sort":2,"value":"4个月"},{"key":"套餐","sort":3,"value":"赠送酷狗会员"}],
[{"key":"充值类型","sort":1,"value":"铂金会员"},{"key":"时间","sort":2,"value":"2个月"},{"key":"套餐","sort":3,"value":"赠送微信会员"}],
[{"key":"充值类型","sort":1,"value":"钻石会员"},{"key":"时间","sort":2,"value":"3个月"},{"key":"套餐","sort":3,"value":"赠送腾讯会员"}],
[{"key":"充值类型","sort":1,"value":"钻石会员"},{"key":"时间","sort":2,"value":"2个月"},{"key":"套餐","sort":3,"value":"赠送微信会员"}]


3、期望得到数据结构

  1. 转换为树数据结构
  2. (sort排序, value相同的去重, 对应的 sort+1 数据做子数据, 子数据绑定对应的父级) 循环该操作, 直至所有数据处理完毕
[{
	"mark": "充值类型",
	"name": "铂金会员",
	"saleAttributeVOList": [{
		"mark": "时间",
		"name": "3个月",
		"saleAttributeVOList": [{
			"mark": "套餐",
			"name": "赠送QQ会员",
			"sort": 3
		}, {
			"mark": "套餐",
			"name": "赠送腾讯会员",
			"saleAttributeVOList": [],
			"sort": 3
		}, {
			"mark": "套餐",
			"name": "赠送京东会员",
			"saleAttributeVOList": [],
			"sort": 3
		}],
		"sort": 2
	}, {
		"mark": "时间",
		"name": "4个月",
		"saleAttributeVOList": [{
			"mark": "套餐",
			"name": "赠送酷狗会员",
			"saleAttributeVOList": [],
			"sort": 3
		}],
		"sort": 2
	}, {
		"mark": "时间",
		"name": "2个月",
		"saleAttributeVOList": [{
			"mark": "套餐",
			"name": "赠送微信会员",
			"saleAttributeVOList": [],
			"sort": 3
		}],
		"sort": 2
	}],
	"sort": 1
}, {
	"mark": "充值类型",
	"name": "钻石会员",
	"saleAttributeVOList": [{
		"mark": "时间",
		"name": "3个月",
		"saleAttributeVOList": [{
			"mark": "套餐",
			"name": "赠送网易会员",
			"sort": 3
		}, {
			"mark": "套餐",
			"name": "赠送腾讯会员",
			"saleAttributeVOList": [],
			"sort": 3
		}],
		"sort": 2
	}, {
		"mark": "时间",
		"name": "2个月",
		"saleAttributeVOList": [{
			"mark": "套餐",
			"name": "赠送微信会员",
			"saleAttributeVOList": [],
			"sort": 3
		}],
		"sort": 2
	}],
	"sort": 1
}]


4、几个难点

1. 如何将这个未知数据处理完毕
2. 如何获取上一级数据
3. 如何获取下一级数据
4. 如何赋值给上一级


5、示列代码

  1. 示列数据可至博客地址获取, 贴上来太长了:https://gitee.com/xmaxm/test-code/blob/master/chaim-special-data/src/main/java/com/chaim/chaimspecialdata/matrix/MatrixData.java
package com.chaim.chaimspecialdata.matrix;

import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

//@formatter:off
/**
 * 处理矩阵数据
 *
 * 源数据(key, value不固定, 规格不固定, 可能更多):
 *      [{"key":"充值类型","sort":1,"value":"铂金会员"},{"key":"时间","sort":2,"value":"3个月"},{"key":"套餐","sort":3,"value":"赠送QQ会员"}],
 *      [{"key":"充值类型","sort":1,"value":"钻石会员"},{"key":"时间","sort":2,"value":"3个月"},{"key":"套餐","sort":3,"value":"赠送网易会员"}],
 *      [{"key":"充值类型","sort":1,"value":"铂金会员"},{"key":"时间","sort":2,"value":"3个月"},{"key":"套餐","sort":3,"value":"赠送腾讯会员"}],
 *      [{"key":"充值类型","sort":1,"value":"铂金会员"},{"key":"时间","sort":2,"value":"3个月"},{"key":"套餐","sort":3,"value":"赠送京东会员"}],
 *      [{"key":"充值类型","sort":1,"value":"铂金会员"},{"key":"时间","sort":2,"value":"4个月"},{"key":"套餐","sort":3,"value":"赠送酷狗会员"}],
 *      [{"key":"充值类型","sort":1,"value":"铂金会员"},{"key":"时间","sort":2,"value":"2个月"},{"key":"套餐","sort":3,"value":"赠送微信会员"}],
 *      [{"key":"充值类型","sort":1,"value":"钻石会员"},{"key":"时间","sort":2,"value":"3个月"},{"key":"套餐","sort":3,"value":"赠送腾讯会员"}],
 *      [{"key":"充值类型","sort":1,"value":"钻石会员"},{"key":"时间","sort":2,"value":"2个月"},{"key":"套餐","sort":3,"value":"赠送微信会员"}]
 *
 * 期望类型(sort相同进行过滤去重, 子集做子数据, 已树形结构展示):
 *      [
 *      {"mark":"充值类型","name":"铂金会员","saleAttributeVOList":[
 *              {"mark":"时间","name":"3个月","saleAttributeVOList":[
 *                      {"mark":"套餐","name":"赠送QQ会员","sort":3},
 *                      {"mark":"套餐","name":"赠送腾讯会员","sort":3},
 *                      {"mark":"套餐","name":"赠送京东会员","sort":3}
 *              ],"sort":2},
 *              {"mark":"时间","name":"4个月","saleAttributeVOList":[
 *                      {"mark":"套餐","name":"赠送酷狗会员","sort":3}
 *               ],"sort":2},
 *              {"mark":"时间","name":"2个月","saleAttributeVOList":[
 *                      {"mark":"套餐","name":"赠送微信会员","sort":3}],"sort":2}
 *              ],"sort":1},
 *       {"mark":"充值类型","name":"钻石会员","saleAttributeVOList":[
 *             {"mark":"时间","name":"3个月","saleAttributeVOList":[
 *                      {"mark":"套餐","name":"赠送网易会员","sort":3},
 *                      {"mark":"套餐","name":"赠送腾讯会员","sort":3}
 *             ],"sort":2},
 *             {"mark":"时间","name":"2个月","saleAttributeVOList":[
 *                      {"mark":"套餐","name":"赠送微信会员","sort":3}],"sort":2}
 *             ],"sort":1}
 *     ]
 *
 * @author Chaim
 * @date 2022/7/30 23:36
 * @Description
 */
public class MatrixData {
    /**
     * 几个点:
     *      1. 如何将这个未知数据处理完毕
     *      2. 如何获取上一级数据
     *      3. 如何赋值给上一级
     *      4. 注释(saleAttributeVO != null)内外的没有联系, 大致逻辑差不多可以参考
     * 处理逻辑
     * 注释逻辑为列: [{"key":"充值类型","sort":1,"value":"铂金会员"},{"key":"时间","sort":2,"value":"3个月"},{"key":"套餐","sort":3,"value":"赠送QQ会员"}]
     *
     * @param args
     */
//@formatter:on
    public static void main(String[] args) {
        // 模拟数据
        List<SaleAttributeBO> saleAttributeBOList = getData();

        // 最终类型
        List<SaleAttributeVO> resultList = new ArrayList<>();
        for (SaleAttributeBO saleAttributeBO : saleAttributeBOList) {
            // 待处理数据
            List<SaleAttributeBO.SaleAttribute> saleAttributeList = saleAttributeBO.getSaleAttributeBO();
            saleAttributeList.sort(Comparator.comparing(SaleAttributeBO.SaleAttribute::getSort));

            // 待处理数据第一条. 第一条数据的处理方式会不一样, 他会涉及到一个类似初始化的过程    ({"key":"充值类型","sort":1,"value":"铂金会员"})
            SaleAttributeBO.SaleAttribute saleAttribute = saleAttributeList.get(0);
            // 第一条的数据值  ("value":"铂金会员")
            String value = saleAttribute.getValue();
            // 判断第一条数据值是否已经初始化过, 或者返回数据中是否已经记录了该数据, 存在就忽略该数据
            SaleAttributeVO saleAttributeVO = resultList.stream().filter(versionVO -> value.equals(versionVO.getName())).findAny().orElse(null);
            if (saleAttributeVO != null) {
                // 开始处理子数据, 从第二个数据开始处理
                for (int i = 1; i < saleAttributeList.size(); i++) {
                    // 当前待处理数据. 比如当前待处理数据 ({"key":"套餐","sort":3,"value":"赠送QQ会员"})
                    SaleAttributeVO recursion = getRecursion(saleAttributeList.get(i));
                    // 初始化: 初始化数据的第一层数据. 递归赋值当前数据上层数据 (while之后的数据{"mark": "时间", "name": "3个月", "saleAttributeVOList": []})
                    List<SaleAttributeVO> saleAttributeVOList = saleAttributeVO.getSaleAttributeVOList();
                    // while结束条件, 做递归操作, 一直获取最低程的子数据
                    int j = i;
                    // 获取上一层数据的条件
                    int n = 1;
                    // 下面初始化数据有注释
                    while (j > 1) {
                        // 当前待处理数据的上一层数据, ({"key":"时间","sort":2,"value":"3个月"})
                        SaleAttributeVO recursionReturn = getRecursion(saleAttributeList.get(n));
                        // 判断待处理的上一层数据是否已经在树中存在
                        SaleAttributeVO versionVO = saleAttributeVOList.stream().filter(vo -> recursionReturn.getName().equals(vo.getName())).findAny().orElse(null);
                        // null的情况不需要处理, 因为你的数据不可能存在: 一级二级三级数据中没有二级数据的情况
                        if (versionVO != null) {
                            // 存在就将上一层的数据的子级数据重新赋值给 saleAttributeVOList , 只有这样才能处理如何获取上一级数据, 并做赋值操作
                            saleAttributeVOList = versionVO.getSaleAttributeVOList();
                        }
                        j--;
                        n++;
                    }

                    // 判断当前待处理数据是否存在子级数据中, 不存在就做子级数据赋值
                    SaleAttributeVO versionVO = saleAttributeVOList.stream().filter(vo -> recursion.getName().equals(vo.getName())).findAny().orElse(null);
                    if (versionVO == null) {
                        // 用于处理子级数据为null导致空指针
                        recursion.setSaleAttributeVOList(new ArrayList<>());
                        saleAttributeVOList.add(recursion);
                    }
                }
                continue;
            }

            // 初始化数据
            SaleAttributeVO attributeVO = new SaleAttributeVO();
            attributeVO.setSort(saleAttribute.getSort());
            attributeVO.setMark(saleAttribute.getKey());
            attributeVO.setName(saleAttribute.getValue());
            // 处理的第一条数据没有初始化过, 证明该数据的子集都可以直接做成子数据
            for (int i = 1; i < saleAttributeList.size(); i++) {
                // 待处理子数据
                SaleAttributeVO recursionReturn = getRecursion(saleAttributeList.get(i));
                List<SaleAttributeVO> saleAttributeArrayList = new ArrayList<>();
                // 第一条数据的子数据赋值, 用于递归处理
                // 注: attributeVO该值是已经持久化到attributeVO, 136行
                SaleAttributeVO versionVO = attributeVO;
                // while结束条件, 做递归操作, 一直获取最低程的子数据
                int j = i;
                // j = i; j > 1.
                // 我们需要的是一个树形数据, 列:
                // 当j=1时, recursionReturn是第一层数据("key":"时间"), 故while条件不成立, 可直接赋值给父级, 做树二级
                // 当j=2时, j--; while最终是执行一次, recursionReturn是第二层数据("key":"套餐"), 获取的上一级数据是第二层("key":"时间"), 做树三级
                while (j > 1) {
                    // 获取上一条数据的子集, 从新赋值,
                    // 初始化数据所以都是get(0)
                    versionVO = versionVO.getSaleAttributeVOList().get(0);
                    j--;
                }
                // 子数据加入父级
                saleAttributeArrayList.add(recursionReturn);
                versionVO.setSaleAttributeVOList(saleAttributeArrayList);
            }
            resultList.add(attributeVO);
        }
        System.out.println(JSONObject.toJSONString(resultList));
    }

    /**
     * 装换数据
     *
     * @param saleAttributeBO
     * @return
     */
    public static SaleAttributeVO getRecursion(SaleAttributeBO.SaleAttribute saleAttributeBO) {
        SaleAttributeVO vo = new SaleAttributeVO();
        vo.setMark(saleAttributeBO.getKey());
        vo.setName(saleAttributeBO.getValue());
        vo.setSort(saleAttributeBO.getSort());
        return vo;
    }

    /**
     * 生成模拟数据
     *
     * @return
     */
    public static List<SaleAttributeBO> getData() {
        List<SaleAttributeBO> skuStockClientVOList = new ArrayList<>();

        /**
         * 贴上来太长了, 而且这个示列数据也不重要
         * 示列数据可至博客地址获取:
         * https://gitee.com/xmaxm/test-code/tree/master/chaim-special-data/src/main/java/com/chaim/special/data/matrix
         */
        SaleAttributeBO saleAttributeBO8 = new SaleAttributeBO();
        List<SaleAttributeBO.SaleAttribute> attributeArrayList8 = new ArrayList<>();
        SaleAttributeBO.SaleAttribute saleAttributeEight = new SaleAttributeBO.SaleAttribute();
        saleAttributeEight.setKey("充值类型");
        saleAttributeEight.setValue("钻石会员");
        saleAttributeEight.setSort(1);
        attributeArrayList8.add(saleAttributeEight);
        SaleAttributeBO.SaleAttribute saleAttributeEight2 = new SaleAttributeBO.SaleAttribute();
        saleAttributeEight2.setKey("时间");
        saleAttributeEight2.setValue("2个月");
        saleAttributeEight2.setSort(2);
        attributeArrayList8.add(saleAttributeEight2);
        SaleAttributeBO.SaleAttribute saleAttributeEight3 = new SaleAttributeBO.SaleAttribute();
        saleAttributeEight3.setKey("套餐");
        saleAttributeEight3.setValue("赠送微信会员");
        saleAttributeEight3.setSort(3);
        attributeArrayList8.add(saleAttributeEight3);
        SaleAttributeBO.SaleAttribute saleAttributeEight4 = new SaleAttributeBO.SaleAttribute();
        saleAttributeEight4.setKey("爆率");
        saleAttributeEight4.setValue("30%");
        saleAttributeEight4.setSort(4);
        attributeArrayList8.add(saleAttributeEight4);
        SaleAttributeBO.SaleAttribute saleAttributeEight5 = new SaleAttributeBO.SaleAttribute();
        saleAttributeEight5.setKey("武器类型");
        saleAttributeEight5.setValue("传奇");
        saleAttributeEight5.setSort(5);
        attributeArrayList8.add(saleAttributeEight5);
        saleAttributeBO8.setSaleAttributeBO(attributeArrayList8);

        skuStockClientVOList.add(saleAttributeBO);
        skuStockClientVOList.add(saleAttributeBO2);
        skuStockClientVOList.add(saleAttributeBO3);
        skuStockClientVOList.add(saleAttributeBO4);
        skuStockClientVOList.add(saleAttributeBO5);
        skuStockClientVOList.add(saleAttributeBO6);
        skuStockClientVOList.add(saleAttributeBO7);
        skuStockClientVOList.add(saleAttributeBO8);

        return skuStockClientVOList;
    }
}


6、备注

  1. 处理起来可能还是稍微有点点复杂, 需要对数据整体的结构有一个很清晰的认知
  2. 如果只是处理两层, 三层, 或者说固定长度数据, 到确实简单, 但是当其长度不确定的时候, 就会稍微复杂一些了
  3. 可能对对象的赋值, 内存结构有了更深的认知吧
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值