Creating a tree from a list of arrays (e.g. ResultSet)

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 to populate an object that has dependencies with fathers, so:

  • A1 has only one child, B1.
    • B1 have 2 children, C1 and C2.
      • C1 has 1 child, D1 that matters ...
      • C2 has 2 children, D1 and D2, which matter ...
  • A2 have only one child, B1 (not the same as the other)
    • B1 have only one child, C1 ... etc.

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

I have a probe with maps, arrays, lists ... I think that the algorithm is impossible ... :(: (

Please, help!

Change explanation:

I have a ResultSet from a database containing 5 or 6 GROUP BY clauses. I have all the simple data and

I need to create a 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...

... ?

+3
3

! !: 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);
     }



 }
+3

, .:)

, , , n- . , node . -, Java.

, 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;
   }
}

, ( JSON ):

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

, .

+2

, .

, 0 n , List (, , a Map, ). .

myList.

  • .
  • .
  • , node node .
  • , .
  • node ( node).
  • 3.
  • 1.
+1

All Articles