java 从数组列表制作一棵树(例如 ResultSet)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6126723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 14:26:30  来源:igfitidea点击:

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

javaalgorithmcollectionstree

提问by ganzux

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.
  • A1只有一个孩子,B1。
    • B1有2个孩子,C1和C2。
      • C1 有 1 个孩子 D1,其值...
      • C2 有 2 个孩子,D1 和 D2,它们的值...
  • A2只有一个孩子,B1(和另一个不一样)
    • B1只有一个孩子,C1……等等。


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

你认为什么样的结构更好?我需要名称 A1、B1 等。

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

我有一个来自数据库的 ResultSet,它有 5 或 6 个 GROUP BY 子句。我有所有的普通数据和

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?

我需要这个数据的对象结构......这可能吗?

采纳答案by ganzux

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

我做的 !我的天啊!: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);
     }



 }

回答by Vivin Paliath

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 wrotesomething that does this in Java.

我认为您所描述的可以通过通用或n 元树来解决。那是一棵树,其中每个节点可以有任意数量的子节点。我用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>>. 这并不能真正让您轻松访问树操作,但它应该维护关系:

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):

所以你基本上得到(我使用 JSON 来演示):

{
   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.

然而,这比树更难遍历。

回答by Péter T?r?k

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 nchildren, 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.

对于任何树结构,它的节点可以有 0 到n个子节点,因此子节点可以简单地存储在 a List(或者Map,如果您想要快速名称查找,则可以选择 a )。对于此任务,似乎没有必要存储父引用。

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

然后你只需要遍历myList. 对于每个数组元素,

  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.
  1. 取现有树的根。
  2. 遍历数组。
  3. 对于数组中的每个字符串,检查当前树节点是否有具有此名称的子节点。
  4. 如果没有,请创建它并将其添加到树中。
  5. 移动到子节点(因此它成为当前节点)。
  6. 从数组中取出下一个字符串并从步骤 3 开始重复。
  7. 从列表中取出下一个数组并从步骤 1 开始重复。