- import java.util.Comparator;
- import com.work.qxgl.model.QxglDept;
- /**
- * @author wangmingjie
- * @date 2008-9-5上午10:33:59
- */
- public class QxglDeptCompartor implements Comparator<QxglDept> {
- public int compare(QxglDept o1, QxglDept o2) {
- return o1.getDeptIntroduce().compareTo(o2.getDeptIntroduce());
- }
- }
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Arrays;
- import com.work.qxgl.model.QxglDept;
- /**
- * 递归排序(测试成功!)
- * 前提:知道了父节点,知道了节点所在的级别,级别内部的排序号是整数且都是唯一的。
- * @author wangmingjie
- * @date 2008-9-4下午10:05:17
- */
- public class SelectTreeTwo {
- // 崔的算法:首先,增加一个属性用来组成排序的字符串的,将父结构的排序id组成字符串,规定从第1级开始排序号要定长。(最顶级为0级)
- // 然后将组合后的排序字符串排序,递归排序就实现了。
- //这种算法的同一级别内排序号必须不一样才可以,否则排序将失败!
- public static List<QxglDept> init() {
- List<QxglDept> l = new LinkedList<QxglDept>();
- //QxglDept id,名称,父id,级别,排序号 (deptIntroduce是辅助字段)
- QxglDept temp = new QxglDept("0", "中国", null, 0, 0);
- l.add(temp);
- // =====================第一级==========================
- temp = new QxglDept("2", "山东省", "0", 1, 2);
- l.add(temp);
- temp = new QxglDept("3", "河北省", "0", 1, 3);
- l.add(temp);
- temp = new QxglDept("1", "北京市", "0", 1, 1);
- l.add(temp);
- // 0-1-11
- // 0-10-1
- // 0-9-1
- // =====================第二级==========================
- temp = new QxglDept("11", "市辖区", "1", 2, 11);
- l.add(temp); // ,"0-1-11"
- temp = new QxglDept("12", "县", "1", 2, 12);
- l.add(temp); // ,"0-1-12"
- temp = new QxglDept("21", "济南市", "2", 2, 1);
- l.add(temp);
- temp = new QxglDept("23", "潍坊市", "2", 2, 3);
- l.add(temp);
- temp = new QxglDept("22", "青岛市", "2", 2, 2);
- l.add(temp);
- temp = new QxglDept("32", "唐山市", "3", 2, 32);
- l.add(temp);
- temp = new QxglDept("31", "石家庄", "3", 2, 31);
- l.add(temp);
- // =======================第三级=================================
- temp = new QxglDept("112", "朝阳区", "11", 3, 112);
- l.add(temp);
- temp = new QxglDept("111", "东城区", "11", 3, 111);
- l.add(temp);
- temp = new QxglDept("113", "西城区", "11", 3, 113);
- l.add(temp);
- temp = new QxglDept("122", "延庆县", "12", 3, 122);
- l.add(temp);
- temp = new QxglDept("121", "密云县", "12", 3, 121);
- l.add(temp);
- temp = new QxglDept("213", "天桥区", "21", 3, 213);
- l.add(temp);
- temp = new QxglDept("212", "市中区", "21", 3, 212);
- l.add(temp);
- temp = new QxglDept("211", "历下区", "21", 3, 211);
- l.add(temp);
- return l;
- }
- /**
- * 入口方法main
- * @param args
- */
- public static void main(String[] args) {
- long start = System.currentTimeMillis();
- LinkedList<QxglDept> l = (LinkedList<QxglDept>) SelectTreeTwo.init(); //
- int LEN = l.size();
- System.out.println("目标list大小为:" + LEN);
- for (int i = 0; i < LEN; i++) {
- setOrderString(l, l.get(i), i);
- }
- sortList(l);
- printTree(l);
- System.out.println("总共花费"+(System.currentTimeMillis()-start)+"毫秒");
- }
- /**
- * 根据DeptIntroduce,由小到大将list排序
- *
- * @param l
- */
- public static void sortList( LinkedList<QxglDept> l) {
- //首先转换成数组
- QxglDept[] dest = new QxglDept[l.size()];
- dest = (QxglDept[]) l.toArray(dest);
- Arrays.sort(dest,new QxglDeptCompartor());
- int LEN = l.size();
- l.clear(); //必须先清空,然后按照数组的顺序增加
- for (int i = 0; i < LEN; i++) {
- l.add(i,dest[i]);
- }
- }
- /**
- * 找到dept的父节点
- *
- * @param l
- * @param dept
- * @return
- */
- public static int getParentIndex(LinkedList<QxglDept> l, QxglDept dept) {
- int LEN = l.size();
- int result = -1;
- String parentId = dept.getDeptParentId();
- for (int i = 0; i < LEN; i++) {
- if (parentId.equals(l.get(i).getId())) {
- result = i;
- break;
- } else {
- continue;
- }
- }
- return result;
- }
- /**
- * 生成排序号的字符串
- *
- * @param l
- * @param dept
- * @param pos
- */
- public static void setOrderString(LinkedList<QxglDept> l, QxglDept dept,
- int pos) {
- if (pos == 0) {
- dept.setDeptIntroduce(dept.getDeptOrderId() + "");
- } else {
- // 首先找到父节点,获取到他的DeptIntroduce,对自己当前的排序号补零;
- QxglDept parentNode = l.get(getParentIndex(l, dept));
- dept.setDeptIntroduce(parentNode.getDeptIntroduce() + "-"
- + getOrderString(dept.getDeptOrderId()));
- }
- }
- /**
- * 2^31= 2147483648 ,最大为10位
- *
- * @param orderId
- * @return
- */
- public static String getOrderString(int orderId) {
- String temp = "";
- int LEN = (orderId + "").length();
- for (int i = 0; i < 10 - LEN; i++) {
- temp = temp + "0";
- }
- temp = temp + orderId;
- return temp;
- }
- public static void print(List<QxglDept> l) {
- for (int i = 0; i < l.size(); i++) {
- QxglDept temp = l.get(i);
- System.out.println(temp.getDeptName() + "||"
- + temp.getDeptIntroduce());
- }
- }
- /**
- * 打印树状菜单
- * @param l
- */
- public static void printTree(List<QxglDept> l) {
- for (int i = 0; i < l.size(); i++) {
- QxglDept temp = l.get(i);
- if (temp.getDeptLevel() == 0)
- System.out.println(l.get(i).getDeptName());
- else {
- for (int j = 0; j < temp.getDeptLevel() - 1; j++) {
- System.out.print(" ");//补齐空格,在html中最好使用全角空格(汉字空格)。
- }
- System.out.println("┣" + l.get(i).getDeptName());
- }
- }
- }
- }
需要的pojo:
- public class QxglDept implements Serializable {
- /**
- *
- */
- private static final long serialVersionUID = -1536071285282466850L;
- // constructors
- public QxglDept() {
- initialize();
- }
- /**
- * Constructor for primary key
- */
- public QxglDept(java.lang.String id) {
- this.setId(id);
- initialize();
- }
- /**
- * Constructor for required fields
- */
- public QxglDept(java.lang.String id, java.lang.String deptName,
- java.lang.String deptParentId, int deptLevel,
- int deptOrderId) {
- this.setId(id);
- this.setDeptName(deptName);
- this.setDeptParentId(deptParentId);
- this.setDeptLevel(deptLevel);
- this.setDeptOrderId(deptOrderId);
- initialize();
- }
- protected void initialize() {
- }
- private int hashCode = Integer.MIN_VALUE;
- // primary key
- private java.lang.String id;
- // fields
- private java.lang.String deptName;
- private java.lang.String deptParentId;// 父部门的id
- private int deptLevel; // 部门级别
- private java.lang.String deptIdPath; // id全路径
- private java.lang.String deptFullname;// 部门全称
- private int deptOrderId; // 排序id,要能够调整
- // private String deptCreateTime; //创建时间 ,由数据库自己来维护
- // collections
- private String deptArea; // 所在地区
- private String deptType;// 部门类别
- private String deptLinkman;// 联系人
- private String deptLinkmanphone;// 联系人电话
- private String deptEmail;// 部门电子邮箱
- private String deptPhone;// 部门电话
- private String deptFax;// 部门传真
- private String deptAddress;// 部门地址
- private String deptPostalcode;// 部门邮编
- private String deptIntroduce;// 部门简介
- /**
- * @return the deptAddress
- */
- public String getDeptAddress() {
- return deptAddress;
- }
- /**
- * @param deptAddress
- * the deptAddress to set
- */
- public void setDeptAddress(String deptAddress) {
- this.deptAddress = deptAddress;
- }
- /**
- * @return the deptEmail
- */
- public String getDeptEmail() {
- return deptEmail;
- }
- /**
- * @param deptEmail
- * the deptEmail to set
- */
- public void setDeptEmail(String deptEmail) {
- this.deptEmail = deptEmail;
- }
- /**
- * @return the deptFax
- */
- public String getDeptFax() {
- return deptFax;
- }
- /**
- * @param deptFax
- * the deptFax to set
- */
- public void setDeptFax(String deptFax) {
- this.deptFax = deptFax;
- }
- /**
- * @return the deptLinkman
- */
- public String getDeptLinkman() {
- return deptLinkman;
- }
- /**
- * @param deptLinkman
- * the deptLinkman to set
- */
- public void setDeptLinkman(String deptLinkman) {
- this.deptLinkman = deptLinkman;
- }
- /**
- * @return the deptLinkmanphone
- */
- public String getDeptLinkmanphone() {
- return deptLinkmanphone;
- }
- /**
- * @param deptLinkmanphone
- * the deptLinkmanphone to set
- */
- public void setDeptLinkmanphone(String deptLinkmanphone) {
- this.deptLinkmanphone = deptLinkmanphone;
- }
- /**
- * @return the deptPhone
- */
- public String getDeptPhone() {
- return deptPhone;
- }
- /**
- * @param deptPhone
- * the deptPhone to set
- */
- public void setDeptPhone(String deptPhone) {
- this.deptPhone = deptPhone;
- }
- /**
- * @return the deptPostalcode
- */
- public String getDeptPostalcode() {
- return deptPostalcode;
- }
- /**
- * @param deptPostalcode
- * the deptPostalcode to set
- */
- public void setDeptPostalcode(String deptPostalcode) {
- this.deptPostalcode = deptPostalcode;
- }
- /**
- * @return the deptType
- */
- public String getDeptType() {
- return deptType;
- }
- /**
- * @param deptType
- * the deptType to set
- */
- public void setDeptType(String deptType) {
- this.deptType = deptType;
- }
- /**
- * Return the unique identifier of this class
- *
- * @hibernate.id generator-class="uuid" column="dept_id"
- */
- public java.lang.String getId() {
- return id;
- }
- /**
- * Set the unique identifier of this class
- *
- * @param id
- * the new ID
- */
- public void setId(java.lang.String id) {
- this.id = id;
- this.hashCode = Integer.MIN_VALUE;
- }
- /**
- * Return the value associated with the column: dept_name
- */
- public java.lang.String getDeptName() {
- return deptName;
- }
- /**
- * Set the value related to the column: dept_name
- *
- * @param deptName
- * the dept_name value
- */
- public void setDeptName(java.lang.String deptName) {
- this.deptName = deptName;
- }
- /**
- * Return the value associated with the column: dept_parent_id
- */
- public java.lang.String getDeptParentId() {
- return deptParentId;
- }
- /**
- * Set the value related to the column: dept_parent_id
- *
- * @param deptParentId
- * the dept_parent_id value
- */
- public void setDeptParentId(java.lang.String deptParentId) {
- this.deptParentId = deptParentId;
- }
- /**
- * Return the value associated with the column: dept_level
- */
- public int getDeptLevel() {
- return deptLevel;
- }
- /**
- * Set the value related to the column: dept_level
- *
- * @param deptLevel
- * the dept_level value
- */
- public void setDeptLevel(int deptLevel) {
- this.deptLevel = deptLevel;
- }
- /**
- * Return the value associated with the column: dept_id_path
- */
- public java.lang.String getDeptIdPath() {
- return deptIdPath;
- }
- /**
- * Set the value related to the column: dept_id_path
- *
- * @param deptIdPath
- * the dept_id_path value
- */
- public void setDeptIdPath(java.lang.String deptIdPath) {
- this.deptIdPath = deptIdPath;
- }
- /**
- * Return the value associated with the column: dept_fullname
- */
- public java.lang.String getDeptFullname() {
- return deptFullname;
- }
- /**
- * Set the value related to the column: dept_fullname
- *
- * @param deptFullname
- * the dept_fullname value
- */
- public void setDeptFullname(java.lang.String deptFullname) {
- this.deptFullname = deptFullname;
- }
- public boolean equals(Object obj) {
- if (null == obj)
- return false;
- if (!(obj instanceof QxglDept))
- return false;
- else {
- QxglDept qxglDept = (QxglDept) obj;
- if (null == this.getId() || null == qxglDept.getId())
- return false;
- else
- return (this.getId().equals(qxglDept.getId()));
- }
- }
- public int hashCode() {
- if (Integer.MIN_VALUE == this.hashCode) {
- if (null == this.getId())
- return super.hashCode();
- else {
- String hashStr = this.getClass().getName() + ":"
- + this.getId().hashCode();
- this.hashCode = hashStr.hashCode();
- }
- }
- return this.hashCode;
- }
- public String toString() {
- return "QxglDept{dept_id=" + id + ",dept_name=" + deptName
- + ",dept_Parent_Id=" + deptParentId + ",dept_leve=" + deptLevel
- + ",deptIdPath=" + deptIdPath + ",dept_fullname="
- + deptFullname + ",dept_order_id=" + deptOrderId + ",deptArea="
- + deptArea + "," + "dept_type=" + deptType + "}";
- }
- // public String getDeptCreateTime() {
- // return deptCreateTime;
- // }
- //
- // public void setDeptCreateTime(String deptCreateTime) {
- // this.deptCreateTime = deptCreateTime;
- // }
- public int getDeptOrderId() {
- return deptOrderId;
- }
- public void setDeptOrderId(int deptOrderId) {
- this.deptOrderId = deptOrderId;
- }
- public int getHashCode() {
- return hashCode;
- }
- public void setHashCode(int hashCode) {
- this.hashCode = hashCode;
- }
- /**
- * @return the deptArea
- */
- public String getDeptArea() {
- return deptArea;
- }
- /**
- * @param deptArea
- * the deptArea to set
- */
- public void setDeptArea(String deptArea) {
- this.deptArea = deptArea;
- }
- /**
- * @return the deptIntroduce
- */
- public String getDeptIntroduce() {
- return deptIntroduce;
- }
- /**
- * @param deptIntroduce
- * the deptIntroduce to set
- */
- public void setDeptIntroduce(String deptIntroduce) {
- this.deptIntroduce = deptIntroduce;
- }
- }
目标list大小为:19
中国
┣北京市
┣市辖区
┣东城区
┣朝阳区
┣西城区
┣县
┣密云县
┣延庆县
┣山东省
┣济南市
┣历下区
┣市中区
┣天桥区
┣青岛市
┣潍坊市
┣河北省
┣石家庄
┣唐山市
总共花费15毫秒