Building a simple tree structure I'm trying to build this simple tree:




Multiverse
|     
|--- Universe
    |
    |--- Supercluster
        |
        |--- Galaxy
            |
            |--- Andromeda
            |
            |--- Milky way



I came up with this simple method which recursively walk a tree:



/**
 * recursively walk a tree structure
 */ 
public static void recursive(TreeViewItem treeNode){

        System.out.println(treeNode.getText());

        Iterator&lt;TreeViewItem&gt; itr = treeNode.getItems().iterator();

        while (itr.hasNext()) {
            TreeViewItem item = (TreeViewItem) itr.next();
            recursive(item);
        }
}


Is there a better way or more elegant way to do this?
You're visiting and printing the tree elements in a depth-first manner, so depthFirstPrint would be a much better name than "recursive" (which is always a terrible name for any recursive function).

You didn't need this cast:


TreeViewItem item = (TreeViewItem) itr.next();



because you correctly declared the iterator as Iterator&lt;TreeViewItem&gt; itr

As of Java 1.5, you can use a for-each loop, which is simple and elegant:

for (TreeViewItem item : treeNode.getItems()) {
    depthFirstPrint(item);
}