ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java-多节点的上下移解决方案

2021-07-25 02:00:27  阅读:180  来源: 互联网

标签:departmentMoveDtoList Short java idList static 下移 new idSet 节点


import java.util.*;

/**
 * @Author 59456
 * @Date 2021/7/24
 * @Descrip 解决离散或者相邻部门上下的问题,时间复杂度O(N)、空间复杂度O(1)
 * @Version 1.0
 */
public class Solution {

    static Short MOVE_UP = 1;
    static Short MOVE_DOWN = 0;
    static List<Long> idList = new ArrayList<>();
    static Set<Long> idSet = new HashSet<>();
    static List<DepartmentMoveDto> departmentMoveDtoList = new ArrayList<>();

    static {
        // 设置原始有序数据集
        for(Long i=0L;i<10L;i++){
            DepartmentMoveDto departmentMoveDto = new DepartmentMoveDto();
            departmentMoveDto.setId(i);
            departmentMoveDto.setSort(Short.valueOf(i.toString()));
            departmentMoveDto.setSortIndex(Double.valueOf(i.toString()));
            departmentMoveDtoList.add(departmentMoveDto);
        }

        // 设置需要移动的节点id
        // 其中0、1是一个空气泡,5、6是另外一个空气泡
        idList.add(0L);
        idList.add(1L);
        idList.add(5L);
        idList.add(6L);
        idList.add(9L);

    }


    // 逆向思考:另外的节点移动
    public static void move(List<Long> idList,Short moveType,Long parentId){
        if(null == idList || idList.isEmpty()){
            return;
        }

        // 获取同级部门排序信息
        //        List<DepartmentMoveDto> departmentMoveDtoList = departmentDao.getDepartmentMoveDtoListByParentId(parentId);
        idSet = new HashSet<>(idList);

        if(MOVE_UP.equals(moveType)){
            moveUp(departmentMoveDtoList ,idSet);
        }else {
            Collections.reverse(departmentMoveDtoList);
            moveUp(departmentMoveDtoList ,idSet);
            Collections.reverse(departmentMoveDtoList);
        }


    }

    /**
     * 专门针对上移设计(其实下移也是另外一个视角的上移)
     * 核心思想聚焦需要移动节点,上方相邻的普通节点(本质也可以看做这些节点的向下冒泡)
     * @param departmentMoveDtoList 部门移动信息
     * @param idSet 需要移动的部门id
     */
    private static void moveUp(List<DepartmentMoveDto> departmentMoveDtoList,Set<Long> idSet){
        int length = departmentMoveDtoList.size();
        for(int index = 0; index<length-1; index++){
            DepartmentMoveDto departmentMoveDtoPre = departmentMoveDtoList.get(index);
            Long idPre = departmentMoveDtoPre.getId();

            DepartmentMoveDto departmentMoveDtoNext = departmentMoveDtoList.get(index+1);
            Long idNext = departmentMoveDtoNext.getId();

            // 如果当前不移动,下一个移动:交换
            if (!idSet.contains(idPre) && idSet.contains(idNext)) {
                DepartmentMoveDto departmentMoveDtoSwitch = departmentMoveDtoList.get(index);
                departmentMoveDtoList.set(index, departmentMoveDtoList.get(index + 1));
                departmentMoveDtoList.set(index + 1,departmentMoveDtoSwitch);
            }
        }
    }


    public static void main(String[] args) {
        departmentMoveDtoList.forEach(entity->{
            System.out.println(entity.toString());
        });
        System.out.println("hello world!");

        // 部门下移
        move( idList, new Short("0"), 0L);

        departmentMoveDtoList.forEach(entity->{
            System.out.println(entity.toString());
        });
    }

}

标签:departmentMoveDtoList,Short,java,idList,static,下移,new,idSet,节点
来源: https://www.cnblogs.com/Mufasa/p/15056939.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有