递归排序的一种实现方法。

  1. import java.util.Comparator;
  2. import com.work.qxgl.model.QxglDept;
  3. /**
  4.  * @author wangmingjie
  5.  * @date 2008-9-5上午10:33:59
  6.  */
  7. public class QxglDeptCompartor implements Comparator<QxglDept> {
  8.     public int compare(QxglDept o1, QxglDept o2) {
  9.             return o1.getDeptIntroduce().compareTo(o2.getDeptIntroduce());
  10.     
  11.     }
  12. }
 
  1. import java.util.LinkedList;
  2. import java.util.List;
  3. import java.util.Arrays;
  4. import com.work.qxgl.model.QxglDept;
  5. /**
  6.  * 递归排序(测试成功!)
  7.  * 前提:知道了父节点,知道了节点所在的级别,级别内部的排序号是整数且都是唯一的。
  8.  * @author wangmingjie
  9.  * @date 2008-9-4下午10:05:17
  10.  */
  11. public class SelectTreeTwo {
  12.     // 崔的算法:首先,增加一个属性用来组成排序的字符串的,将父结构的排序id组成字符串,规定从第1级开始排序号要定长。(最顶级为0级)
  13.     // 然后将组合后的排序字符串排序,递归排序就实现了。
  14.     //这种算法的同一级别内排序号必须不一样才可以,否则排序将失败!
  15.     
  16.     public static List<QxglDept> init() {
  17.         List<QxglDept> l = new LinkedList<QxglDept>();
  18.         //QxglDept  id,名称,父id,级别,排序号  (deptIntroduce是辅助字段)
  19.         QxglDept temp = new QxglDept("0""中国"null00);
  20.         l.add(temp);
  21.         // =====================第一级==========================
  22.         temp = new QxglDept("2""山东省""0"12);
  23.         l.add(temp);
  24.         temp = new QxglDept("3""河北省""0"13);
  25.         l.add(temp);
  26.         temp = new QxglDept("1""北京市""0"11);
  27.         l.add(temp);
  28.         // 0-1-11
  29.         // 0-10-1
  30.         // 0-9-1
  31.         // =====================第二级==========================
  32.         temp = new QxglDept("11""市辖区""1"211);
  33.         l.add(temp); // ,"0-1-11"
  34.         temp = new QxglDept("12""县""1"212);
  35.         l.add(temp); // ,"0-1-12"
  36.         temp = new QxglDept("21""济南市""2"21);
  37.         l.add(temp);
  38.         temp = new QxglDept("23""潍坊市""2"23);
  39.         l.add(temp);
  40.         temp = new QxglDept("22""青岛市""2"22);
  41.         l.add(temp);
  42.         temp = new QxglDept("32""唐山市""3"232);
  43.         l.add(temp);
  44.         temp = new QxglDept("31""石家庄""3"231);
  45.         l.add(temp);
  46.         // =======================第三级=================================
  47.         temp = new QxglDept("112""朝阳区""11"3112);
  48.         l.add(temp);
  49.         temp = new QxglDept("111""东城区""11"3111);
  50.         l.add(temp);
  51.         temp = new QxglDept("113""西城区""11"3113);
  52.         l.add(temp);
  53.         temp = new QxglDept("122""延庆县""12"3122);
  54.         l.add(temp);
  55.         temp = new QxglDept("121""密云县""12"3121);
  56.         l.add(temp);
  57.         temp = new QxglDept("213""天桥区""21"3213);
  58.         l.add(temp);
  59.         temp = new QxglDept("212""市中区""21"3212);
  60.         l.add(temp);
  61.         temp = new QxglDept("211""历下区""21"3211);
  62.         l.add(temp);
  63.         return l;
  64.     }
  65.     /**
  66.      * 入口方法main
  67.      * @param args
  68.      */
  69.     public static void main(String[] args) {
  70.         long start = System.currentTimeMillis();
  71.         LinkedList<QxglDept> l = (LinkedList<QxglDept>) SelectTreeTwo.init(); //
  72.         int LEN = l.size();
  73.         System.out.println("目标list大小为:" + LEN);
  74.         for (int i = 0; i < LEN; i++) {
  75.             setOrderString(l, l.get(i), i);
  76.         }
  77.         sortList(l);
  78.         printTree(l);
  79.         System.out.println("总共花费"+(System.currentTimeMillis()-start)+"毫秒");
  80.     }
  81.     /**
  82.      * 根据DeptIntroduce,由小到大将list排序
  83.      * 
  84.      * @param l
  85.      */
  86.     public static void sortList( LinkedList<QxglDept> l) {
  87.         //首先转换成数组
  88.         QxglDept[] dest = new QxglDept[l.size()];
  89.         dest = (QxglDept[]) l.toArray(dest);
  90.         Arrays.sort(dest,new QxglDeptCompartor());
  91.         int LEN = l.size();
  92.         l.clear(); //必须先清空,然后按照数组的顺序增加
  93.         for (int i = 0; i < LEN; i++) {
  94.             l.add(i,dest[i]);
  95.         }
  96.     }
  97.     /**
  98.      * 找到dept的父节点
  99.      * 
  100.      * @param l
  101.      * @param dept
  102.      * @return
  103.      */
  104.     public static int getParentIndex(LinkedList<QxglDept> l, QxglDept dept) {
  105.         int LEN = l.size();
  106.         int result = -1;
  107.         String parentId = dept.getDeptParentId();
  108.         for (int i = 0; i < LEN; i++) {
  109.             if (parentId.equals(l.get(i).getId())) {
  110.                 result = i;
  111.                 break;
  112.             } else {
  113.                 continue;
  114.             }
  115.         }
  116.         return result;
  117.     }
  118.     /**
  119.      * 生成排序号的字符串
  120.      * 
  121.      * @param l
  122.      * @param dept
  123.      * @param pos
  124.      */
  125.     public static void setOrderString(LinkedList<QxglDept> l, QxglDept dept,
  126.             int pos) {
  127.         if (pos == 0) {
  128.             dept.setDeptIntroduce(dept.getDeptOrderId() + "");
  129.         } else {
  130.             // 首先找到父节点,获取到他的DeptIntroduce,对自己当前的排序号补零;
  131.             QxglDept parentNode = l.get(getParentIndex(l, dept));
  132.             dept.setDeptIntroduce(parentNode.getDeptIntroduce() + "-"
  133.                     + getOrderString(dept.getDeptOrderId()));
  134.         }
  135.     }
  136.     /**
  137.      * 2^31= 2147483648 ,最大为10位
  138.      * 
  139.      * @param orderId
  140.      * @return
  141.      */
  142.     public static String getOrderString(int orderId) {
  143.         String temp = "";
  144.         int LEN = (orderId + "").length();
  145.         for (int i = 0; i < 10 - LEN; i++) {
  146.             temp = temp + "0";
  147.         }
  148.         temp = temp + orderId;
  149.         return temp;
  150.     }
  151.     public static void print(List<QxglDept> l) {
  152.         for (int i = 0; i < l.size(); i++) {
  153.             QxglDept temp = l.get(i);
  154.             System.out.println(temp.getDeptName() + "||"
  155.                     + temp.getDeptIntroduce());
  156.         }
  157.     }
  158.     /**
  159.      * 打印树状菜单
  160.      * @param l
  161.      */
  162.     public static void printTree(List<QxglDept> l) {
  163.         for (int i = 0; i < l.size(); i++) {
  164.             QxglDept temp = l.get(i);
  165.             if (temp.getDeptLevel() == 0)
  166.                 System.out.println(l.get(i).getDeptName());
  167.             else {
  168.                 for (int j = 0; j < temp.getDeptLevel() - 1; j++) {
  169.                     System.out.print(" ");//补齐空格,在html中最好使用全角空格(汉字空格)。
  170.                 }
  171.                 System.out.println("┣" + l.get(i).getDeptName());
  172.             }
  173.         }
  174.     }
  175. }

需要的pojo:

  1. public class QxglDept implements Serializable {
  2.     /**
  3.      * 
  4.      */
  5.     private static final long serialVersionUID = -1536071285282466850L;
  6.     // constructors
  7.     public QxglDept() {
  8.         initialize();
  9.     }
  10.     /**
  11.      * Constructor for primary key
  12.      */
  13.     public QxglDept(java.lang.String id) {
  14.         this.setId(id);
  15.         initialize();
  16.     }
  17.     /**
  18.      * Constructor for required fields
  19.      */
  20.     public QxglDept(java.lang.String id, java.lang.String deptName,
  21.             java.lang.String deptParentId, int deptLevel,
  22.             int deptOrderId) {
  23.         this.setId(id);
  24.         this.setDeptName(deptName);
  25.         this.setDeptParentId(deptParentId);
  26.         this.setDeptLevel(deptLevel);
  27.         this.setDeptOrderId(deptOrderId);
  28.         initialize();
  29.     }
  30.     
  31.     protected void initialize() {
  32.     }
  33.     private int hashCode = Integer.MIN_VALUE;
  34.     // primary key
  35.     private java.lang.String id;
  36.     // fields
  37.     private java.lang.String deptName;
  38.     private java.lang.String deptParentId;// 父部门的id
  39.     private int deptLevel; // 部门级别
  40.     private java.lang.String deptIdPath; // id全路径
  41.     private java.lang.String deptFullname;// 部门全称
  42.     private int deptOrderId; // 排序id,要能够调整
  43.     // private String deptCreateTime; //创建时间 ,由数据库自己来维护
  44.     // collections
  45.     private String deptArea; // 所在地区
  46.     private String deptType;// 部门类别
  47.     private String deptLinkman;// 联系人
  48.     private String deptLinkmanphone;// 联系人电话
  49.     private String deptEmail;// 部门电子邮箱
  50.     private String deptPhone;// 部门电话
  51.     private String deptFax;// 部门传真
  52.     private String deptAddress;// 部门地址
  53.     private String deptPostalcode;// 部门邮编
  54.     private String deptIntroduce;// 部门简介
  55.     
  56.     /**
  57.      * @return the deptAddress
  58.      */
  59.     public String getDeptAddress() {
  60.         return deptAddress;
  61.     }
  62.     /**
  63.      * @param deptAddress
  64.      *            the deptAddress to set
  65.      */
  66.     public void setDeptAddress(String deptAddress) {
  67.         this.deptAddress = deptAddress;
  68.     }
  69.     /**
  70.      * @return the deptEmail
  71.      */
  72.     public String getDeptEmail() {
  73.         return deptEmail;
  74.     }
  75.     /**
  76.      * @param deptEmail
  77.      *            the deptEmail to set
  78.      */
  79.     public void setDeptEmail(String deptEmail) {
  80.         this.deptEmail = deptEmail;
  81.     }
  82.     /**
  83.      * @return the deptFax
  84.      */
  85.     public String getDeptFax() {
  86.         return deptFax;
  87.     }
  88.     /**
  89.      * @param deptFax
  90.      *            the deptFax to set
  91.      */
  92.     public void setDeptFax(String deptFax) {
  93.         this.deptFax = deptFax;
  94.     }
  95.     /**
  96.      * @return the deptLinkman
  97.      */
  98.     public String getDeptLinkman() {
  99.         return deptLinkman;
  100.     }
  101.     /**
  102.      * @param deptLinkman
  103.      *            the deptLinkman to set
  104.      */
  105.     public void setDeptLinkman(String deptLinkman) {
  106.         this.deptLinkman = deptLinkman;
  107.     }
  108.     /**
  109.      * @return the deptLinkmanphone
  110.      */
  111.     public String getDeptLinkmanphone() {
  112.         return deptLinkmanphone;
  113.     }
  114.     /**
  115.      * @param deptLinkmanphone
  116.      *            the deptLinkmanphone to set
  117.      */
  118.     public void setDeptLinkmanphone(String deptLinkmanphone) {
  119.         this.deptLinkmanphone = deptLinkmanphone;
  120.     }
  121.     /**
  122.      * @return the deptPhone
  123.      */
  124.     public String getDeptPhone() {
  125.         return deptPhone;
  126.     }
  127.     /**
  128.      * @param deptPhone
  129.      *            the deptPhone to set
  130.      */
  131.     public void setDeptPhone(String deptPhone) {
  132.         this.deptPhone = deptPhone;
  133.     }
  134.     /**
  135.      * @return the deptPostalcode
  136.      */
  137.     public String getDeptPostalcode() {
  138.         return deptPostalcode;
  139.     }
  140.     /**
  141.      * @param deptPostalcode
  142.      *            the deptPostalcode to set
  143.      */
  144.     public void setDeptPostalcode(String deptPostalcode) {
  145.         this.deptPostalcode = deptPostalcode;
  146.     }
  147.     /**
  148.      * @return the deptType
  149.      */
  150.     public String getDeptType() {
  151.         return deptType;
  152.     }
  153.     /**
  154.      * @param deptType
  155.      *            the deptType to set
  156.      */
  157.     public void setDeptType(String deptType) {
  158.         this.deptType = deptType;
  159.     }
  160.     /**
  161.      * Return the unique identifier of this class
  162.      * 
  163.      * @hibernate.id generator-class="uuid" column="dept_id"
  164.      */
  165.     public java.lang.String getId() {
  166.         return id;
  167.     }
  168.     /**
  169.      * Set the unique identifier of this class
  170.      * 
  171.      * @param id
  172.      *            the new ID
  173.      */
  174.     public void setId(java.lang.String id) {
  175.         this.id = id;
  176.         this.hashCode = Integer.MIN_VALUE;
  177.     }
  178.     /**
  179.      * Return the value associated with the column: dept_name
  180.      */
  181.     public java.lang.String getDeptName() {
  182.         return deptName;
  183.     }
  184.     /**
  185.      * Set the value related to the column: dept_name
  186.      * 
  187.      * @param deptName
  188.      *            the dept_name value
  189.      */
  190.     public void setDeptName(java.lang.String deptName) {
  191.         this.deptName = deptName;
  192.     }
  193.     /**
  194.      * Return the value associated with the column: dept_parent_id
  195.      */
  196.     public java.lang.String getDeptParentId() {
  197.         return deptParentId;
  198.     }
  199.     /**
  200.      * Set the value related to the column: dept_parent_id
  201.      * 
  202.      * @param deptParentId
  203.      *            the dept_parent_id value
  204.      */
  205.     public void setDeptParentId(java.lang.String deptParentId) {
  206.         this.deptParentId = deptParentId;
  207.     }
  208.     /**
  209.      * Return the value associated with the column: dept_level
  210.      */
  211.     public int getDeptLevel() {
  212.         return deptLevel;
  213.     }
  214.     /**
  215.      * Set the value related to the column: dept_level
  216.      * 
  217.      * @param deptLevel
  218.      *            the dept_level value
  219.      */
  220.     public void setDeptLevel(int deptLevel) {
  221.         this.deptLevel = deptLevel;
  222.     }
  223.     /**
  224.      * Return the value associated with the column: dept_id_path
  225.      */
  226.     public java.lang.String getDeptIdPath() {
  227.         return deptIdPath;
  228.     }
  229.     /**
  230.      * Set the value related to the column: dept_id_path
  231.      * 
  232.      * @param deptIdPath
  233.      *            the dept_id_path value
  234.      */
  235.     public void setDeptIdPath(java.lang.String deptIdPath) {
  236.         this.deptIdPath = deptIdPath;
  237.     }
  238.     /**
  239.      * Return the value associated with the column: dept_fullname
  240.      */
  241.     public java.lang.String getDeptFullname() {
  242.         return deptFullname;
  243.     }
  244.     /**
  245.      * Set the value related to the column: dept_fullname
  246.      * 
  247.      * @param deptFullname
  248.      *            the dept_fullname value
  249.      */
  250.     public void setDeptFullname(java.lang.String deptFullname) {
  251.         this.deptFullname = deptFullname;
  252.     }
  253.     public boolean equals(Object obj) {
  254.         if (null == obj)
  255.             return false;
  256.         if (!(obj instanceof QxglDept))
  257.             return false;
  258.         else {
  259.             QxglDept qxglDept = (QxglDept) obj;
  260.             if (null == this.getId() || null == qxglDept.getId())
  261.                 return false;
  262.             else
  263.                 return (this.getId().equals(qxglDept.getId()));
  264.         }
  265.     }
  266.     public int hashCode() {
  267.         if (Integer.MIN_VALUE == this.hashCode) {
  268.             if (null == this.getId())
  269.                 return super.hashCode();
  270.             else {
  271.                 String hashStr = this.getClass().getName() + ":"
  272.                         + this.getId().hashCode();
  273.                 this.hashCode = hashStr.hashCode();
  274.             }
  275.         }
  276.         return this.hashCode;
  277.     }
  278.     public String toString() {
  279.         return "QxglDept{dept_id=" + id + ",dept_name=" + deptName
  280.                 + ",dept_Parent_Id=" + deptParentId + ",dept_leve=" + deptLevel
  281.                 + ",deptIdPath=" + deptIdPath + ",dept_fullname="
  282.                 + deptFullname + ",dept_order_id=" + deptOrderId + ",deptArea="
  283.                 + deptArea + "," + "dept_type=" + deptType + "}";
  284.     }
  285.     // public String getDeptCreateTime() {
  286.     // return deptCreateTime;
  287.     // }
  288.     //
  289.     // public void setDeptCreateTime(String deptCreateTime) {
  290.     // this.deptCreateTime = deptCreateTime;
  291.     // }
  292.     public int getDeptOrderId() {
  293.         return deptOrderId;
  294.     }
  295.     public void setDeptOrderId(int deptOrderId) {
  296.         this.deptOrderId = deptOrderId;
  297.     }
  298.     public int getHashCode() {
  299.         return hashCode;
  300.     }
  301.     public void setHashCode(int hashCode) {
  302.         this.hashCode = hashCode;
  303.     }
  304.     /**
  305.      * @return the deptArea
  306.      */
  307.     public String getDeptArea() {
  308.         return deptArea;
  309.     }
  310.     /**
  311.      * @param deptArea
  312.      *            the deptArea to set
  313.      */
  314.     public void setDeptArea(String deptArea) {
  315.         this.deptArea = deptArea;
  316.     }
  317.     /**
  318.      * @return the deptIntroduce
  319.      */
  320.     public String getDeptIntroduce() {
  321.         return deptIntroduce;
  322.     }
  323.     /**
  324.      * @param deptIntroduce
  325.      *            the deptIntroduce to set
  326.      */
  327.     public void setDeptIntroduce(String deptIntroduce) {
  328.         this.deptIntroduce = deptIntroduce;
  329.     }
  330.   }

目标list大小为:19
中国
┣北京市
 ┣市辖区
  ┣东城区
  ┣朝阳区
  ┣西城区
 ┣县
  ┣密云县
  ┣延庆县
┣山东省
 ┣济南市
  ┣历下区
  ┣市中区
  ┣天桥区
 ┣青岛市
 ┣潍坊市
┣河北省
 ┣石家庄
 ┣唐山市
总共花费15毫秒


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值