开发者

Make a Tree from List of Array (ResultSet for example)

开发者 https://www.devze.com 2023-03-08 18:46 出处:网络
I have a List of Arrays, like this one: List<String[]>开发者_如何转开发 myList = new ArrayList<String[]>();

I have a List of Arrays, like this one:

List<String[]>开发者_如何转开发 myList = new ArrayList<String[]>();
myList.add( new String[]{"A1","B1","C1","D1","values"} );
myList.add( new String[]{"A1","B1","C2","D1","values"} );
myList.add( new String[]{"A1","B1","C2","D2","values"} );
myList.add( new String[]{"A2","B1","C1","D1","values"} );
myList.add( new String[]{"A2","B1","C1","D2","values"} );

I need fill An Object that have dependences with the fathers, so:

  • A1 have only one child, B1.
    • B1 have 2 children, C1 and C2.
      • C1 have 1 child, D1, that have the values...
      • C2 have 2 children, D1 and D2, that have the values...
  • A2 have only one child, B1 (Not the same that the other one)
    • B1 have only one child, C1... etc.

What kind of structure do you think is better? I need the names A1, B1, etc.

I have probe with Maps, Arrays, Lists... I think that the algorithm is not possible... :( :(

Please, HELP!

Edit explaining:

I have a ResultSet from a DataBase that have 5 or 6 GROUP BY clauses. I have all the plain data and

I need to make an structure with Text Objects, for example:

Person A - Building 1 - Tower 1 - Some Text A

Person A - Building 1 - Tower 2 - Another Text

Person A - Building 2 - Tower 1 - Another one Text

Person A - Building 2 - Tower 3 - My Text

Person B - Building 1 - Tower 2 - Any Text

Person B - Building 3 - Tower 1 - A Text...

I need an Object structure for this data... Is it possible?


I DID IT ! OH MY GOD! :D :D :D :D :D :D :D...

private static void finalFillerTotalSuperSpecial(List<String[]> initialList, HashMap<String,Object> mapa){

     String[] currentElement = null;
     String currentKey = null;

     String[] nextElement = null;
     String nextKey = null;
     int i=0,start,end;

     while (i < initialList.size()) {
     start = i;

     currentElement = initialList.get( i++ );
     currentKey = currentElement[0];

     if (i<initialList.size()){
         nextElement = initialList.get( i );
         nextKey = nextElement[0];
     }

     HashMap<String,Object> insideMap = new HashMap<String,Object>(); 
     mapa.put(currentKey, insideMap);

     while (currentKey.equals(nextKey) && i < initialList.size()) {
         currentElement = initialList.get( i++ );
         currentKey = currentElement[0];

         if (i<initialList.size()){
         nextElement = initialList.get( i );
         nextKey = nextElement[0];
         }

     }
     end = i;

     List<String[]> listOfCurrentElements = new ArrayList<String[]>();
     for (int j=start;j<end;j++)
         listOfCurrentElements.add( getNextArray(initialList.get(j)) );

     if ( listOfCurrentElements.get(0).length>1 )
         finalFillerTotalSuperSpecial(listOfCurrentElements,insideMap);
     else
         insideMap.put(listOfCurrentElements.get(0)[0], null);
     }



 }


Of course it is possible. :)

I think what you're describing can be solved by a generic or n-ary tree. That is a tree where each node can have any number of children. I wrote something that does this in Java.

If you don't want to build a tree can build use a Map<String, Set<String>>. This doesn't really give you easy access to tree operations, but it should maintain the relationships:

Map<String, Set<String>> myNodes = new LinkedHashMap<String, Set<String>>();
for(String[] myArray : myList) {
   String previousNode = null;
   for(String node : myArray) {
      if(myNodes.get(node) == null) {
         myNodes.put(node, new HashSet<String>());
      }

      if(previousNode != null) {
         myNodes.get(previousNode).add(node);
      }

      previousNode = node;
   }
}

So you essentially get (I'm using JSON to demonstrate):

{
   A1: ["B1"],
   A2: ["B1"],
   B1: ["C1", "C2"],
   C1: ["D1"],
   C2: ["D1", "D2"],
   D1: ["values"],
   D2: ["values"]
}

This is much more difficult to traverse however, than a tree.


So you want to build a tree structure from this list specific representation of the tree.

As for any tree structure, its nodes can have 0 to n children, so the children could trivially be stored in a List (or optionally, a Map, if you want fast name lookup). For this task, storing the parent reference doesn't seem necessary.

Then you just need to iterate through myList. For each array element,

  1. Take the root of your existing tree.
  2. Iterate through the array.
  3. For each string in the array, check whether the current tree node has a child node with this name.
  4. If not, create it and add it to the tree.
  5. Move to the child node (so it becomes the current node).
  6. Take the next string from the array and repeat from step 3.
  7. Take the next array from the list and repeat from step 1.
0

精彩评论

暂无评论...
验证码 换一张
取 消