Generic solution for constructing a tree structure from collection of  paths in Java