2012-01-24 48 views
3

我在GWT如何在哈希映射迭代,直到所需的对象

我有类FolderColection和文件夹, 在FolderColection类做的节目有哈希映射HM

public class folderCollection{ 

    public Map<String,Folder>FolderCollection = new HashMap<String,Folder>(); 

    public void addFolder(Folder folder){ 
    String folderKey = folder.getKey(); 
    FolderCollection.put(folderKey, folder); 
    } 
} 

在文件夹类

public Class Folder{ 
    // Not a complete code , but an idea of a code 
    String name; 
    String dateOfcreation; 

    private FolderCollection folderCollection = new FolderCollection(); 
    // Folder can also have many sub folders 
    //More variables 
    // all get set methods 
} 

现在例如:所有文件夹

1.A 
    1.Aa 
     1.Aa1 
     2.Aa2 
    2.Ab 
2.B 
    1.Ba 
    2.Bb 
3.C 

A,B,C文件夹位于FolderCollection中。 由于A也是一个文件夹,它包含FolderCollection(文件夹Aa,文件夹Ab)。 类似的文件夹Aa具有FolderCollection(文件夹Aa1,文件夹Aa2)。

我能够做到这一点,我已经在上面解释过了。

我很难找到对象,例如文件夹Aa1的对象。 假设我想更改文件夹Aa2的名称,因为我必须迭代到该对象。

请帮我解决这个问题。

我有路径,因为所有的文件夹名称被添加到树的小部件accordint parentTreeItem。

例如:如果我必须改变为Aa2的名字,那么 我有

String [] path ={A,Aa,Aa2}; 

一些帮助我,这将是很大的帮助。

+4

如果您有路径,则不必通过散列映射_iterate_,只需从各个文件夹集合中获取每个路径元素的值即可。 – Thomas

+1

@Thomas:确切地说,但我认为我必须为它编写递归代码。但我无法做到这一点。请告诉我一小段代码,以便我能理解。 – NewCodeLearner

+1

btw,整理你的类,方法和变量的大小写。类+接口应该以大写字母,方法和变量小写字母开头。 – wmorrison365

回答

0

如果您有路径,则不必遍历散列映射,只需从各个文件夹集合中获取每个路径元素的值即可。

1

我希望你将不得不在你的文件夹类上有一个getFolder(?),它提供了文件夹名称,并返回具有该名称的子文件夹。你可以使用你的路径遍历这个循环,以“cd”到你感兴趣的级别。然后,我猜getFile将返回一个File对象。您可以然后rename您的文件(尽管您的路径将不得不更新以反映任何文件夹名称更改)。

Folder rootFolder = ...; 
String[] path = ...; 
Folder f = null; 
for (int i=0; i<path.length; i++) { 
    folder = rootFolder.getFolder(path[i]); 
} 

// f now contains the folder at "path" 
// N.B. Haven't handled FolderNotFoundException 
File file = f.getFile("Aa2.txt"); 
file.setName("Aa2.new.txt"); 

/* Folder#getFolder returning sub-folder for name passed */ 
public Folder getFolder(String name) { 
    Folder f = folderCollection.get(name); //no null checks done! 
    if (f == null) { 
     ... //FolderNotFoundException? 
    } 
    return f; 
} 

或者,您可以提供路径(或路径的封装表示)到Folder#getFolderInPath和内部处理这一切Folder。此版本是迭代的,循环遍历路径数组,在每次迭代时使用文件夹路径中的该级别更新f。它使用从上述getFolder

Folder rootFolder = ...; 
String[] path = ...; 
Folder f = rootFolder.getFolderInPath(path); 
File file = f.getFile("Aa2.txt"); 

/* Folder#getFolder returning sub-folder for name passed 
* This is an "iterative" implementation - looping path array 
*/ 
public Folder getFolderInPath(String[] path) { 
    Folder f = null; 
    for (int i=0; i<path.length; i++) { 
     folder = this.getFolder(path[i]); 
    } 
    // f now contains the folder at "path" 
    // N.B. Haven't handled FolderNotFoundException 
    return f; 
} 

这是上面的一个递归版本和再次使用getFolder(以上×2)。需要一些解释 - #getFolderInPath现在只是委托给递归方法#getFolderRecursive,传递递归的根值(起始文件夹[“this”]和起始点在path - index中)。

方法#getFolderRecursive(Folder递归调用自身直到路径被遍历(而索引< path.length)。当它成功遍历时(FolderNotfoundException?),则index = path.length。在每个级别,我们使用匹配路径中该级别名称的文件夹进行递归调用(由索引管理)。在最底层,#getFolder找到的文件夹是路径中的最后一个,应该返回。然后将递归方法解开,将var上传到var folderArtPath(为了清楚起见)。

Folder rootFolder = ...; 
String[] path = ...; 
Folder f = rootFolder.getFolderInPath(path); 
File file = f.getFile("Aa2.txt"); 

/* Folder#getFolder returning sub-folder for name passed 
* This is an "recursive" implementation - digging into the Folder structure 
*/ 
public Folder getFolderInPath(String[] path) { 
    //Begin recursion with "this2 folder" 
    return getFolderRecursive(this, path, 0); 
} 

/* Internal recursive method 
*/ 
private Folder getFolderRecursive(Folder baseFolder, String[] path, int index) { 

    Folder folderAtPath = null; //This is going to carry the Folder at "path" 

    if (index < path.length) { //Recursive base condition (are we done?) 

     //Get folder f with name according to path and index 
     Folder f = baseFolder.getFolder(path[index])); //FolderNotFoundException? 

     //Recursively call found folder f with path and index referring 
     //to next path-part to be used (index+1) 
     folderAtPath = getFolderRecursive(f, path, index+1); 
    } 
    else { 
     folderAtPath = baseFolder; 
    } 

    return folderAtPath; 
} 

可能有更好的方法来做到这一点,但我不能审视它:当路径遍历,它是在IF块进行了堆栈在该行folderAtPath =else条件设置该变量现在。花了太多时间,但不得不纠正我的迷你厨师。递归有点有趣......找到一个简单的例子,并玩它。

你也可能想让文件夹和文件有一些共同的接口。

+0

哎呦,小概率。将编辑它。 – wmorrison365

+0

现在纠正小概率和花费的方式太多了。哎呦! – wmorrison365

+0

Thanks.But你不明白我的问题。 文件夹类是用户定义的类。它具有名称,ID等文件夹的属性。此文件夹保存在foldercolection的Hashmap中,每个文件夹都有其自己的floder集合。 你可以查看Thomas的评论。他得到它写。但我无法做到。 – NewCodeLearner