Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to generate a tree structure in java

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Java how to generate a tree structure, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

Tree structure is a very common data structure. The common methods to generate tree structure are recursion and for loop. No matter what method is used, if you can make good use of memory to add tree nodes reasonably, it will greatly improve the efficiency.

The test code is as follows:

Public class TestTree {public static void main (String [] args) {C3p0Jdbc jdbc = new C3p0Jdbc (); String sql = "SELECT code value,up_code upCode,name label,level FROM test_table WHERE level < 4 ORDER BY level"; JSONArray arr = jdbc.query (sql) / / put the data loop into the newly created map Map treeMap = new HashMap (); int length = arr.size (); long T1 = System.currentTimeMillis (); for (int I = 0; I < length; iLoop +) {String key = arr.getJSONObject (I) .getString ("value") TreeMap.put (key, arr.getJSONObject (I));} long T2 = System.currentTimeMillis (); System.out.println ("cycle time:" + (T2-T1) + "ms"); JSONArray new_tree = new JSONArray () / / using a double-layer for loop, according to the judgment condition, add the qualified node data to the tree structure for (int I = 0; I < length; iTunes +) {JSONObject parent = arr.getJSONObject (I); String key = parent.getString ("value") Int level = parent.getIntValue ("level"); / / this is the only place where new objects are needed JSONArray children = new JSONArray (); for (int j = 0; j < length; jacks +) {JSONObject child = arr.getJSONObject (j) String childKey = child.getString ("value"); String upCode = child.getString ("upCode"); if (upCode.equals (key)) {children.add (treeMap.get (childKey)) }} parent.put ("children", children); if (level = = 1) {new_tree.add (parent) } long T3 = System.currentTimeMillis (); System.out.println ("second traversal time:" + (T3-T2) + "ms"); System.out.println ("whole process time:" + (T3-T1) + "ms");}}

Be sure to reduce the number of new objects in the for loop. New objects are time-consuming operations, not only new objects can be time-consuming, but other operations such as converting an object to jsonarray or json will also be time-consuming, because this operation modifies the reference address of objects in memory, and the best part of the test code is to load all objects into map. During the operation of new_tree.add (parent), the memory reference address of the parent object is directly added to the new_tree, so the efficiency is very fast. My test data is 3227 pieces of domestic provincial, municipal, district and county data. The effect of the whole process is between 600 and 850 ms as shown in the figure:

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report