In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "using JAVA to write infinite tree menu code". In daily operation, I believe that many people have doubts about writing infinite tree menu code with JAVA. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "writing infinite tree menu code with JAVA". Next, please follow the editor to study!
Because we often come across a common method written by a tree structure at work, although there has been unlimited code before, it is limited to objects, and different objects or different fields in objects cannot be used.
This method accepts any type of List collection and returns a List collection with all the subsets stitched together. Note that the List received by the method and the List returned by the method are the same object.
This method is implemented in the form of Map, so you need to provide field names in terms of parameters, so you can avoid writing a lot of repetitive code because the parent is different from your own fields. In addition, the name of the subset can be customized. If the name is the same as my refactoring method, you can use the refactoring method (in fact, this is useless, but it may be more convenient)
Don't spray if you write by yourself, if there is a better way to comment, thank you!
Private static List all / * * @ param list any type of list collection * @ param id the field name of the id at this level * @ param parentId the field name of the Id at the upper level * @ the name of the subset in the param childrenListName object * @ the parent of the highest level of param firstId * @ return returns any type of List * @ throws Exception conversion exception * / public static List toJson (List list, String id, String parentId, String childrenListName,Integer firstId) throws Exception {List mapList = new ArrayList () For (Object o: list) {Map map = ObjectToMapUtils.objectToMap (o); mapList.add (map);} all = new ArrayList (mapList); List root = new ArrayList (); for (Map map: mapList) {if (Integer.parseInt (map.get (parentId). ToString ()) = firstId) {root.add (map);}} all.removeAll (root) For (Map map: root) {map.put (childrenListName,getChildren (map,id,parentId,childrenListName));} Gson gson = new Gson (); List lists = gson.fromJson (JSONObject.toJSONString (root), list.getClass ()); return lists;} public static List toJson (List list) throws Exception {return toJson (list, "id", "parentId", "list",-1) } public static List toJson (List list,String id) throws Exception {return toJson (list,id, "parentId", "list",-1);} public static List toJson (List list,String id,String parentId) throws Exception {return toJson (list,id,parentId, "list",-1);} public static List toJson (List list,String id,String parentId,String childrenListName) throws Exception {return toJson (list,id,parentId,childrenListName,-1) } public static List getChildren (Map parent,String id,String parentId,String childrenListName) {List mapList; if (parent.containsKey (childrenListName) & & parent.get (childrenListName)! = null) {mapList = (List) parent.get (childrenListName);} else {mapList = new ArrayList () } for (Map map: all) {if (Integer.parseInt (parent.get (id). ToString ()) = Integer.parseInt (map.get (parentId). ToString ()) {mapList.add (map);}} if (mapList! = null) {all.removeAll (mapList); for (Map map: mapList) {map.put (childrenListName,getChildren (map,id,parentId,childrenListName)) }} return mapList;} public static void main (String [] arg) {List list = new ArrayList (); try {TreeUtils.toJson (list, "id", "parentId", "list", 0);} catch (Exception e) {}} so far, the study on "writing infinite tree menu code with JAVA" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.