Java Tree Structure Tool class

Source: Internet
Author: User

A long time to write a blog, struggling because there is not a moment ... Never mind. From now on, every day I will try to spare some time to do a summary. I think in the project needs to think very useful things, show to everyone, hope that we learn together, progress together. The first time to do a summary, write bad, put forward to study together, thank you.

Analysis: In the business needs of total, we always encounter some business. For example: to the hierarchy of user relations to traverse, the resource permissions to traverse ... Wait a minute. These businesses are very common. In the same project, his code traversal is similar, in order to more efficient reuse of code, we need to make deep-turn. Here is a little time for me to carry out the package, there is a need for direct use, reproduced please indicate the source.

This code is mainly to save the hierarchy of the relationship between the entity class to traverse, to be generic encapsulation. Not much to say: on the code.


Import java.io.Serializable;
Import java.util.ArrayList;
Import java.util.List;

/**
*
* Description: Grassroots tree structure entity class--id and ParentID and Childslist
* XFZ
* Date: July 2, 2016
* Version Number: 1.0
*/
public class Basetreeobj<e, ID extends serializable> implements serializable{
Private static final long serialversionuid = 1L;
Private ID ID;
Private ID ParentID;
Private list<e> childslist=new arraylist<e> ();
Public ID getId () {
return ID;
}
public void SetId (ID ID) {
This.id = ID;
}
Public ID Getparentid () {
return parentid;
}
public void Setparentid (ID parentid) {
This.parentid = ParentID;
}
Public list<e> getchildslist () {
return childslist;
}
public void Setchildslist (list<e> childslist) {
This.childslist = childslist;
}
}


Import java.io.Serializable;
Import java.util.List;

/**
*
* Description: Tree structure service class
* XFZ
* Date: July 2, 2016
* Version Number: 1.0
*/
Public interface treeinterface<t extends Basetreeobj<t,id>, ID extends serializable>{
/**
* Get all the archives under the specified node
* @param list
* @param parentid
* @return
* @author XFZ
* 1:09:49
*/
Public list<t> getchildtreeobjects (list<t> list,id parentid);
/**
* Recursive list
* @param list
* @param t
* @author XFZ
* 1:11:57
*/
public void Recursionfn (list<t> list,t T);
/**
* Get all child nodes under the specified node
* @param list
* @param t
* @return
* @author XFZ
* 1:12:55
*/
Public list<t> getchildlist (list<t> list,t T);
/**
* Determine if there is a next child node
* @param list
* @param t
* @return
* @author XFZ
* 1:13:43
*/
public boolean haschild (list<t> List, T T);
}


Import java.io.Serializable;
Import java.util.ArrayList;
Import Java.util.Iterator;
Import java.util.List;

/**
*
* Description: Get tree-shaped service implementation XFZ

* Date: July 2, 2016

* Version Number: 1.0
*/
Public abstract class Treeservice<t extends Basetreeobj<t,id>, ID extends serializable> implements
Treeinterface<t, id> {

Public list<t> getchildtreeobjects (list<t> List, ID parentid) {
list<t> returnlist = new arraylist<t> ();
for (iterator<t> Iterator = List.iterator (); Iterator.hasnext ();) {
T res= (t) iterator.next ();
/**
* Determines whether the first object is the first node
*
*/
if (Res.getparentid () ==parentid) {
/**
* Equal--Indicates that the first node is the parent node--the child node under recursion
*/
RECURSIONFN (list, res);
Returnlist.add (RES);
}
}
return returnlist;
}

/**
* Recursive list
*
* @param list
* @param t
* @author XFZ

* 1:11:57
*/
public void Recursionfn (list<t> List, T t) {
List<t> childslist=getchildlist (list, T);
/**
* Set his subset object set
*/
T.setchildslist (childslist);
/**
* Iterations--objects of these subsets--when there are sub-objects of the next level
*/
for (T nextchild:childslist) {
/**
* Next object, with all the resource sets to judge
*/
if (Haschild (list, nextchild)) {
/**
* There is the next child node, recursive
*/
Iterator<t> it = Childslist.iterator ();
while (It.hasnext ()) {
T node = It.next ();
/**
* All objects--with the current object child node of this childslist
*/
RECURSIONFN (list, node);
}
}
}
}

/**
* Get all child nodes under the specified node
*
* @param list
* @param t
* @return
* @author XFZ

* 1:12:55
*/
Public list<t> getchildlist (list<t> List, T t) {
List<t> childslist=new arraylist<t> ();
Iterator<t> It=list.iterator ();
while (It.hasnext ()) {
T Child=it.next ();
/**
* Determines whether the parent ID of the collection equals the ID of the previous level
*/
if ((basetreeobj<t,id>) child). Getparentid () = = ((basetreeobj<t,id>) T). GetId ()) {
Childslist.add (child);
}
}
return childslist;
}

/**
* Determine if there is a next child node
*
* @param list
* @param t
* @return
* @author XFZ

* 1:13:43

*/
public boolean haschild (list<t> List, T t) {
return getchildlist (list, T). Size () > 0? True:false;
}
}

Java Tree Structure Tool class

Related Article

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.