In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to use the tree construction algorithm of Java simple VO". In the daily operation, I believe that many people have doubts about how to use the tree construction algorithm of Java simple VO. 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 "how to use the tree construction algorithm of Java simple VO". Next, please follow the editor to study!
/ * take a collection of list and analyze the attribute name corresponding to the parent id of the tree * * @ param vos * @ param idField-vo according to the parent attribute and subset attribute (only numbers or strings are supported! ) * @ param parentField-the attribute of the parent id (only numbers or strings are supported! ) * @ param childField-the property name of the child collection (java.util.Collection or array is supported) * @ param * @ return * / public static List toTree (List vos, String idField, String parentField, String childField) {if (CollUtil.isEmpty (vos)) {return vos;} / / cache, quickly find HashMap allMap = Maps.newHashMapWithExpectedSize (vos.size ()) of the parent vo For (T vo: vos) {allMap.put (ReflectUtil.getFieldValue (vo, idField), vo);} ArrayList list = Lists.newArrayListWithCapacity (vos.size ()); Object idValue; Object pValue; Object cValue; Collection childs; Collection pChilds; List directChild; HashMap id2Childs = Maps.newHashMapWithExpectedSize (vos.size ()); for (T vo: vos) {/ / get id idValue = ReflectUtil.getFieldValue (vo, idField) / / get the parent id pValue of the current vo = ReflectUtil.getFieldValue (vo, parentField); childs = id2Childs.get (idValue); if (childs = = null) {childs = Sets.newHashSetWithExpectedSize (vos.size ()); id2Childs.put (idValue, childs);} pChilds = id2Childs.get (pValue) If (pChilds = = null) {pChilds = Sets.newHashSetWithExpectedSize (vos.size ()); id2Childs.put (pValue, pChilds);} directChild = findDirectChild (vos, idValue, parentField); childs.addAll (directChild); if (pValue = = null | |! allMap.containsKey (pValue)) {/ / there is no parent id root! List.add (vo);} else {pChilds.add (vo);}} Class aClass = list.get (0). GetClass (); Field childF = ReflectUtil.getField (aClass, childField); boolean isList = Collection.class.isAssignableFrom (childF.getType ()); for (T vo: vos) {/ / get id idValue = ReflectUtil.getFieldValue (vo, idField); childs = id2Childs.get (idValue) If (childs! = null) {if (isList) {ReflectUtil.setFieldValue (vo, childF, childs);} else {ReflectUtil.setFieldValue (vo, childF, toArray (childs));}} return list } / * collection is converted into an array, which is the type of elements in the collection * @ param vos * @ param * @ return * / public static T [] toArray (Collection vos) {Class aClass = vos.iterator (). Next (). GetClass (); T [] os = (T []) Array.newInstance (aClass, vos.size ()); Iterator iterator = vos.iterator (); int I = 0 While (iterator.hasNext ()) {os [iTunes +] = iterator.next ();} return os } / * take a collection of list, according to the parent attribute and the parent id, find his immediate first-grade child * * @ param vos * @ param id * @ param parentField * @ param * @ return * / public static List findDirectChild (List vos, Object id, String parentField) {if (CollUtil.isEmpty (vos)) {return Collections.emptyList ();} ArrayList list = Lists.newArrayListWithCapacity (vos.size ()); Object pValue For (T vo: vos) {/ / get the parent of the current vo id pValue = ReflectUtil.getFieldValue (vo, parentField); if (pValue = = id | | id.equals (pValue)) {list.add (vo);}} return list;} so far, the study on "how to use the tree construction algorithm of Java simple VO" 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.