- importjava.util.Comparator;
- importcom.work.qxgl.model.QxglDept;
- /**
- *@authorwangmingjie
- *@date2008-9-5上午10:33:59
- */
- publicclassQxglDeptCompartorimplementsComparator<QxglDept>{
- publicintcompare(QxglDepto1,QxglDepto2){
- returno1.getDeptIntroduce().compareTo(o2.getDeptIntroduce());
- }
- }
- importjava.util.LinkedList;
- importjava.util.List;
- importjava.util.Arrays;
- importcom.work.qxgl.model.QxglDept;
- /**
- *递归排序(测试成功!)
- *前提:知道了父节点,知道了节点所在的级别,级别内部的排序号是整数且都是唯一的。
- *@authorwangmingjie
- *@date2008-9-4下午10:05:17
- */
- publicclassSelectTreeTwo{
- //崔的算法:首先,增加一个属性用来组成排序的字符串的,将父结构的排序id组成字符串,规定从第1级开始排序号要定长。(最顶级为0级)
- //然后将组合后的排序字符串排序,递归排序就实现了。
- //这种算法的同一级别内排序号必须不一样才可以,否则排序将失败!
- publicstaticList<QxglDept>init(){
- List<QxglDept>l=newLinkedList<QxglDept>();
- //QxglDeptid,名称,父id,级别,排序号(deptIntroduce是辅助字段)
- QxglDepttemp=newQxglDept("0","中国",null,0,0);
- l.add(temp);
- //=====================第一级==========================
- temp=newQxglDept("2","山东省","0",1,2);
- l.add(temp);
- temp=newQxglDept("3","河北省","0",1,3);
- l.add(temp);
- temp=newQxglDept("1","北京市","0",1,1);
- l.add(temp);
- //0-1-11
- //0-10-1
- //0-9-1
- //=====================第二级==========================
- temp=newQxglDept("11","市辖区","1",2,11);
- l.add(temp);//,"0-1-11"
- temp=newQxglDept("12","县","1",2,12);
- l.add(temp);//,"0-1-12"
- temp=newQxglDept("21","济南市","2",2,1);
- l.add(temp);
- temp=newQxglDept("23","潍坊市","2",2,3);
- l.add(temp);
- temp=newQxglDept("22","青岛市","2",2,2);
- l.add(temp);
- temp=newQxglDept("32","唐山市","3",2,32);
- l.add(temp);
- temp=newQxglDept("31","石家庄","3",2,31);
- l.add(temp);
- //=======================第三级=================================
- temp=newQxglDept("112","朝阳区","11",3,112);
- l.add(temp);
- temp=newQxglDept("111","东城区","11",3,111);
- l.add(temp);
- temp=newQxglDept("113","西城区","11",3,113);
- l.add(temp);
- temp=newQxglDept("122","延庆县","12",3,122);
- l.add(temp);
- temp=newQxglDept("121","密云县","12",3,121);
- l.add(temp);
- temp=newQxglDept("213","天桥区","21",3,213);
- l.add(temp);
- temp=newQxglDept("212","市中区","21",3,212);
- l.add(temp);
- temp=newQxglDept("211","历下区","21",3,211);
- l.add(temp);
- returnl;
- }
- /**
- *入口方法main
- *@paramargs
- */
- publicstaticvoidmain(String[]args){
- longstart=System.currentTimeMillis();
- LinkedList<QxglDept>l=(LinkedList<QxglDept>)SelectTreeTwo.init();//
- intLEN=l.size();
- System.out.println("目标list大小为:"+LEN);
- for(inti=0;i<LEN;i++){
- setOrderString(l,l.get(i),i);
- }
- sortList(l);
- printTree(l);
- System.out.println("总共花费"+(System.currentTimeMillis()-start)+"毫秒");
- }
- /**
- *根据DeptIntroduce,由小到大将list排序
- *
- *@paraml
- */
- publicstaticvoidsortList(LinkedList<QxglDept>l){
- //首先转换成数组
- QxglDept[]dest=newQxglDept[l.size()];
- dest=(QxglDept[])l.toArray(dest);
- Arrays.sort(dest,newQxglDeptCompartor());
- intLEN=l.size();
- l.clear();//必须先清空,然后按照数组的顺序增加
- for(inti=0;i<LEN;i++){
- l.add(i,dest[i]);
- }
- }
- /**
- *找到dept的父节点
- *
- *@paraml
- *@paramdept
- *@return
- */
- publicstaticintgetParentIndex(LinkedList<QxglDept>l,QxglDeptdept){
- intLEN=l.size();
- intresult=-1;
- StringparentId=dept.getDeptParentId();
- for(inti=0;i<LEN;i++){
- if(parentId.equals(l.get(i).getId())){
- result=i;
- break;
- }else{
- continue;
- }
- }
- returnresult;
- }
- /**
- *生成排序号的字符串
- *
- *@paraml
- *@paramdept
- *@parampos
- */
- publicstaticvoidsetOrderString(LinkedList<QxglDept>l,QxglDeptdept,
- intpos){
- if(pos==0){
- dept.setDeptIntroduce(dept.getDeptOrderId()+"");
- }else{
- //首先找到父节点,获取到他的DeptIntroduce,对自己当前的排序号补零;
- QxglDeptparentNode=l.get(getParentIndex(l,dept));
- dept.setDeptIntroduce(parentNode.getDeptIntroduce()+"-"
- +getOrderString(dept.getDeptOrderId()));
- }
- }
- /**
- *2^31=2147483648,最大为10位
- *
- *@paramorderId
- *@return
- */
- publicstaticStringgetOrderString(intorderId){
- Stringtemp="";
- intLEN=(orderId+"").length();
- for(inti=0;i<10-LEN;i++){
- temp=temp+"0";
- }
- temp=temp+orderId;
- returntemp;
- }
- publicstaticvoidprint(List<QxglDept>l){
- for(inti=0;i<l.size();i++){
- QxglDepttemp=l.get(i);
- System.out.println(temp.getDeptName()+"||"
- +temp.getDeptIntroduce());
- }
- }
- /**
- *打印树状菜单
- *@paraml
- */
- publicstaticvoidprintTree(List<QxglDept>l){
- for(inti=0;i<l.size();i++){
- QxglDepttemp=l.get(i);
- if(temp.getDeptLevel()==0)
- System.out.println(l.get(i).getDeptName());
- else{
- for(intj=0;j<temp.getDeptLevel()-1;j++){
- System.out.print(" ");//补齐空格,在html中最好使用全角空格(汉字空格)。
- }
- System.out.println("┣"+l.get(i).getDeptName());
- }
- }
- }
- }
需要的pojo:
- publicclassQxglDeptimplementsSerializable{
- /**
- *
- */
- privatestaticfinallongserialVersionUID=-1536071285282466850L;
- //constructors
- publicQxglDept(){
- initialize();
- }
- /**
- *Constructorforprimarykey
- */
- publicQxglDept(java.lang.Stringid){
- this.setId(id);
- initialize();
- }
- /**
- *Constructorforrequiredfields
- */
- publicQxglDept(java.lang.Stringid,java.lang.StringdeptName,
- java.lang.StringdeptParentId,intdeptLevel,
- intdeptOrderId){
- this.setId(id);
- this.setDeptName(deptName);
- this.setDeptParentId(deptParentId);
- this.setDeptLevel(deptLevel);
- this.setDeptOrderId(deptOrderId);
- initialize();
- }
- protectedvoidinitialize(){
- }
- privateinthashCode=Integer.MIN_VALUE;
- //primarykey
- privatejava.lang.Stringid;
- //fields
- privatejava.lang.StringdeptName;
- privatejava.lang.StringdeptParentId;//父部门的id
- privateintdeptLevel;//部门级别
- privatejava.lang.StringdeptIdPath;//id全路径
- privatejava.lang.StringdeptFullname;//部门全称
- privateintdeptOrderId;//排序id,要能够调整
- //privateStringdeptCreateTime;//创建时间,由数据库自己来维护
- //collections
- privateStringdeptArea;//所在地区
- privateStringdeptType;//部门类别
- privateStringdeptLinkman;//联系人
- privateStringdeptLinkmanphone;//联系人电话
- privateStringdeptEmail;//部门电子邮箱
- privateStringdeptPhone;//部门电话
- privateStringdeptFax;//部门传真
- privateStringdeptAddress;//部门地址
- privateStringdeptPostalcode;//部门邮编
- privateStringdeptIntroduce;//部门简介
- /**
- *@returnthedeptAddress
- */
- publicStringgetDeptAddress(){
- returndeptAddress;
- }
- /**
- *@paramdeptAddress
- *thedeptAddresstoset
- */
- publicvoidsetDeptAddress(StringdeptAddress){
- this.deptAddress=deptAddress;
- }
- /**
- *@returnthedeptEmail
- */
- publicStringgetDeptEmail(){
- returndeptEmail;
- }
- /**
- *@paramdeptEmail
- *thedeptEmailtoset
- */
- publicvoidsetDeptEmail(StringdeptEmail){
- this.deptEmail=deptEmail;
- }
- /**
- *@returnthedeptFax
- */
- publicStringgetDeptFax(){
- returndeptFax;
- }
- /**
- *@paramdeptFax
- *thedeptFaxtoset
- */
- publicvoidsetDeptFax(StringdeptFax){
- this.deptFax=deptFax;
- }
- /**
- *@returnthedeptLinkman
- */
- publicStringgetDeptLinkman(){
- returndeptLinkman;
- }
- /**
- *@paramdeptLinkman
- *thedeptLinkmantoset
- */
- publicvoidsetDeptLinkman(StringdeptLinkman){
- this.deptLinkman=deptLinkman;
- }
- /**
- *@returnthedeptLinkmanphone
- */
- publicStringgetDeptLinkmanphone(){
- returndeptLinkmanphone;
- }
- /**
- *@paramdeptLinkmanphone
- *thedeptLinkmanphonetoset
- */
- publicvoidsetDeptLinkmanphone(StringdeptLinkmanphone){
- this.deptLinkmanphone=deptLinkmanphone;
- }
- /**
- *@returnthedeptPhone
- */
- publicStringgetDeptPhone(){
- returndeptPhone;
- }
- /**
- *@paramdeptPhone
- *thedeptPhonetoset
- */
- publicvoidsetDeptPhone(StringdeptPhone){
- this.deptPhone=deptPhone;
- }
- /**
- *@returnthedeptPostalcode
- */
- publicStringgetDeptPostalcode(){
- returndeptPostalcode;
- }
- /**
- *@paramdeptPostalcode
- *thedeptPostalcodetoset
- */
- publicvoidsetDeptPostalcode(StringdeptPostalcode){
- this.deptPostalcode=deptPostalcode;
- }
- /**
- *@returnthedeptType
- */
- publicStringgetDeptType(){
- returndeptType;
- }
- /**
- *@paramdeptType
- *thedeptTypetoset
- */
- publicvoidsetDeptType(StringdeptType){
- this.deptType=deptType;
- }
- /**
- *Returntheuniqueidentifierofthisclass
- *
- *@hibernate.idgenerator-class="uuid"column="dept_id"
- */
- publicjava.lang.StringgetId(){
- returnid;
- }
- /**
- *Settheuniqueidentifierofthisclass
- *
- *@paramid
- *thenewID
- */
- publicvoidsetId(java.lang.Stringid){
- this.id=id;
- this.hashCode=Integer.MIN_VALUE;
- }
- /**
- *Returnthevalueassociatedwiththecolumn:dept_name
- */
- publicjava.lang.StringgetDeptName(){
- returndeptName;
- }
- /**
- *Setthevaluerelatedtothecolumn:dept_name
- *
- *@paramdeptName
- *thedept_namevalue
- */
- publicvoidsetDeptName(java.lang.StringdeptName){
- this.deptName=deptName;
- }
- /**
- *Returnthevalueassociatedwiththecolumn:dept_parent_id
- */
- publicjava.lang.StringgetDeptParentId(){
- returndeptParentId;
- }
- /**
- *Setthevaluerelatedtothecolumn:dept_parent_id
- *
- *@paramdeptParentId
- *thedept_parent_idvalue
- */
- publicvoidsetDeptParentId(java.lang.StringdeptParentId){
- this.deptParentId=deptParentId;
- }
- /**
- *Returnthevalueassociatedwiththecolumn:dept_level
- */
- publicintgetDeptLevel(){
- returndeptLevel;
- }
- /**
- *Setthevaluerelatedtothecolumn:dept_level
- *
- *@paramdeptLevel
- *thedept_levelvalue
- */
- publicvoidsetDeptLevel(intdeptLevel){
- this.deptLevel=deptLevel;
- }
- /**
- *Returnthevalueassociatedwiththecolumn:dept_id_path
- */
- publicjava.lang.StringgetDeptIdPath(){
- returndeptIdPath;
- }
- /**
- *Setthevaluerelatedtothecolumn:dept_id_path
- *
- *@paramdeptIdPath
- *thedept_id_pathvalue
- */
- publicvoidsetDeptIdPath(java.lang.StringdeptIdPath){
- this.deptIdPath=deptIdPath;
- }
- /**
- *Returnthevalueassociatedwiththecolumn:dept_fullname
- */
- publicjava.lang.StringgetDeptFullname(){
- returndeptFullname;
- }
- /**
- *Setthevaluerelatedtothecolumn:dept_fullname
- *
- *@paramdeptFullname
- *thedept_fullnamevalue
- */
- publicvoidsetDeptFullname(java.lang.StringdeptFullname){
- this.deptFullname=deptFullname;
- }
- publicbooleanequals(Objectobj){
- if(null==obj)
- returnfalse;
- if(!(objinstanceofQxglDept))
- returnfalse;
- else{
- QxglDeptqxglDept=(QxglDept)obj;
- if(null==this.getId()||null==qxglDept.getId())
- returnfalse;
- else
- return(this.getId().equals(qxglDept.getId()));
- }
- }
- publicinthashCode(){
- if(Integer.MIN_VALUE==this.hashCode){
- if(null==this.getId())
- returnsuper.hashCode();
- else{
- StringhashStr=this.getClass().getName()+":"
- +this.getId().hashCode();
- this.hashCode=hashStr.hashCode();
- }
- }
- returnthis.hashCode;
- }
- publicStringtoString(){
- 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+"}";
- }
- //publicStringgetDeptCreateTime(){
- //returndeptCreateTime;
- //}
- //
- //publicvoidsetDeptCreateTime(StringdeptCreateTime){
- //this.deptCreateTime=deptCreateTime;
- //}
- publicintgetDeptOrderId(){
- returndeptOrderId;
- }
- publicvoidsetDeptOrderId(intdeptOrderId){
- this.deptOrderId=deptOrderId;
- }
- publicintgetHashCode(){
- returnhashCode;
- }
- publicvoidsetHashCode(inthashCode){
- this.hashCode=hashCode;
- }
- /**
- *@returnthedeptArea
- */
- publicStringgetDeptArea(){
- returndeptArea;
- }
- /**
- *@paramdeptArea
- *thedeptAreatoset
- */
- publicvoidsetDeptArea(StringdeptArea){
- this.deptArea=deptArea;
- }
- /**
- *@returnthedeptIntroduce
- */
- publicStringgetDeptIntroduce(){
- returndeptIntroduce;
- }
- /**
- *@paramdeptIntroduce
- *thedeptIntroducetoset
- */
- publicvoidsetDeptIntroduce(StringdeptIntroduce){
- this.deptIntroduce=deptIntroduce;
- }
- }
目标list大小为:19
中国
┣北京市
┣市辖区
┣东城区
┣朝阳区
┣西城区
┣县
┣密云县
┣延庆县
┣山东省
┣济南市
┣历下区
┣市中区
┣天桥区
┣青岛市
┣潍坊市
┣河北省
┣石家庄
┣唐山市
总共花费15毫秒