【java】關於Map的排序性的一次使用,有序的Map


關於Map的排序性的一次使用,有序的Map

>>>>>

hashmap是按key的hash碼排序的,而treemap是利用comparator 進行key的自然排序的

 

/**
     * 根據商品分類ID
     * 獲取長編碼,拿到向上的所有父級商品分類ID
     *
     * 根據parentUidList  獲取 parentList
     *
     * 迭代parentUid 根據uid 獲取在有序List中的 下標
     *
     * 按照[k:v][下標:具體信息]
     * 放入TreeMap 即得到有序的父層商品分類層級信息
     * @param entity
     * @return
     */
    @Override
    public AjaxResult<Map<Integer, GoodsTypeSimpleBean>> findGoodsTypeTree(GoodsType entity) {
        LunaResultBean.checkField(entity,"uid");
        TenementUser tenementUser = RequestData.TENEMENT_USER.get();
        AjaxResult<Map<Integer,GoodsTypeSimpleBean>> res = new AjaxResult<>();
        GoodsType goodsType = goodsTypeService.get(entity.getUid());
        if (goodsType != null && tenementUser.getTenementId().equals(goodsType.getTenementId())){
            String longCode = goodsType.getLangCode();
            List<String> parentUidList = Arrays.asList(longCode.split(":"));
            List<GoodsType> parentList = goodsTypeService.findByUidIn(parentUidList);

            Map<Integer,GoodsTypeSimpleBean> sortMap = new TreeMap<>();
            for (GoodsType type : parentList) {
                GoodsTypeSimpleBean bean = new GoodsTypeSimpleBean();
                bean.setGoodsTypeName(type.getName());
                bean.setOuterCode(type.getOuterCode());

                int index = parentUidList.indexOf(type.getUid())+1;
                sortMap.put(index,bean);
            }

            res.initTrue(sortMap);
        }else {
            res.initFalse("商品分類不存在",LunaResultBean.ERROR_BUSINESS);
        }
        return res;
    }
View Code

 

獲取結果如下:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM