Java: how to create a Java tree ordred by string

I have a list of lines like this:

{"/ foo", "/ bar", "/ foo / admin", "/ foo / cust", "/ bar / erp", "/ bar / erp / call", "/ foo / cust / profile"}

How to create a path of a tree of constructive lines? Or Where can I find a library that can solve my problems?

the other part is that I want to know how to quote by structure in order to get the data I need (for example, the node tree will contain a string path, but it may also contain a collection of an object with a path attribute), so you can understand that you need complex data structure

A tree can be represented as follows:

- / -- /foo -- -- /foo/admin -- -- /foo/cust -- -- -- /foo/cust/profile -- /bar -- -- /bar/erp -- -- -- /bar/erp/call 

thanks

+6
source share
3 answers

try the following:

 import java.util.*; public class Main { public static void main(String[] args){ List<String> data = Arrays.asList("/foo", "/bar", "/foo/admin", "/foo/cust", "/bar/erp", "/bar/erp/call", "/foo/cust/profile"); // order by path Collections.sort(data, new Comparator<String>(){ @Override public int compare(String o1, String o2) { return o1.compareTo(o2); } }); for (String s : data){ int length = s.split("/").length - 1; // -1 means.. without empty string for (int i=0; i< length; i++){ System.out.print("-- "); } System.out.println(s); } } } 

//result

 -- /bar -- -- /bar/erp -- -- -- /bar/erp/call -- /foo -- -- /foo/admin -- -- /foo/cust -- -- -- /foo/cust/profile 
+2
source

What you need:

  • The main loop that iterates through an array of strings 1 at a time from start to finish.
  • A tokenizer function that splits a path, such as / foo / bar / sid, into an array of strings {'foo', 'bar', 'sid'}.
  • Tree structure (if you don’t know how to represent trees in memory, check out this java instruction: http://vivin.net/2010/01/30/generic-n-ary-tree-in-java/ but it would also be useful take a look at the independent language guide, because it will give you a good overview of the theory behind it: http://people.cis.ksu.edu/~schmidt/300s05/Lectures/Week7b.html ). The top of your tree should be something like "root", since foo and bar should be under the same tree.

How to use them together: Iterates through the main array in 1., passing each line to the tokenizer in 2. one at a time. Use a new tokenized string to go through the tree, using the first token as the first level of the tree, the second as the second, etc. When you encounter tokens that do not exist in the tree, add them.

After you build a tree, you simply iterate through it a branch, repeating its contents.

Greetings and happy coding!

+6
source

You can try using a comparator with arrays this way:

 String array[]={"/foo", "/bar", "/foo/admin", "/foo/cust", "/bar/erp", "/bar/erp/call", "/foo/cust/profile"}; Arrays.sort(array,new Comparator<String>() { @Override public int compare(String o1, String o2) { return o1.compareTo(o2); } }); for(int i=0;i<array.length;i++){ if(i>0){ if(array[i].startsWith(array[i-1])){ System.out.print("\t"); } } System.out.println(array[i]); } 
0
source

Source: https://habr.com/ru/post/920808/


All Articles