Java list recursive sort, unordered list sorted by parent relationship (two sort types)

Source: Internet
Author: User

When there is a list of lists that are unordered, the data in the list is associated with ParentID, sorted by Java into two sort types:

The test list used by the top-level no parentid, if it is a special value, modified under the judgment method can be.

First sort: Sort by tree structure

before sorting: 122,13,121,1,131,12,132 ...
unordered
[Testsort [id=122, name= level three B, parentid=12], Testsort [id=13, name= two B, parentid=1], Testsort [id=121, Name= three-A, parentid=12], TestSo RT [Id=1, Name=, Parentid=null], Testsort [id=131, name= level three C, parentid=13], Testsort [id=12, Name= two A, parentid=1], TestS ORT [id=132, name= level three D, parentid=13]]

sorted after: 1, 13,131,132,12,122,121
Sort by hierarchy
Span style= "COLOR: #3366ff" > [Testsort [Id=1, Name=, Parentid=null], Testsort [id=13, name= two level B, parentid=1], Testsort [id=131, name= level three C, parentid=13], Testsort [id=132, name= three D, Parentid=13], Testsort [id=12, Name= two A, parentid=1 ], Testsort [id=122, name= level three B, parentid=12], Testsort [id=121, Name= three-A, parentid=12]]

To test the entity class:

/** * <p> Department list sort test class <p> * @version 1.0 * @author Li_hao * @date April 12, 2018 */public class Dept {private String I D;  Idprivate String name;  Name Private String ParentID;  Parent Idpublic String GetId () {return ID;} public void SetId (String id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public String Getparentid () {return parentid;} public void Setparentid (String parentid) {this.parentid = ParentID;} @Overridepublic String toString () {return "Testsort [id=" + ID + ", name=" + name + ", parentid=" + ParentID + "]";}}

Sort code (first sort):

Import Java.util.arraylist;import Java.util.list;import Java.util.objects;import org.apache.commons.lang.stringutils;/** * <p> list sort, sorted by tree structure list (top no parent node) <p> * Before sorting: 122,  13,121,1,131,12,132. * unordered * [Testsort [id=122, name= level three B, parentid=12], Testsort [id=13, name= two B, parentid=1], TestSort [id=121, name= level Three A, parentid=12], Testsort [Id=1, Name=, Parentid=null], Testsort [id=131, name= three C, parentid=13], TestS ORT [id=12, name= level Two A, parentid=1], Testsort [id=132, name= three D, parentid=13]] * * sorted after: 1,13,131,132,12,122,121 ... * Sorted by tree structure * [Testsort [Id=1, Name=, Parentid=null], Testsort [id=13, name= two level B, parentid=1], Testsort [id=131, name= three C, PARENTID=13], Testsort [id=132, name= level three D, parentid=13], Testsort [id=12, Name= Two-A, parentid=1], Testsort [id=122, Name= Three b, parentid=12], Testsort [id=121, name= level Three A, parentid=12]] * @version 1.0 * @author Li_hao * @date April 12, 2018 */public CLA  SS Deptsort {Private list<dept> resultlist = new arraylist<> (); Output List Private LIST&LT;DEPT> deptlist; Input list/** * sort * @param deptlist */public deptsort (list<dept> deptlist) {this.deptlist = Deptlist;for (Dept dept:thi  S.deptlist) {if (Stringutils.isblank (Dept.getparentid ())) {//When the parent is empty resultlist.add (dept);  When the parent is empty, the top level is first placed in the Output list Findchildren (dept); Query Downlevel}}}/** * Query subordinate * @param dept */private Void Findchildren (Dept dept) {list<dept> Childrenlist = new arraylist< > ();//Traversal input list, query subordinate for (Dept d:deptlist) {if (Objects.equals (Dept.getid (), D.getparentid ())) Childrenlist.add (d);} Traverse to the end, no subordinate, exit traverse if (Childrenlist.isempty ()) {return;} Traverse the downlevel for (Dept d:childrenlist) {resultlist.add (d); Findchildren (d);}} Public list<dept> getresultlist () {return resultlist;} public static list<dept> sort (list<dept> originallist) {return new Deptsort (originallist). Getresultlist () ;} /** * Test * @param args */public static void main (string[] args) {list<dept> originallist = new arraylist<> ();D EPT Dept = New Dept ();d ept = new Dept ();d Ept.setid ("122");d Ept.setname ("Level ThreeB ");d Ept.setparentid (" n "), Originallist.add (dept);d ept = new Dept ();d Ept.setid (" two ");d Ept.setname (" Level B "); Dept.setparentid ("1"); Originallist.add (dept);d ept = new Dept ();d Ept.setid ("121");d Ept.setname ("Level Three A"); Dept.setparentid ("n"), Originallist.add (dept);d ept = new Dept ();d Ept.setid ("1");d ept.setname ("level"); O Riginallist.add (dept);d ept = new Dept ();d Ept.setid ("131");d Ept.setname ("Level three C");d Ept.setparentid ("D"); O Riginallist.add (dept);d ept = new Dept ();d Ept.setid ("n");d Ept.setname ("Level Two A");d Ept.setparentid ("1"); O Riginallist.add (dept);d ept = new Dept ();d Ept.setid ("" ");d Ept.setname (" level Three D ");d Ept.setparentid (" "); o Riginallist.add (dept); List<dept> resultlist = sort (originallist);        SYSTEM.OUT.PRINTLN ("Input list:" + originallist); SYSTEM.OUT.PRINTLN ("Output list:" + Resultlist);}}

Test results:

Before sorting:

[Testsort [id=122, name= level three B, parentid=12], Testsort [id=13, name= two B, parentid=1], Testsort [id=121, Name= three A, parentid=1 2], Testsort [Id=1, Name=, Parentid=null], Testsort [id=131, name= Three-level C, parentid=13], Testsort [id=12, Name= Two-A, parentid =1], Testsort [id=132, name= level three D, parentid=13]]

After sorting:

[Testsort [Id=1, name= level, parentid=null], Testsort [id=13, name= two B, parentid=1], Testsort [id=131, name= three C, parentid=13 ], Testsort [id=132, name= level three D, parentid=13], Testsort [id=12, Name= two A, parentid=1], Testsort [id=122, name= three B, parentid= Testsort [id=121, name= level Three A, parentid=12]]

Second sort: Sort by hierarchy from top to bottom, with a piece of sibling

before sorting: 122,13,121,1,131,12,132 ...
unordered
[Testsort [id=122, name= level three B, parentid=12], Testsort [id=13, name= two B, parentid=1], Testsort [id=121, Name= three-A, parentid=12], TestSo RT [Id=1, Name=, Parentid=null], Testsort [id=131, name= level three C, parentid=13], Testsort [id=12, Name= two A, parentid=1], TestS ORT [id=132, name= level three D, parentid=13]]

sorted after: 1, 13,12,131,132,122,121
Sort by hierarchy
Span style= "COLOR: #3366ff" > [Testsort [Id=1, Name=, Parentid=null], Testsort [id=13, name= two level B, parentid=1], Testsort [id=12, name= level Two A, parentid=1], Testsort [id=131, name= three C, parentid=13], Testsort [id=132, name= three D, parentid=13 ], Testsort [id=122, name= level three B, parentid=12], Testsort [id=121, Name= three-A, parentid=12]]

Sort code (second sort):

Import Java.util.arraylist;import java.util.iterator;import Java.util.list;import Java.util.objects;import Java.util.treemap;import org.apache.commons.lang.stringutils;/** * * <p> list sort, sort by hierarchy from top to bottom list, sibling drop piece (top no parent node) <p> * * Before sorting: 122,13,121,1,131,12,132 ... * unordered * [Testsort [id=122, name= level three B, parentid=12], Testsort [id=13, name= two B, parentid=1], Testsort [id=121, name= three A, par ENTID=12], Testsort [Id=1, Name=, Parentid=null], Testsort [id=131, name= level three C, parentid=13], Testsort [id=12, Name= Two-A, p Arentid=1], Testsort [id=132, name= level three D, parentid=13]] * * sorted after: 1,13,12,131,132,122,121 ... * Sorted by hierarchy * [Testsort [Id=1, Name=, Parentid=null], Testsort [id=13, name= Two-level B, parentid=1], Testsort [id=12, Name= Two-A, PA Rentid=1], Testsort [id=131, name= level three C, parentid=13], Testsort [id=132, name= three D, Parentid=13], Testsort [id=122, name= three B , parentid=12], Testsort [id=121, name= level Three A, parentid=12]] * @version 1.0 * @author Li_hao * @date April 12, 2018 */public Clas s DeptSort2 {private treemap<inteGER, list<dept>> TreeMap;  Defines a treemap,key is a rank, value is the current level for all objects that correspond to list private Integer levels = 2;  Define private list<dept> resultlist = new ArrayList ();  Output list private list<dept> deptlist; Input list/** * Sort * @param deptlist */Public DeptSort2 (list<dept> deptlist) {THIS.DEPTL        ist = deptlist; for (Dept dept:this.deptList) {if (Stringutils.isblank (Dept.getparentid ())) {//When the parent is null result  List.add (dept);                When the parent is empty, the top level is first placed in the output list treeMap = new treemap<> ();  Findchildren (dept);  Query downlevel Iterator it = Treemap.keyset (). Iterator (); Iterate TreeMap while (It.hasnext ()) {//checks if there are elements in the sequence, and if there are elements in the iteration returning True (because TreeMap has all the lists under Level 2 and Level 2, you only need to judge It.has  Hnext) Resultlist.addall (Treemap.get (It.next ())); Add all the lists in TreeMap to Resultlist}}}}/** * Query the Subordinate department * method to log the current layer when you go in Series, FINDCWhen the Hildren method is finished, it means that there is no logic in this layer, and it is handed back to the previous layer, so this point level minus one * @param dept */private void Findchildren (Dept dept) {  Integer level = this.level++;            The first time you come in, the level value is 2,this.level value of 3 try {list<dept> childrenlist = new arraylist<> ();                    Traverse the input list and query for subordinate for (Dept d:deptlist) {if (Objects.equals (Dept.getid (), D.getparentid ()))            Childrenlist.add (d);            }//Traverse to the end, no subordinate, exit traverse if (Childrenlist.isempty ()) {return; }//to traverse subordinates for (Dept d:childrenlist) {Addtomap (level, d);//Add rank and corresponding Dept to TreeMap (  The level value for the first execution is 2) Findchildren (d);            Query subordinates, (for example: the first time the level value is 2,this.level value is 3, after entering this method, level is 3,this.level 4, did not find out, level minus one)}} finally {  this.level--; Because the value of This.level is +1 when the Findchildren is executed again, the finally:this.level--} void Addtomap is required after execution (Integer level, DEP T dept) {        if (Objects.isnull (Treemap.get)) Treemap.put (level, New arraylist<dept> ());  If TreeMap has no rank treemap.get (level). Add (dept);    If there is a rank in the TreeMap, add Dept} public list<dept> getresultlist () {return resultlist; public static list<dept> sort (list<dept> originallist) {return new DeptSort2 (originallist). GE    Tresultlist ();        } public static void Main (string[] args) {list<dept> originallist = new arraylist<> ();                Dept Dept = new Dept (); Dept = New Dept ();d Ept.setid ("122");d Ept.setname ("level three B");d Ept.setparentid ("n"), Originallist.add (dept);d EPT = new Dept ();d Ept.setid ("two");d Ept.setname ("Level B");d Ept.setparentid ("1"), Originallist.add (Dept);d ept = new Dept (); Dept.setid ("121");d Ept.setname ("Level Three A");d Ept.setparentid ("n"), Originallist.add (dept);d ept = new Dept ();d Ept.setid ( "1");d ept.setname ("level"), Originallist.add (dept);d ept = new Dept ();d Ept.setid ("131");d Ept.setname ("Level three C");d EPt.setparentid ("Originallist.add"), dept;d ept = new Dept ();d Ept.setid ("a");d Ept.setname ("Level Two A"); Dept.setparentid ("1"); Originallist.add (dept);d ept = new Dept ();d Ept.setid ("section");d Ept.setname ("level three D");        Dept.setparentid ("the"); Originallist.add (dept);        list<dept> resultlist = Deptsort2.sort (originallist);    SYSTEM.OUT.PRINTLN ("Input list:" + originallist);    SYSTEM.OUT.PRINTLN ("Output list:" + resultlist); }}

Test results:

Before sorting:

[Testsort [id=122, name= level three B, parentid=12], Testsort [id=13, name= two B, parentid=1], Testsort [id=121, Name= three A, parentid=1 2], Testsort [Id=1, Name=, Parentid=null], Testsort [id=131, name= Three-level C, parentid=13], Testsort [id=12, Name= Two-A, parentid =1], Testsort [id=132, name= level three D, parentid=13]]

After sorting:

[Testsort [Id=1, Name=, Parentid=null], Testsort [id=13, name= two B, parentid=1], Testsort [id=12, Name= Two-A, parentid=1], Testsort [id=131, name= level three C, parentid=13], Testsort [id=132, name= three D, Parentid=13], Testsort [id=122, name= three B, parentid= Testsort [id=121, name= level Three A, parentid=12]]

Java list recursive sort, unordered list sorted by parent relationship (two sort types)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.